forked from alibaba/funcraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-invoke.test.js
233 lines (171 loc) · 7.03 KB
/
http-invoke.test.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
'use strict';
const HttpInvoke = require('../../lib/local/http-invoke');
const sinon = require('sinon');
const assert = sinon.assert;
var express = require('express');
const path = require('path');
const mkdirp = require('mkdirp-promise');
const { hasDocker } = require('../conditions');
const tempDir = require('temp-dir');
const rimraf = require('rimraf');
const fs = require('fs');
const expect = require('expect.js');
const httpx = require('httpx');
const FC = require('@alicloud/fc2');
const { serviceName, serviceRes, functionName, functionRes,
httptriggerServiceRes, httpTriggerFunctionRes
} = require('./mock-data');
const { setProcess } = require('../test-utils');
const httpOutputStream = `"FC Invoke Start RequestId: 65ca478d-b3cf-41d5-b668-9b89a4d481d8
load code for handler:read.handler
--------------------response begin-----------------
${Buffer.from('HTTP/1.1 200 OK\r\n'
+ 'x-fc-http-params: eyJzdGF0dXMiOjIwMCwiaGVhZGVycyI6eyJjb250ZW50LXR5cGUiOiJhcHBsaWNhdGlvbi9qc29uIn0sImhlYWRlcnNNYXAiOnsiY29udGVudC10eXBlIjpbImFwcGxpY2F0aW9uL2pzb24iXX19'
+ '\r\n\r\ntestBody').toString('base64')}
--------------------response end-----------------
--------------------execution info begin-----------------
OWM4MWI1M2UtZWQxNy00MzI3LWFjNzctMjhkYWMzNzRlMDU1CjE4MgoxOTk4CjIwCg==
--------------------execution info end-----------------
[0;32mRequestId: 65ca478d-b3cf-41d5-b668-9b89a4d481d8 Billed Duration: 44 ms Memory Size: 1998 MB Max Memory Used: 19 MB[0m
`;
const httpErrorOutputStream = `--------------------response begin-----------------
SFRUUC8xLjEgNDAwIE9LDQoNCnRlc3RCb2R5
--------------------response end-----------------`;
describe('test http response', async () => {
const httpInvoke = new HttpInvoke(serviceName, serviceRes, functionName, functionRes);
it('test response success http trigger', async () => {
const resp = {
send: sinon.stub(),
status: sinon.stub(),
setHeader: sinon.stub()
};
httpInvoke.response(httpOutputStream, '', resp);
console.log(Buffer.from('HTTP/1.1 400 OK\r\n\r\ntestBody').toString('base64'));
assert.calledWith(resp.status, 200);
assert.calledWith(resp.setHeader, 'content-type', ['application/json']);
assert.calledWith(resp.send, Buffer.from('testBody'));
});
it('test response with 4xx invoke http status', async () => {
const resp = {
send: sinon.stub(),
status: sinon.stub(),
setHeader: sinon.stub()
};
httpInvoke.response(httpErrorOutputStream, '', resp);
assert.calledWith(resp.status, '400');
assert.calledWith(resp.send, Buffer.from('testBody'));
});
it('test response error http trigger', async () => {
const resp = {
send: sinon.stub(),
status: sinon.stub(),
setHeader: sinon.stub()
};
httpInvoke.response(httpErrorOutputStream, 'function invoke error', resp);
assert.calledWith(resp.status, '400');
assert.calledWith(resp.send, Buffer.from('testBody'));
});
});
(hasDocker ? describe : describe.skip)('Integration::http-invoke', () => {
const projectDir = path.join(tempDir, 'http-invoke-it-dir');
const ymlPath = path.join(projectDir, 'template.yml');
const index = path.join(projectDir, 'index.py');
const serverPort = 8990;
const accountId = 'testAccountId';
const accessKeyId = 'testKeyId';
const accessKeySecret = 'testKeySecret';
let app;
let server;
let restoreProcess;
let httpInvoke;
beforeEach(async () => {
await mkdirp(projectDir);
app = express();
restoreProcess = setProcess({
ACCOUNT_ID: accountId,
ACCESS_KEY_ID: accessKeyId,
ACCESS_KEY_SECRET: accessKeySecret
}, projectDir);
console.log('tempDir: %s', projectDir);
fs.writeFileSync(index, `
def handler(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [b"Hello world!\\n"]
`);
});
afterEach(async () => {
rimraf.sync(projectDir);
restoreProcess();
server.close();
});
it('test http local invoke with authType anonymous', async () => {
server = app.listen(serverPort, () => {
console.log(`anonymous http server start on port ${serverPort}!`);
console.log();
});
const endpointPrefix = `/2016-08-15/proxy/${serviceName}/${functionName}`;
const endpoint = `${endpointPrefix}*`;
httpInvoke = new HttpInvoke(serviceName, httptriggerServiceRes,
functionName, httpTriggerFunctionRes, null, null, ymlPath, 'ANONYMOUS', endpointPrefix);
app.get(endpoint, async (req, res) => {
await httpInvoke.invoke(req, res);
});
const resp = await httpx.request(`http://localhost:${serverPort}${endpointPrefix}`, {
method: 'GET',
timeout: '3000'
});
const body = await httpx.read(resp, 'utf8');
expect(body).to.contain('Hello world!');
await httpInvoke.runner.stop();
});
it('test http local invoke with authType function with invalid signature', async () => {
const functionServerPort = serverPort + 1;
server = app.listen(functionServerPort, () => {
console.log(`function http server start on port ${functionServerPort}!`);
console.log();
});
const endpointPrefix = `/2016-08-15/proxy/${serviceName}/${functionName}`;
const endpoint = `${endpointPrefix}*`;
httpInvoke = new HttpInvoke(serviceName, httptriggerServiceRes,
functionName, httpTriggerFunctionRes, null, null, ymlPath, 'FUNCTION', endpointPrefix);
app.get(endpoint, async (req, res) => {
await httpInvoke.invoke(req, res);
});
const resp = await httpx.request(`http://localhost:${functionServerPort}${endpointPrefix}`, {
method: 'GET',
timeout: '3000'
});
const body = await httpx.read(resp, 'utf8');
expect(body).to.contain('Signature doesn\'t match, request signature is');
await httpInvoke.runner.stop();
});
it('test http local invoke with authType function with valid signature', async () => {
const functionServerPort = serverPort + 2;
server = app.listen(functionServerPort, () => {
console.log(`function http server start on port ${functionServerPort}!`);
console.log();
});
const endpointPrefix = `/2016-08-15/proxy/${serviceName}/${functionName}`;
const endpoint = `${endpointPrefix}*`;
const httpInvoke = new HttpInvoke(serviceName, httptriggerServiceRes,
functionName, httpTriggerFunctionRes, null, null, ymlPath, 'FUNCTION', endpointPrefix);
app.get(endpoint, async (req, res) => {
await httpInvoke.invoke(req, res);
});
const headers = {
'content-type': 'text/plain'
};
const signature = FC.getSignature(accessKeyId, accessKeySecret, 'GET', endpointPrefix, headers);
headers['authorization'] = signature;
const resp = await httpx.request(`http://localhost:${functionServerPort}${endpointPrefix}`, {
method: 'GET',
timeout: '3000',
headers
});
const body = await httpx.read(resp, 'utf8');
expect(body).to.contain('Hello world!');
await httpInvoke.runner.stop();
});
});