forked from GoogleCloudPlatform/functions-framework-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunction_wrappers.ts
108 lines (100 loc) · 3.38 KB
/
function_wrappers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import * as assert from 'assert';
import * as sinon from 'sinon';
import {Request, Response} from 'express';
import {Context, CloudEvent} from '../src/functions';
import {wrapUserFunction} from '../src/function_wrappers';
describe('wrapUserFunction', () => {
const CLOUD_EVENT = {
specversion: '1.0',
type: 'foobar',
source: '//somewhere/',
id: 'aaaaaa-1111-bbbb-2222-cccccccccccc',
time: '1970-01-01T00:00:00.000Z',
datacontenttype: 'application/json',
data: {
hello: 'world',
},
};
const createRequest = (body: object) =>
({
body,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
header: (_: string) => '',
} as Request);
const createResponse = () =>
({
locals: {
functionExecutionFinished: false,
},
sendStatus: sinon.spy(),
} as unknown as Response);
it('correctly wraps an http function', done => {
const request = createRequest({foo: 'bar'});
const response = createResponse();
const func = wrapUserFunction((req: Request, res: Response) => {
assert.deepStrictEqual(req, request);
assert.deepStrictEqual(res, response);
done();
}, 'http');
func(request, response, () => {});
});
it('correctly wraps an async background function', done => {
const request = createRequest({context: 'context', data: 'data'});
const response = createResponse();
const func = wrapUserFunction(async (data: {}, context: Context) => {
assert.deepStrictEqual(data, 'data');
assert.deepStrictEqual(context, 'context');
// await to make sure wrapper handles async code
await new Promise(resolve => setTimeout(resolve, 20));
done();
}, 'event');
func(request, response, () => {});
});
it('correctly wraps a background function with callback', done => {
const request = createRequest({context: 'context', data: 'data'});
const response = createResponse();
const func = wrapUserFunction(
(data: {}, context: Context, callback: Function) => {
// timeout to make sure wrapper waits for callback
setTimeout(() => {
assert.deepStrictEqual(data, 'data');
assert.deepStrictEqual(context, 'context');
callback();
done();
}, 20);
},
'event'
);
func(request, response, () => {});
});
it('correctly wraps an async CloudEvent function', done => {
const request = createRequest(CLOUD_EVENT);
const response = createResponse();
const func = wrapUserFunction(
async (cloudEvent: CloudEvent<typeof CLOUD_EVENT.data>) => {
assert.deepStrictEqual(cloudEvent, CLOUD_EVENT);
// await to make sure wrapper handles async code
await new Promise(resolve => setTimeout(resolve, 20));
done();
},
'cloudevent'
);
func(request, response, () => {});
});
it('correctly wraps a CloudEvent function with callback', done => {
const request = createRequest(CLOUD_EVENT);
const response = createResponse();
const func = wrapUserFunction(
(cloudEvent: CloudEvent<typeof CLOUD_EVENT.data>, callback: Function) => {
// timeout to make sure wrapper waits for callback
setTimeout(() => {
assert.deepStrictEqual(cloudEvent, CLOUD_EVENT);
callback();
done();
}, 20);
},
'cloudevent'
);
func(request, response, () => {});
});
});