v2 backwards compat: SdkError status code (#2049)
This commit is contained in:
parent
22595b9685
commit
4f226c1e35
3 changed files with 137 additions and 45 deletions
|
|
@ -3,7 +3,7 @@ import { createServer } from 'node:http';
|
|||
import type { AddressInfo } from 'node:net';
|
||||
|
||||
import type { JSONRPCMessage, OAuthTokens } from '@modelcontextprotocol/core';
|
||||
import { OAuthError, OAuthErrorCode, SdkError, SdkErrorCode } from '@modelcontextprotocol/core';
|
||||
import { OAuthError, OAuthErrorCode, SdkErrorCode, SdkHttpError } from '@modelcontextprotocol/core';
|
||||
import { listenOnRandomPort } from '@modelcontextprotocol/test-helpers';
|
||||
import type { Mock, Mocked, MockedFunction, MockInstance } from 'vitest';
|
||||
|
||||
|
|
@ -1575,7 +1575,7 @@ describe('SSEClientTransport', () => {
|
|||
await expect(transport.send(message)).rejects.toThrow(UnauthorizedError);
|
||||
});
|
||||
|
||||
it('enforces circuit breaker on double-401: onUnauthorized called once, then throws SdkError', async () => {
|
||||
it('enforces circuit breaker on double-401: onUnauthorized called once, then throws SdkHttpError', async () => {
|
||||
postResponses = [401, 401];
|
||||
await setupServer();
|
||||
|
||||
|
|
@ -1587,8 +1587,9 @@ describe('SSEClientTransport', () => {
|
|||
await transport.start();
|
||||
|
||||
const error = await transport.send(message).catch(e => e);
|
||||
expect(error).toBeInstanceOf(SdkError);
|
||||
expect((error as SdkError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect(error).toBeInstanceOf(SdkHttpError);
|
||||
expect((error as SdkHttpError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect((error as SdkHttpError).status).toBe(401);
|
||||
expect(authProvider.onUnauthorized).toHaveBeenCalledTimes(1);
|
||||
expect(postCount).toBe(2);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import type { JSONRPCMessage, JSONRPCRequest } from '@modelcontextprotocol/core';
|
||||
import { OAuthError, OAuthErrorCode, SdkError, SdkErrorCode } from '@modelcontextprotocol/core';
|
||||
import { OAuthError, OAuthErrorCode, SdkErrorCode, SdkHttpError } from '@modelcontextprotocol/core';
|
||||
import type { Mock, Mocked } from 'vitest';
|
||||
|
||||
import type { OAuthClientProvider } from '../../src/client/auth.js';
|
||||
|
|
@ -240,8 +240,9 @@ describe('StreamableHTTPClientTransport', () => {
|
|||
transport.onerror = errorSpy;
|
||||
|
||||
await expect(transport.send(message)).rejects.toThrow(
|
||||
new SdkError(SdkErrorCode.ClientHttpNotImplemented, 'Error POSTing to endpoint: Session not found', {
|
||||
new SdkHttpError(SdkErrorCode.ClientHttpNotImplemented, 'Error POSTing to endpoint: Session not found', {
|
||||
status: 404,
|
||||
statusText: 'Not Found',
|
||||
text: 'Session not found'
|
||||
})
|
||||
);
|
||||
|
|
@ -1871,8 +1872,9 @@ describe('StreamableHTTPClientTransport', () => {
|
|||
.mockResolvedValueOnce(unauthedResponse);
|
||||
|
||||
const error = await transport.send(message).catch(e => e);
|
||||
expect(error).toBeInstanceOf(SdkError);
|
||||
expect((error as SdkError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect(error).toBeInstanceOf(SdkHttpError);
|
||||
expect((error as SdkHttpError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect((error as SdkHttpError).status).toBe(401);
|
||||
expect(mockAuthProvider.saveTokens).toHaveBeenCalledWith({
|
||||
access_token: 'new-access-token',
|
||||
token_type: 'Bearer',
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import type { IncomingMessage, Server } from 'node:http';
|
|||
import { createServer } from 'node:http';
|
||||
|
||||
import type { JSONRPCMessage, OAuthClientInformation, OAuthClientMetadata, OAuthTokens } from '@modelcontextprotocol/core';
|
||||
import { SdkError, SdkErrorCode } from '@modelcontextprotocol/core';
|
||||
import { SdkErrorCode, SdkHttpError } from '@modelcontextprotocol/core';
|
||||
import { listenOnRandomPort } from '@modelcontextprotocol/test-helpers';
|
||||
import type { Mock } from 'vitest';
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ describe('StreamableHTTPClientTransport with AuthProvider', () => {
|
|||
expect(retryInit.headers.get('Authorization')).toBe('Bearer new-token');
|
||||
});
|
||||
|
||||
it('should throw SdkError(ClientHttpAuthentication) if retry after onUnauthorized also gets 401', async () => {
|
||||
it('should throw SdkHttpError(ClientHttpAuthentication) if retry after onUnauthorized also gets 401', async () => {
|
||||
const authProvider: AuthProvider = {
|
||||
token: vi.fn(async () => 'still-bad'),
|
||||
onUnauthorized: vi.fn(async () => {})
|
||||
|
|
@ -99,8 +99,9 @@ describe('StreamableHTTPClientTransport with AuthProvider', () => {
|
|||
.mockResolvedValueOnce({ ok: false, status: 401, headers: new Headers(), text: async () => 'unauthorized' });
|
||||
|
||||
const error = await transport.send(message).catch(e => e);
|
||||
expect(error).toBeInstanceOf(SdkError);
|
||||
expect((error as SdkError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect(error).toBeInstanceOf(SdkHttpError);
|
||||
expect((error as SdkHttpError).code).toBe(SdkErrorCode.ClientHttpAuthentication);
|
||||
expect((error as SdkHttpError).status).toBe(401);
|
||||
expect(authProvider.onUnauthorized).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue