Skip to content

Commit 63c98f1

Browse files
authored
Merge pull request #296 from H4ad/feat/firebase-functions-v5-v6
feat(firebase): ensure support to v5 and v6 sdk
2 parents 0c31d92 + bc6886e commit 63c98f1

File tree

4 files changed

+334
-30
lines changed

4 files changed

+334
-30
lines changed

package-lock.json

Lines changed: 93 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"@deepkit/framework": "1.0.1-alpha.99",
8585
"@deepkit/http": "1.0.1-alpha.98",
8686
"@deepkit/workflow": "1.0.1-alpha.97",
87-
"@google-cloud/functions-framework": "3.3.0",
87+
"@google-cloud/functions-framework": "3.4.2",
8888
"@hapi/hapi": "21.3.2",
8989
"@microsoft/api-documenter": "7.23.15",
9090
"@microsoft/api-extractor": "7.39.0",
@@ -122,6 +122,8 @@
122122
"fastify-v5": "npm:fastify@5",
123123
"firebase-admin": "11.11.1",
124124
"firebase-functions": "4.5.0",
125+
"firebase-functions-v5": "npm:firebase-functions@5",
126+
"firebase-functions-v6": "npm:firebase-functions@6",
125127
"glob": "10.3.10",
126128
"husky": "8.0.3",
127129
"koa": "2.15.0",
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)