Skip to content

Commit

Permalink
tests for composite handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Jan 4, 2025
1 parent 103efa6 commit 9897257
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
16 changes: 14 additions & 2 deletions packages/server/src/adapters/fetch/composite-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('compositeHandler', () => {
expect(mockHandler1.fetch).toHaveBeenCalledTimes(0)
expect(mockHandler2.fetch).toHaveBeenCalledTimes(0)
expect(response.status).toBe(404)
expect(await response.text()).toBe('None of the handlers can handle the request.')
expect(await response.text()).toBe('No handler found for the request.')
})

it('should handle an empty handlers array', async () => {
Expand All @@ -63,7 +63,7 @@ describe('compositeHandler', () => {
const response = await compositeHandler.fetch(request)

expect(response.status).toBe(404)
expect(await response.text()).toBe('None of the handlers can handle the request.')
expect(await response.text()).toBe('No handler found for the request.')
})

it('should handle multiple handlers, but only call the first matching handler', async () => {
Expand All @@ -86,4 +86,16 @@ describe('compositeHandler', () => {
expect(mockHandler2.fetch).toHaveBeenCalledTimes(0)
expect(await response.text()).toBe('Handler 1 response')
})

it('returnFalseOnNoMatch option', async () => {
const compositeHandler = new CompositeHandler([])

const request = new Request('https://example.com/unknown')

const r1 = await compositeHandler.fetch(request)
expect(r1).toBeInstanceOf(Response)

const r2 = await compositeHandler.fetch(request, { returnFalseOnNoMatch: true })
expect(r2).toBe(false)
})
})
18 changes: 16 additions & 2 deletions packages/server/src/adapters/node/composite-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CompositeHandler } from './composite-handler'
// Mock a basic handler implementation
function createMockHandler(
condition: (request: IncomingMessage) => boolean,
handle: (req: IncomingMessage, res: ServerResponse, options?: RequestOptions<any>) => Promise<void>,
handle: (req: IncomingMessage, res: ServerResponse, options?: RequestOptions<any, any>) => Promise<void>,
): ConditionalRequestHandler<any> {
return {
condition,
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('compositeHandler', () => {
expect(mockHandler1.handle).toHaveBeenCalledTimes(0)
expect(mockHandler2.handle).toHaveBeenCalledTimes(0)
expect(mockEnd).toHaveBeenCalledTimes(1)
expect(mockEnd).toHaveBeenCalledWith('None of the handlers can handle the request.')
expect(mockEnd).toHaveBeenCalledWith('No handler found for the request.')
})

it('should handle multiple handlers, but only call the first matching handler', async () => {
Expand Down Expand Up @@ -79,4 +79,18 @@ describe('compositeHandler', () => {
expect(mockEnd).toHaveBeenCalledTimes(1)
expect(mockEnd).toHaveBeenCalledWith('Handler 1 response')
})

it('returnFalseOnNoMatch option', async () => {
const mockEnd = vi.fn()

const result = await compositeHandler.handle({ url: 'handler6' } as any, { end: mockEnd } as any, { context: {} })
expect(result).toBe(undefined)
expect(mockEnd).toHaveBeenCalledTimes(1)
expect(mockEnd).toHaveBeenCalledWith('No handler found for the request.')

vi.clearAllMocks()
const result2 = await compositeHandler.handle({ url: 'handler6' } as any, { end: mockEnd } as any, { context: {}, returnFalseOnNoMatch: true })
expect(result2).toBe(false)
expect(mockEnd).toHaveBeenCalledTimes(0)
})
})

0 comments on commit 9897257

Please sign in to comment.