diff --git a/src/browser/setupWorker/start/createFallbackRequestListener.ts b/src/browser/setupWorker/start/createFallbackRequestListener.ts index 733317e09..37a3473ef 100644 --- a/src/browser/setupWorker/start/createFallbackRequestListener.ts +++ b/src/browser/setupWorker/start/createFallbackRequestListener.ts @@ -8,7 +8,7 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest' import { SetupWorkerInternalContext, StartOptions } from '../glossary' import type { RequiredDeep } from '~/core/typeUtils' import { handleRequest } from '~/core/utils/handleRequest' -import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers' +import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly' export function createFallbackRequestListener( context: SetupWorkerInternalContext, diff --git a/src/browser/setupWorker/start/createRequestListener.ts b/src/browser/setupWorker/start/createRequestListener.ts index 755f83633..9e2a7aee0 100644 --- a/src/browser/setupWorker/start/createRequestListener.ts +++ b/src/browser/setupWorker/start/createRequestListener.ts @@ -13,7 +13,7 @@ import { handleRequest } from '~/core/utils/handleRequest' import { RequiredDeep } from '~/core/typeUtils' import { devUtils } from '~/core/utils/internal/devUtils' import { toResponseInit } from '~/core/utils/toResponseInit' -import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers' +import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly' export const createRequestListener = ( context: SetupWorkerInternalContext, diff --git a/src/core/handlers/RequestHandler.ts b/src/core/handlers/RequestHandler.ts index f9d34f384..bd610ea04 100644 --- a/src/core/handlers/RequestHandler.ts +++ b/src/core/handlers/RequestHandler.ts @@ -117,6 +117,8 @@ export abstract class RequestHandler< StrictRequest >() + private readonly __kind: 'RequestHandler' + public info: HandlerInfo & RequestHandlerInternalInfo /** * Indicates whether this request handler has been used @@ -151,6 +153,7 @@ export abstract class RequestHandler< } this.isUsed = false + this.__kind = 'RequestHandler' } /** diff --git a/src/core/handlers/WebSocketHandler.ts b/src/core/handlers/WebSocketHandler.ts index 26d443872..6413640a8 100644 --- a/src/core/handlers/WebSocketHandler.ts +++ b/src/core/handlers/WebSocketHandler.ts @@ -28,6 +28,8 @@ const kStopPropagationPatched = Symbol('kStopPropagationPatched') const KOnStopPropagation = Symbol('KOnStopPropagation') export class WebSocketHandler { + private readonly __kind: 'WebSocketHandler' + public id: string public callFrame?: string @@ -38,6 +40,7 @@ export class WebSocketHandler { this[kEmitter] = new Emitter() this.callFrame = getCallFrame(new Error()) + this.__kind = 'WebSocketHandler' } public parse(args: { diff --git a/src/core/utils/internal/toRequestHandlersOnly.test.ts b/src/core/utils/internal/toRequestHandlersOnly.test.ts new file mode 100644 index 000000000..5bbfb638e --- /dev/null +++ b/src/core/utils/internal/toRequestHandlersOnly.test.ts @@ -0,0 +1,40 @@ +import { GraphQLHandler } from '../../handlers/GraphQLHandler' +import { HttpHandler } from '../../handlers/HttpHandler' +import { RequestHandler } from '../../handlers/RequestHandler' +import { WebSocketHandler } from '../../handlers/WebSocketHandler' +import { toRequestHandlersOnly } from './toRequestHandlersOnly' + +it('returns true for HttpHandler', () => { + expect(toRequestHandlersOnly(new HttpHandler('*', '*', () => {}))).toBe(true) +}) + +it('returns true for GraphQLHandler', () => { + expect( + toRequestHandlersOnly(new GraphQLHandler('all', '*', '*', () => {})), + ).toBe(true) +}) + +it('returns true for a custom RequestHandler', () => { + class MyHandler extends RequestHandler { + constructor() { + super({ info: { header: '*' }, resolver: () => {} }) + } + predicate = () => false + log() {} + } + + expect(toRequestHandlersOnly(new MyHandler())).toBe(true) +}) + +it('returns false for a WebSocketHandler', () => { + expect(toRequestHandlersOnly(new WebSocketHandler('*'))).toBe(false) +}) + +it('returns false for an arbitrary values', () => { + expect(toRequestHandlersOnly(undefined)).toBe(false) + expect(toRequestHandlersOnly(null)).toBe(false) + expect(toRequestHandlersOnly({})).toBe(false) + expect(toRequestHandlersOnly([])).toBe(false) + expect(toRequestHandlersOnly(123)).toBe(false) + expect(toRequestHandlersOnly('hello')).toBe(false) +}) diff --git a/src/core/utils/internal/filterRequestHandlers.ts b/src/core/utils/internal/toRequestHandlersOnly.ts similarity index 71% rename from src/core/utils/internal/filterRequestHandlers.ts rename to src/core/utils/internal/toRequestHandlersOnly.ts index 671516de1..7f003fe2b 100644 --- a/src/core/utils/internal/filterRequestHandlers.ts +++ b/src/core/utils/internal/toRequestHandlersOnly.ts @@ -6,5 +6,10 @@ import { RequestHandler } from '../../handlers/RequestHandler' * out other handlers, like `WebSocketHandler`. */ export function toRequestHandlersOnly(input: unknown): input is RequestHandler { - return input instanceof RequestHandler + return ( + input != null && + typeof input === 'object' && + '__kind' in input && + input.__kind === 'RequestHandler' + ) } diff --git a/src/node/SetupServerCommonApi.ts b/src/node/SetupServerCommonApi.ts index ccea4fdf9..8d50e5c27 100644 --- a/src/node/SetupServerCommonApi.ts +++ b/src/node/SetupServerCommonApi.ts @@ -20,7 +20,7 @@ import { InternalError, devUtils } from '~/core/utils/internal/devUtils' import type { SetupServerCommon } from './glossary' import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent' import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor' -import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers' +import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly' export const DEFAULT_LISTEN_OPTIONS: RequiredDeep = { onUnhandledRequest: 'warn',