Skip to content

Commit 1a262d2

Browse files
Split up invoker tests (#281)
1 parent 91a2642 commit 1a262d2

File tree

5 files changed

+334
-289
lines changed

5 files changed

+334
-289
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"on-finished": "^2.3.0"
1616
},
1717
"scripts": {
18-
"test": "mocha build/test",
18+
"test": "mocha build/test --recursive",
1919
"check": "gts check",
2020
"clean": "gts clean",
2121
"compile": "tsc -p .",

test/integration/cloudevent.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as assert from 'assert';
16+
import * as functions from '../../src/functions';
17+
import {getServer} from '../../src/server';
18+
import {SignatureType} from '../../src/types';
19+
import * as supertest from 'supertest';
20+
21+
const TEST_CLOUD_EVENT = {
22+
specversion: '1.0',
23+
type: 'com.google.cloud.storage',
24+
source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
25+
subject: 'test-subject',
26+
id: 'test-1234-1234',
27+
time: '2020-05-13T01:23:45Z',
28+
datacontenttype: 'application/json',
29+
data: {
30+
some: 'payload',
31+
},
32+
};
33+
34+
describe('CloudEvent Function', () => {
35+
const testData = [
36+
{
37+
name: 'CloudEvents v1.0 structured content request',
38+
headers: {'Content-Type': 'application/cloudevents+json'},
39+
body: TEST_CLOUD_EVENT,
40+
},
41+
{
42+
name: 'CloudEvents v1.0 binary content request',
43+
headers: {
44+
'Content-Type': 'application/json',
45+
'ce-specversion': TEST_CLOUD_EVENT.specversion,
46+
'ce-type': TEST_CLOUD_EVENT.type,
47+
'ce-source': TEST_CLOUD_EVENT.source,
48+
'ce-subject': TEST_CLOUD_EVENT.subject,
49+
'ce-id': TEST_CLOUD_EVENT.id,
50+
'ce-time': TEST_CLOUD_EVENT.time,
51+
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
52+
},
53+
body: TEST_CLOUD_EVENT.data,
54+
},
55+
];
56+
testData.forEach(test => {
57+
it(`should receive data and context from ${test.name}`, async () => {
58+
let receivedCloudEvent: functions.CloudEventsContext | null = null;
59+
const server = getServer((cloudevent: functions.CloudEventsContext) => {
60+
receivedCloudEvent = cloudevent as functions.CloudEventsContext;
61+
}, SignatureType.CLOUDEVENT);
62+
await supertest(server)
63+
.post('/')
64+
.set(test.headers)
65+
.send(test.body)
66+
.expect(204);
67+
assert.notStrictEqual(receivedCloudEvent, null);
68+
assert.strictEqual(
69+
receivedCloudEvent!.specversion,
70+
TEST_CLOUD_EVENT.specversion
71+
);
72+
assert.strictEqual(receivedCloudEvent!.type, TEST_CLOUD_EVENT.type);
73+
assert.strictEqual(receivedCloudEvent!.source, TEST_CLOUD_EVENT.source);
74+
assert.strictEqual(receivedCloudEvent!.subject, TEST_CLOUD_EVENT.subject);
75+
assert.strictEqual(receivedCloudEvent!.id, TEST_CLOUD_EVENT.id);
76+
assert.strictEqual(receivedCloudEvent!.time, TEST_CLOUD_EVENT.time);
77+
assert.strictEqual(
78+
receivedCloudEvent!.datacontenttype,
79+
TEST_CLOUD_EVENT.datacontenttype
80+
);
81+
assert.deepStrictEqual(receivedCloudEvent!.data, TEST_CLOUD_EVENT.data);
82+
});
83+
});
84+
});

test/integration/http.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as assert from 'assert';
16+
import * as express from 'express';
17+
import {getServer} from '../../src/server';
18+
import {SignatureType} from '../../src/types';
19+
import * as supertest from 'supertest';
20+
21+
describe('HTTP Function', () => {
22+
const testData = [
23+
{
24+
name: 'POST to empty path',
25+
httpVerb: 'POST',
26+
path: '/',
27+
expectedBody: {result: 'hello'},
28+
expectedStatus: 200,
29+
expectedCallCount: 1,
30+
},
31+
{
32+
name: 'POST to empty path',
33+
httpVerb: 'POST',
34+
path: '/foo',
35+
expectedBody: {result: 'hello'},
36+
expectedStatus: 200,
37+
expectedCallCount: 1,
38+
},
39+
{
40+
name: 'GET with query params',
41+
httpVerb: 'GET',
42+
path: '/foo?param=val',
43+
expectedBody: {query: 'val'},
44+
expectedStatus: 200,
45+
expectedCallCount: 1,
46+
},
47+
{
48+
name: 'GET favicon.ico',
49+
httpVerb: 'GET',
50+
path: '/favicon.ico',
51+
expectedBody: '',
52+
expectedStatus: 404,
53+
expectedCallCount: 0,
54+
},
55+
{
56+
name: 'with robots.txt',
57+
httpVerb: 'GET',
58+
path: '/robots.txt',
59+
expectedBody: '',
60+
expectedStatus: 404,
61+
expectedCallCount: 0,
62+
},
63+
];
64+
65+
testData.forEach(test => {
66+
it(test.name, async () => {
67+
let callCount = 0;
68+
const server = getServer(
69+
(req: express.Request, res: express.Response) => {
70+
++callCount;
71+
res.send({
72+
result: req.body.text,
73+
query: req.query.param,
74+
});
75+
},
76+
SignatureType.HTTP
77+
);
78+
const st = supertest(server);
79+
await (test.httpVerb === 'GET'
80+
? st.get(test.path)
81+
: st.post(test.path).send({text: 'hello'})
82+
)
83+
.set('Content-Type', 'application/json')
84+
.expect(test.expectedBody)
85+
.expect(test.expectedStatus);
86+
assert.strictEqual(callCount, test.expectedCallCount);
87+
});
88+
});
89+
});

test/integration/legacy_event.ts

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import * as assert from 'assert';
16+
import * as functions from '../../src/functions';
17+
import {getServer} from '../../src/server';
18+
import {SignatureType} from '../../src/types';
19+
import * as supertest from 'supertest';
20+
21+
const TEST_CLOUD_EVENT = {
22+
specversion: '1.0',
23+
type: 'com.google.cloud.storage',
24+
source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
25+
subject: 'test-subject',
26+
id: 'test-1234-1234',
27+
time: '2020-05-13T01:23:45Z',
28+
datacontenttype: 'application/json',
29+
data: {
30+
some: 'payload',
31+
},
32+
};
33+
34+
describe('Event Function', () => {
35+
const testData = [
36+
{
37+
name: 'GCF event',
38+
headers: {},
39+
body: {
40+
context: {
41+
eventId: 'testEventId',
42+
timestamp: 'testTimestamp',
43+
eventType: 'testEventType',
44+
resource: 'testResource',
45+
},
46+
data: {some: 'payload'},
47+
},
48+
expectedResource: {
49+
eventId: 'testEventId',
50+
eventType: 'testEventType',
51+
resource: 'testResource',
52+
timestamp: 'testTimestamp',
53+
},
54+
},
55+
{
56+
name: 'GCF legacy event',
57+
headers: {},
58+
body: {
59+
eventId: 'testEventId',
60+
timestamp: 'testTimestamp',
61+
eventType: 'testEventType',
62+
resource: 'testResource',
63+
data: {some: 'payload'},
64+
},
65+
expectedResource: {
66+
eventId: 'testEventId',
67+
eventType: 'testEventType',
68+
resource: 'testResource',
69+
timestamp: 'testTimestamp',
70+
},
71+
},
72+
{
73+
name: 'GCF event with resource JSON',
74+
headers: {},
75+
body: {
76+
context: {
77+
eventId: 'testEventId',
78+
timestamp: 'testTimestamp',
79+
eventType: 'testEventType',
80+
resource: {
81+
service: 'testService',
82+
name: 'testName',
83+
type: 'testType',
84+
},
85+
},
86+
data: {some: 'payload'},
87+
},
88+
expectedResource: {
89+
eventId: 'testEventId',
90+
eventType: 'testEventType',
91+
resource: {
92+
name: 'testName',
93+
service: 'testService',
94+
type: 'testType',
95+
},
96+
timestamp: 'testTimestamp',
97+
},
98+
},
99+
{
100+
name: 'CloudEvents v1.0 structured content request',
101+
headers: {'Content-Type': 'application/cloudevents+json'},
102+
body: TEST_CLOUD_EVENT,
103+
expectedResource: {
104+
datacontenttype: 'application/json',
105+
id: 'test-1234-1234',
106+
source:
107+
'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
108+
specversion: '1.0',
109+
subject: 'test-subject',
110+
time: '2020-05-13T01:23:45Z',
111+
type: 'com.google.cloud.storage',
112+
},
113+
},
114+
{
115+
name: 'CloudEvents v1.0 binary content request',
116+
headers: {
117+
'Content-Type': 'application/cloudevents+json',
118+
'ce-specversion': TEST_CLOUD_EVENT.specversion,
119+
'ce-type': TEST_CLOUD_EVENT.type,
120+
'ce-source': TEST_CLOUD_EVENT.source,
121+
'ce-subject': TEST_CLOUD_EVENT.subject,
122+
'ce-id': TEST_CLOUD_EVENT.id,
123+
'ce-time': TEST_CLOUD_EVENT.time,
124+
'ce-datacontenttype': TEST_CLOUD_EVENT.datacontenttype,
125+
},
126+
body: TEST_CLOUD_EVENT.data,
127+
expectedResource: {
128+
datacontenttype: 'application/json',
129+
id: 'test-1234-1234',
130+
source:
131+
'https://github.com/GoogleCloudPlatform/functions-framework-nodejs',
132+
specversion: '1.0',
133+
subject: 'test-subject',
134+
time: '2020-05-13T01:23:45Z',
135+
type: 'com.google.cloud.storage',
136+
},
137+
},
138+
];
139+
testData.forEach(test => {
140+
it(test.name, async () => {
141+
let receivedData: {} | null = null;
142+
let receivedContext: functions.CloudFunctionsContext | null = null;
143+
const server = getServer((data: {}, context: functions.Context) => {
144+
receivedData = data;
145+
receivedContext = context as functions.CloudFunctionsContext;
146+
}, SignatureType.EVENT);
147+
const requestHeaders = {
148+
'Content-Type': 'application/json',
149+
...test.headers,
150+
};
151+
await supertest(server)
152+
.post('/')
153+
.send(test.body)
154+
.set(requestHeaders)
155+
.expect(204);
156+
assert.deepStrictEqual(receivedData, {some: 'payload'});
157+
assert.deepStrictEqual(receivedContext, test.expectedResource);
158+
});
159+
});
160+
});

0 commit comments

Comments
 (0)