|
| 1 | +import { RPCHandler } from '../adapters/fetch' |
| 2 | +import { os } from '../builder' |
| 3 | +import { MethodOverrideHandlerPlugin } from './method-override' |
| 4 | + |
| 5 | +function getInterceptor(options?: ConstructorParameters<typeof MethodOverrideHandlerPlugin>[0]) { |
| 6 | + const existingInterceptor = vi.fn() |
| 7 | + |
| 8 | + const handlerOptions = new MethodOverrideHandlerPlugin<any>(options).init({ |
| 9 | + routingInterceptors: [existingInterceptor], |
| 10 | + } as any) |
| 11 | + |
| 12 | + return { |
| 13 | + interceptor: handlerOptions.routingInterceptors![0]!, |
| 14 | + existingInterceptor, |
| 15 | + routingInterceptors: handlerOptions.routingInterceptors!, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +async function invokeInterceptor( |
| 20 | + request: { method: string, url: string }, |
| 21 | + options?: ConstructorParameters<typeof MethodOverrideHandlerPlugin>[0], |
| 22 | +) { |
| 23 | + const nextResult = { matched: true, response: 'ok' } as any |
| 24 | + const next = vi.fn().mockResolvedValue(nextResult) |
| 25 | + const { interceptor } = getInterceptor(options) |
| 26 | + |
| 27 | + const result = await interceptor({ |
| 28 | + context: {}, |
| 29 | + request, |
| 30 | + next, |
| 31 | + } as any) |
| 32 | + |
| 33 | + return { result, next, nextResult } |
| 34 | +} |
| 35 | + |
| 36 | +describe('methodOverrideHandlerPlugin', () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks() |
| 39 | + }) |
| 40 | + |
| 41 | + it('prepends its routing interceptor before existing ones', () => { |
| 42 | + const { routingInterceptors, existingInterceptor } = getInterceptor() |
| 43 | + |
| 44 | + expect(routingInterceptors).toHaveLength(2) |
| 45 | + expect(routingInterceptors[0]).not.toBe(existingInterceptor) |
| 46 | + expect(routingInterceptors[1]).toBe(existingInterceptor) |
| 47 | + }) |
| 48 | + |
| 49 | + it('ignores non-POST requests', async () => { |
| 50 | + const { result, next, nextResult } = await invokeInterceptor({ method: 'GET', url: '/todos/1?method=DELETE' }) |
| 51 | + |
| 52 | + expect(next).toHaveBeenCalledExactlyOnceWith() |
| 53 | + expect(result).toBe(nextResult) |
| 54 | + }) |
| 55 | + |
| 56 | + it('ignores POST requests without the override param', async () => { |
| 57 | + const { result, next, nextResult } = await invokeInterceptor({ method: 'POST', url: '/todos/1?x=1' }) |
| 58 | + |
| 59 | + expect(next).toHaveBeenCalledExactlyOnceWith() |
| 60 | + expect(result).toBe(nextResult) |
| 61 | + }) |
| 62 | + |
| 63 | + it('overrides the method and strips the param', async () => { |
| 64 | + const { result, next, nextResult } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=DELETE' }) |
| 65 | + |
| 66 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 67 | + request: expect.objectContaining({ method: 'DELETE', url: '/todos/1' }), |
| 68 | + })) |
| 69 | + expect(result).toBe(nextResult) |
| 70 | + }) |
| 71 | + |
| 72 | + it('matches the override value case-insensitively', async () => { |
| 73 | + const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=delete' }) |
| 74 | + |
| 75 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 76 | + request: expect.objectContaining({ method: 'DELETE', url: '/todos/1' }), |
| 77 | + })) |
| 78 | + }) |
| 79 | + |
| 80 | + it('uses the last occurrence when the param is repeated and strips all of them', async () => { |
| 81 | + const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=PUT&method=DELETE' }) |
| 82 | + |
| 83 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 84 | + request: expect.objectContaining({ method: 'DELETE', url: '/todos/1' }), |
| 85 | + })) |
| 86 | + }) |
| 87 | + |
| 88 | + it('preserves other query params and the hash', async () => { |
| 89 | + const { next } = await invokeInterceptor({ method: 'POST', url: '/a?x=1&method=DELETE&y=2#h' }) |
| 90 | + |
| 91 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 92 | + request: expect.objectContaining({ method: 'DELETE', url: '/a?x=1&y=2#h' }), |
| 93 | + })) |
| 94 | + }) |
| 95 | + |
| 96 | + it.each(['FOO', 'GET', 'HEAD', 'POST'])('silently ignores an override value that is not allowed: %s', async (method) => { |
| 97 | + const { result, next, nextResult } = await invokeInterceptor({ method: 'POST', url: `/todos/1?method=${method}` }) |
| 98 | + |
| 99 | + expect(next).toHaveBeenCalledExactlyOnceWith() |
| 100 | + expect(result).toBe(nextResult) |
| 101 | + }) |
| 102 | + |
| 103 | + it('honors a custom param name', async () => { |
| 104 | + const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?_method=DELETE&method=PUT' }, { param: '_method' }) |
| 105 | + |
| 106 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 107 | + request: expect.objectContaining({ method: 'DELETE', url: '/todos/1?method=PUT' }), |
| 108 | + })) |
| 109 | + }) |
| 110 | + |
| 111 | + it('honors custom allowed methods', async () => { |
| 112 | + const { next } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=GET' }, { methods: ['get'] }) |
| 113 | + |
| 114 | + expect(next).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ |
| 115 | + request: expect.objectContaining({ method: 'GET', url: '/todos/1' }), |
| 116 | + })) |
| 117 | + |
| 118 | + const { next: next2 } = await invokeInterceptor({ method: 'POST', url: '/todos/1?method=DELETE' }, { methods: ['GET'] }) |
| 119 | + |
| 120 | + expect(next2).toHaveBeenCalledExactlyOnceWith() |
| 121 | + }) |
| 122 | + |
| 123 | + it('works with RPCHandler', async () => { |
| 124 | + const procedureHandler = vi.fn(() => 'pong') |
| 125 | + const handler = new RPCHandler( |
| 126 | + { |
| 127 | + ping: os.handler(procedureHandler), |
| 128 | + }, |
| 129 | + { |
| 130 | + plugins: [new MethodOverrideHandlerPlugin()], |
| 131 | + }, |
| 132 | + ) |
| 133 | + |
| 134 | + const { matched, response } = await handler.handle(new Request('https://example.com/ping?method=DELETE', { |
| 135 | + method: 'POST', |
| 136 | + headers: { |
| 137 | + 'content-type': 'application/json', |
| 138 | + }, |
| 139 | + body: JSON.stringify({ json: 'input' }), |
| 140 | + })) |
| 141 | + |
| 142 | + expect(matched).toBe(true) |
| 143 | + expect(response!.status).toBe(200) |
| 144 | + await expect(response!.text()).resolves.toContain('pong') |
| 145 | + expect(procedureHandler).toHaveBeenCalledWith(expect.any(Object), 'input') |
| 146 | + }) |
| 147 | +}) |
0 commit comments