|
| 1 | +import { RPCHandler } from '../adapters/fetch' |
| 2 | +import { os } from '../builder' |
| 3 | +import { GetMethodGuardPlugin } from './get-method-guard' |
| 4 | + |
| 5 | +describe('getMethodGuardPlugin', () => { |
| 6 | + const interceptor = vi.fn(({ next }) => next()) |
| 7 | + |
| 8 | + const handler = new RPCHandler({ |
| 9 | + ping: os.handler(() => 'pong'), |
| 10 | + pong: os.route({ method: 'GET' }).handler(() => 'pong'), |
| 11 | + }, { |
| 12 | + plugins: [ |
| 13 | + new GetMethodGuardPlugin(), |
| 14 | + ], |
| 15 | + rootInterceptors: [interceptor], |
| 16 | + }) |
| 17 | + |
| 18 | + it('not allow use GET method for procedure not explicitly marked as GET', async () => { |
| 19 | + await expect( |
| 20 | + handler.handle(new Request('http://localhost/ping?data=%7B%7D')), |
| 21 | + ).resolves.toEqual({ matched: true, response: expect.toSatisfy(response => response.status === 405) }) |
| 22 | + |
| 23 | + await expect( |
| 24 | + handler.handle(new Request('http://localhost/ping', { |
| 25 | + method: 'POST', |
| 26 | + body: JSON.stringify({ }), |
| 27 | + headers: { |
| 28 | + 'Content-Type': 'application/json', |
| 29 | + }, |
| 30 | + })), |
| 31 | + ).resolves.toEqual({ matched: true, response: expect.toSatisfy(response => response.status === 200) }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('allow use GET method for procedure explicitly marked as GET', async () => { |
| 35 | + await expect( |
| 36 | + handler.handle(new Request('http://localhost/pong?data=%7B%7D')), |
| 37 | + ).resolves.toEqual({ matched: true, response: expect.toSatisfy(response => response.status === 200) }) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should throw error when interceptor messes with the context', async () => { |
| 41 | + interceptor.mockImplementation((options) => { |
| 42 | + return options.next({ |
| 43 | + ...options, |
| 44 | + context: {}, // <-- interceptor messes with the context |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + await expect( |
| 49 | + handler.handle(new Request('http://localhost/ping', { |
| 50 | + method: 'POST', |
| 51 | + body: JSON.stringify({}), |
| 52 | + headers: { |
| 53 | + 'Content-Type': 'application/json', |
| 54 | + }, |
| 55 | + })), |
| 56 | + ).resolves.toEqual({ matched: true, response: expect.toSatisfy(response => response.status === 500) }) |
| 57 | + }) |
| 58 | +}) |
0 commit comments