forked from aws/aws-lambda-nodejs-runtime-interface-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAPIDClientTest.js
More file actions
213 lines (186 loc) · 6.18 KB
/
RAPIDClientTest.js
File metadata and controls
213 lines (186 loc) · 6.18 KB
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
/**
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*/
'use strict';
require('should');
let RAPIDClient = require('lambda-runtime/RAPIDClient.js');
let runtimeErrors = require('lambda-runtime/Errors.js');
/**
* Stub request object.
* Provides no-op definitions of the request functions used by the rapid client.
*/
const noOpRequest = Object.freeze({
/* no op, return itself to allow continuations/chaining */
on: () => noOpRequest,
/* no op, return itself to allow continuations/chaining */
end: () => noOpRequest,
});
class StubHttp {
constructor() {
this.lastUsedOptions = {};
this.Agent = class FakeAgent {};
}
request(options, _callback) {
this.lastUsedOptions = options;
return noOpRequest;
}
}
class NoOpNativeHttp {
constructor() {
this.lastRequestId = '';
this.lastErrorRequestId = '';
}
done(requestId) {
this.lastRequestId = requestId;
}
error(requestId) {
this.lastErrorRequestId = requestId;
}
}
class MockNativeClient {
constructor(response) {
this.response = response;
this.called = false;
this.shouldThrowError = false;
}
next() {
this.called = true;
if (this.shouldThrowError) {
return Promise.reject(new Error('Failed to get next invocation'));
} else {
return Promise.resolve(this.response);
}
}
}
class EvilError extends Error {
get name() {
throw 'gotcha';
}
}
const EXPECTED_ERROR_HEADER = 'Lambda-Runtime-Function-Error-Type';
describe('building error requests with the RAPIDClient', () => {
let stubHttp = new StubHttp();
let client = new RAPIDClient('notUsed:1337', stubHttp, new NoOpNativeHttp());
let errors = [
[new Error('generic failure'), 'Error'],
[new runtimeErrors.ImportModuleError(), 'Runtime.ImportModuleError'],
[new runtimeErrors.HandlerNotFound(), 'Runtime.HandlerNotFound'],
[new runtimeErrors.MalformedHandlerName(), 'Runtime.MalformedHandlerName'],
[new runtimeErrors.UserCodeSyntaxError(), 'Runtime.UserCodeSyntaxError'],
[{ data: 'some random object' }, 'object'],
[new EvilError(), 'handled'],
];
describe('the error header in postInitError', () => {
errors.forEach(([error, name]) => {
it(`should be ${name} for ${error.constructor.name}`, () => {
client.postInitError(error);
stubHttp.lastUsedOptions.should.have
.property('headers')
.have.property(EXPECTED_ERROR_HEADER, name);
});
});
});
});
describe('invalid request id works', () => {
const nativeClient = new NoOpNativeHttp();
const client = new RAPIDClient('notUsed:1337', undefined, nativeClient);
[
// Encoding expected:
['#', '%23'],
['%', '%25'],
['/', '%2F'],
['?', '%3F'],
['\x7F', '%7F'],
["<script>alert('1')</script>", "%3Cscript%3Ealert('1')%3C%2Fscript%3E"],
['⚡', '%E2%9A%A1'],
// No encoding:
['.', '.'],
['..', '..'],
['a', 'a'],
[
'59b22c65-fa81-47fb-a6dc-23028a63566f',
'59b22c65-fa81-47fb-a6dc-23028a63566f',
],
].forEach(([requestId, expected]) => {
it(`postInvocationResponse should encode requestId: '${requestId}'`, () => {
client.postInvocationResponse({}, requestId, () => {});
nativeClient.lastRequestId.should.be.equal(expected);
});
it(`postInvocationError should encode requestId: '${requestId}'`, () => {
client.postInvocationError(new Error(), requestId, () => {});
nativeClient.lastErrorRequestId.should.be.equal(expected);
});
});
});
describe('next invocation with native client works', () => {
it('should call the native client next() method', async () => {
const mockNative = new MockNativeClient({
bodyJson: '',
headers: {
'lambda-runtime-aws-request-id': 'test-request-id',
},
});
const client = new RAPIDClient('notUsed:1337', undefined, mockNative);
client.useAlternativeClient = false;
await client.nextInvocation();
// verify native client was called
mockNative.called.should.be.true();
});
it('should parse all required headers', async () => {
const mockResponse = {
bodyJson: '{"message":"Hello from Lambda!"}',
headers: {
'lambda-runtime-aws-request-id': 'test-request-id',
'lambda-runtime-deadline-ms': 1619712000000,
'lambda-runtime-trace-id': 'test-trace-id',
'lambda-runtime-invoked-function-arn': 'test-function-arn',
'lambda-runtime-client-context': '{"client":{"app_title":"MyApp"}}',
'lambda-runtime-cognito-identity':
'{"identityId":"id123","identityPoolId":"pool123"}',
'lambda-runtime-aws-tenant-id': 'test-tenant-id',
},
};
const mockNative = new MockNativeClient(mockResponse);
const client = new RAPIDClient('notUsed:1337', undefined, mockNative);
client.useAlternativeClient = false;
const response = await client.nextInvocation();
// Verify all headers are present
response.headers.should.have.property(
'lambda-runtime-aws-request-id',
'test-request-id',
);
response.headers.should.have.property(
'lambda-runtime-deadline-ms',
1619712000000,
);
response.headers.should.have.property(
'lambda-runtime-trace-id',
'test-trace-id',
);
response.headers.should.have.property(
'lambda-runtime-invoked-function-arn',
'test-function-arn',
);
response.headers.should.have.property(
'lambda-runtime-client-context',
'{"client":{"app_title":"MyApp"}}',
);
response.headers.should.have.property(
'lambda-runtime-cognito-identity',
'{"identityId":"id123","identityPoolId":"pool123"}',
);
response.headers.should.have.property(
'lambda-runtime-aws-tenant-id',
'test-tenant-id',
);
// Verify body is correctly passed through
response.bodyJson.should.equal('{"message":"Hello from Lambda!"}');
});
it('should handle native client errors', async () => {
const nativeClient = new MockNativeClient({});
nativeClient.shouldThrowError = true;
const client = new RAPIDClient('localhost:8080', null, nativeClient);
client.useAlternativeClient = false;
await client.nextInvocation().should.be.rejectedWith('Failed to get next invocation');
});
});