|
| 1 | +import type { HttpsOptions } from 'firebase-functions/v2/https'; |
| 2 | +import { describe, expect, it, vitest } from 'vitest'; |
| 3 | +import { |
| 4 | + type FrameworkContract, |
| 5 | + ServerlessRequest, |
| 6 | + ServerlessResponse, |
| 7 | + waitForStreamComplete, |
| 8 | +} from '../../src'; |
| 9 | +import { HttpFirebaseV2Handler } from '../../src/handlers/firebase'; |
| 10 | +import { FrameworkMock } from '../mocks/framework.mock'; |
| 11 | + |
| 12 | +vitest.mock('firebase-functions/v2', async () => { |
| 13 | + // eslint-disable-next-line import/no-unresolved |
| 14 | + return await import('firebase-functions-v5/v2'); |
| 15 | +}); |
| 16 | + |
| 17 | +describe(HttpFirebaseV2Handler.name, () => { |
| 18 | + it('should forward correctly the request to framework', async () => { |
| 19 | + const handlerFactory = new HttpFirebaseV2Handler(); |
| 20 | + |
| 21 | + const method = 'POST'; |
| 22 | + const url = '/users/batata'; |
| 23 | + const headers = { 'Content-Type': 'application/json' }; |
| 24 | + const remoteAddress = '168.16.0.1'; |
| 25 | + const body = Buffer.from('{"test": true}', 'utf-8'); |
| 26 | + |
| 27 | + const request = new ServerlessRequest({ |
| 28 | + method, |
| 29 | + url, |
| 30 | + headers, |
| 31 | + remoteAddress, |
| 32 | + body, |
| 33 | + }); |
| 34 | + |
| 35 | + const response = new ServerlessResponse({ |
| 36 | + method, |
| 37 | + }); |
| 38 | + |
| 39 | + const responseBody = { batata: true }; |
| 40 | + const responseStatus = 200; |
| 41 | + const framework = new FrameworkMock(responseStatus, responseBody); |
| 42 | + |
| 43 | + const handler = handlerFactory.getHandler(null, framework); |
| 44 | + |
| 45 | + handler(request, response); |
| 46 | + |
| 47 | + await waitForStreamComplete(response); |
| 48 | + |
| 49 | + expect(response.statusCode).toBe(responseStatus); |
| 50 | + expect(ServerlessResponse.body(response).toString()).toStrictEqual( |
| 51 | + JSON.stringify(responseBody), |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should handle weird body types', () => { |
| 56 | + const handlerFactory = new HttpFirebaseV2Handler(); |
| 57 | + |
| 58 | + const method = 'POST'; |
| 59 | + const url = '/users/batata'; |
| 60 | + const headers = { 'Content-Type': 'application/json' }; |
| 61 | + const remoteAddress = '168.16.0.1'; |
| 62 | + const options = [{ potato: true }, [{ test: true }]]; |
| 63 | + |
| 64 | + for (const option of options) { |
| 65 | + const request = new ServerlessRequest({ |
| 66 | + method, |
| 67 | + url, |
| 68 | + headers, |
| 69 | + remoteAddress, |
| 70 | + body: option as any, |
| 71 | + }); |
| 72 | + |
| 73 | + const response = new ServerlessResponse({ |
| 74 | + method, |
| 75 | + }); |
| 76 | + |
| 77 | + const framework: FrameworkContract<unknown> = { |
| 78 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 79 | + sendRequest: vitest.fn( |
| 80 | + async ( |
| 81 | + _app: null, |
| 82 | + req: ServerlessRequest, |
| 83 | + res: ServerlessResponse, |
| 84 | + ) => { |
| 85 | + expect(req.body?.toString()).toEqual(JSON.stringify(option)); |
| 86 | + expect(req.headers['content-length']).toEqual( |
| 87 | + Buffer.byteLength(JSON.stringify(option)).toString(), |
| 88 | + ); |
| 89 | + |
| 90 | + req.pipe(res); |
| 91 | + |
| 92 | + await waitForStreamComplete(res); |
| 93 | + |
| 94 | + expect(ServerlessResponse.body(res).toString()).toEqual( |
| 95 | + JSON.stringify(option), |
| 96 | + ); |
| 97 | + }, |
| 98 | + ), |
| 99 | + }; |
| 100 | + |
| 101 | + const handler = handlerFactory.getHandler(null, framework); |
| 102 | + |
| 103 | + handler(request, response); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + it('should forward the properties to https.onRequest', () => { |
| 108 | + const options: HttpsOptions = { |
| 109 | + concurrency: 400, |
| 110 | + }; |
| 111 | + const factory = new HttpFirebaseV2Handler(options); |
| 112 | + |
| 113 | + const spyMethod = vitest.spyOn(factory, 'onRequestWithOptions' as any); |
| 114 | + |
| 115 | + factory.getHandler(null, new FrameworkMock(200, {})); |
| 116 | + |
| 117 | + expect(spyMethod).toHaveBeenCalledWith(options, expect.any(Function)); |
| 118 | + }); |
| 119 | +}); |
0 commit comments