Skip to content

Commit f3fa19f

Browse files
committed
add test
1 parent adcf757 commit f3fa19f

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

test/integration/programmatic.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2021 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 sinon from 'sinon';
17+
import * as supertest from 'supertest';
18+
import {main} from '../../src/main';
19+
import * as server from '../../src/server';
20+
import {HttpFunction, CloudEventFunction} from '../../src/functions';
21+
import {Server} from 'http';
22+
23+
describe('programmatic functions', () => {
24+
let exitStub: sinon.SinonStub;
25+
let errorStub: sinon.SinonStub;
26+
let getServerStub: sinon.SinonStub;
27+
28+
beforeEach(() => {
29+
exitStub = sinon.stub(process, 'exit');
30+
errorStub = sinon.stub(console, 'error');
31+
});
32+
33+
afterEach(() => {
34+
exitStub.restore();
35+
errorStub.restore();
36+
if (getServerStub) {
37+
getServerStub.restore();
38+
}
39+
});
40+
41+
it('should run an HTTP function', async () => {
42+
const httpFunc: HttpFunction = (req, res) => {
43+
res.send('hello');
44+
};
45+
46+
let capturedServer: Server | null = null;
47+
let listenStub: sinon.SinonStub;
48+
const originalGetServer = server.getServer;
49+
getServerStub = sinon.stub(server, 'getServer').callsFake((fn, opts) => {
50+
const s = originalGetServer(fn, opts);
51+
capturedServer = s;
52+
listenStub = sinon.stub(s, 'listen').returns(s);
53+
return s;
54+
});
55+
56+
await main(httpFunc);
57+
58+
listenStub!.restore();
59+
60+
assert.ok(capturedServer);
61+
const st = supertest(capturedServer!);
62+
const response = await st.get('/');
63+
assert.strictEqual(response.status, 200);
64+
assert.strictEqual(response.text, 'hello');
65+
});
66+
67+
it('should run a CloudEvent function', async () => {
68+
let receivedEvent: any = null;
69+
const cloudEventFunc: CloudEventFunction = cloudEvent => {
70+
receivedEvent = cloudEvent;
71+
};
72+
73+
let capturedServer: Server | null = null;
74+
let listenStub: sinon.SinonStub;
75+
const originalGetServer = server.getServer;
76+
getServerStub = sinon.stub(server, 'getServer').callsFake((fn, opts) => {
77+
const s = originalGetServer(fn, opts);
78+
capturedServer = s;
79+
listenStub = sinon.stub(s, 'listen').returns(s);
80+
return s;
81+
});
82+
83+
const argv = process.argv;
84+
process.argv = ['node', 'index.js', '--signature-type=cloudevent'];
85+
await main(cloudEventFunc);
86+
process.argv = argv;
87+
88+
listenStub!.restore();
89+
90+
assert.ok(capturedServer);
91+
const st = supertest(capturedServer!);
92+
const event = {
93+
specversion: '1.0',
94+
type: 'com.google.cloud.storage',
95+
source: 'test',
96+
id: 'test',
97+
data: 'hello',
98+
};
99+
const response = await st
100+
.post('/')
101+
.send(event)
102+
.set('Content-Type', 'application/cloudevents+json');
103+
104+
assert.strictEqual(response.status, 204);
105+
assert.ok(receivedEvent);
106+
assert.strictEqual(receivedEvent.data, 'hello');
107+
});
108+
});

0 commit comments

Comments
 (0)