@salesforce/pwa-kit-runtime 3.8.0-preview.0-basepath → 3.8.0-preview.2-basepath
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -5
- package/ssr/server/build-remote-server.js +1159 -0
- package/ssr/server/build-remote-server.test.js +41 -0
- package/ssr/server/constants.js +37 -0
- package/ssr/server/express.js +462 -0
- package/ssr/server/express.lambda.test.js +390 -0
- package/ssr/server/express.test.js +963 -0
- package/ssr/server/test_fixtures/favicon.ico +0 -0
- package/ssr/server/test_fixtures/loadable-stats.json +1 -0
- package/ssr/server/test_fixtures/localhost.pem +45 -0
- package/ssr/server/test_fixtures/main.js +7 -0
- package/ssr/server/test_fixtures/mobify.png +0 -0
- package/ssr/server/test_fixtures/server-renderer.js +12 -0
- package/ssr/server/test_fixtures/worker.js +7 -0
- package/ssr/server/test_fixtures/worker.js.map +1 -0
- package/utils/logger-factory.js +154 -0
- package/utils/logger-factory.test.js +71 -0
- package/utils/logger-instance.js +19 -0
- package/utils/middleware/index.js +16 -0
- package/utils/middleware/security.js +111 -0
- package/utils/middleware/security.test.js +110 -0
- package/utils/morgan-stream.js +28 -0
- package/utils/ssr-cache.js +177 -0
- package/utils/ssr-cache.test.js +64 -0
- package/utils/ssr-config.client.js +23 -0
- package/utils/ssr-config.client.test.js +25 -0
- package/utils/ssr-config.js +20 -0
- package/utils/ssr-config.server.js +98 -0
- package/utils/ssr-config.server.test.js +50 -0
- package/utils/ssr-namespace-paths.js +60 -0
- package/utils/ssr-namespace-paths.test.js +30 -0
- package/utils/ssr-paths.js +51 -0
- package/utils/ssr-paths.test.js +49 -0
- package/utils/ssr-proxying.js +859 -0
- package/utils/ssr-proxying.test.js +593 -0
- package/utils/ssr-request-processing.js +164 -0
- package/utils/ssr-request-processing.test.js +95 -0
- package/utils/ssr-server/cached-response.js +116 -0
- package/utils/ssr-server/configure-proxy.js +304 -0
- package/utils/ssr-server/metrics-sender.js +204 -0
- package/utils/ssr-server/outgoing-request-hook.js +139 -0
- package/utils/ssr-server/parse-end-parameters.js +38 -0
- package/utils/ssr-server/process-express-response.js +56 -0
- package/utils/ssr-server/process-lambda-response.js +43 -0
- package/utils/ssr-server/update-global-agent-options.js +41 -0
- package/utils/ssr-server/update-global-agent-options.test.js +28 -0
- package/utils/ssr-server/utils.js +124 -0
- package/utils/ssr-server/utils.test.js +69 -0
- package/utils/ssr-server/wrap-response-write.js +40 -0
- package/utils/ssr-server.js +115 -0
- package/utils/ssr-server.test.js +869 -0
- package/utils/ssr-shared.js +183 -0
|
@@ -0,0 +1,869 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _sinon = _interopRequireDefault(require("sinon"));
|
|
4
|
+
var _ssrServer = require("./ssr-server");
|
|
5
|
+
var _ssrShared = require("./ssr-shared");
|
|
6
|
+
var _constants = require("../ssr/server/constants");
|
|
7
|
+
var _ssrPaths = require("./ssr-paths");
|
|
8
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
10
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
11
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
12
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
13
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
14
|
+
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } /*
|
|
15
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
16
|
+
* All rights reserved.
|
|
17
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
18
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
19
|
+
*/ // TODO: The methods tested in this file have been split from one file into
|
|
20
|
+
// multiple, so the tests should be split into multiple files as well.
|
|
21
|
+
// TODO: There are a lot batched tests with conditional assertions. Can they be
|
|
22
|
+
// split into smaller batches or otherwise refactored to avoid the conditionals?
|
|
23
|
+
/* eslint-disable jest/no-conditional-expect */
|
|
24
|
+
const baseMobify = {
|
|
25
|
+
ssrEnabled: true,
|
|
26
|
+
ssrOnly: ['main.js.map', 'ssr.js', 'ssr.js.map', 'vendor.js.map'],
|
|
27
|
+
ssrShared: ['main.{js,css}', '!**/no-op.*', 'ssr-loader.js', 'vendor.js*', 'worker.js'],
|
|
28
|
+
ssrParameters: {
|
|
29
|
+
proxyConfigs: [{
|
|
30
|
+
protocol: 'https',
|
|
31
|
+
host: 'www.merlinspotions.com',
|
|
32
|
+
path: 'base'
|
|
33
|
+
}, {
|
|
34
|
+
protocol: 'https',
|
|
35
|
+
host: 'api.merlinspotions.com',
|
|
36
|
+
path: 'base2',
|
|
37
|
+
caching: true
|
|
38
|
+
}]
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
jest.mock('./ssr-config', () => {
|
|
42
|
+
return {
|
|
43
|
+
getConfig: () => baseMobify
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
let consoleLog;
|
|
47
|
+
let consoleWarn;
|
|
48
|
+
let consoleError;
|
|
49
|
+
const sandbox = _sinon.default.createSandbox();
|
|
50
|
+
const originalConsoleLog = console.log;
|
|
51
|
+
const originalConsoleWarn = console.warn;
|
|
52
|
+
const originalConsoleError = console.error;
|
|
53
|
+
beforeAll(() => console.log(`
|
|
54
|
+
NOTICE: Console log, warn and error have been suppressed in these tests!
|
|
55
|
+
|
|
56
|
+
They are suppressed because the tests themselves produce large amounts
|
|
57
|
+
of logs. You may un-suppress console log, warn and error by commenting
|
|
58
|
+
out their suppression in the utils/ssr-server.test.js
|
|
59
|
+
`));
|
|
60
|
+
|
|
61
|
+
// You can comment out the `beforeEach()` and `afterAll()` below to
|
|
62
|
+
// unsuppress console logs, warnings and errors
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
// Suppress test console logs
|
|
65
|
+
consoleLog = sandbox.spy();
|
|
66
|
+
console.log = consoleLog;
|
|
67
|
+
|
|
68
|
+
// Suppress test console warns
|
|
69
|
+
consoleWarn = sandbox.spy();
|
|
70
|
+
console.warn = consoleWarn;
|
|
71
|
+
|
|
72
|
+
// Suppress test console errors
|
|
73
|
+
consoleError = sandbox.spy();
|
|
74
|
+
console.error = consoleError;
|
|
75
|
+
});
|
|
76
|
+
afterAll(() => {
|
|
77
|
+
console.log = originalConsoleLog;
|
|
78
|
+
console.warn = originalConsoleWarn;
|
|
79
|
+
console.error = originalConsoleError;
|
|
80
|
+
});
|
|
81
|
+
describe('utils/ssr-server tests', () => {
|
|
82
|
+
test('getFullRequestURL', () => {
|
|
83
|
+
expect((0, _ssrServer.getFullRequestURL)('https://a.b/c')).toBe('https://a.b/c');
|
|
84
|
+
expect(() => (0, _ssrServer.getFullRequestURL)('/somepath')).toThrow();
|
|
85
|
+
(0, _ssrShared.updatePackageMobify)(baseMobify);
|
|
86
|
+
expect((0, _ssrServer.getFullRequestURL)(`${(0, _ssrPaths.getProxyPath)()}/base/somepath`)).toBe('https://www.merlinspotions.com/somepath');
|
|
87
|
+
expect((0, _ssrServer.getFullRequestURL)(`${(0, _ssrPaths.getProxyPath)()}/base2/somepath`)).toBe('https://api.merlinspotions.com/somepath');
|
|
88
|
+
});
|
|
89
|
+
describe('updatePackageMobify tests', () => {
|
|
90
|
+
afterEach(() => {
|
|
91
|
+
// Clear environment of overrides
|
|
92
|
+
for (let i = 1; i <= 9; i++) {
|
|
93
|
+
delete process.env[`SSR_PROXY${i}`];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const testCases = [{
|
|
97
|
+
name: 'new format proxy configs',
|
|
98
|
+
mobify: {},
|
|
99
|
+
validate: () => {
|
|
100
|
+
const inputConfigs = baseMobify.ssrParameters.proxyConfigs;
|
|
101
|
+
expect(_ssrShared.proxyConfigs).toHaveLength(inputConfigs.length);
|
|
102
|
+
for (let i = 0; i < inputConfigs.length; i++) {
|
|
103
|
+
expect(_ssrShared.proxyConfigs[i].protocol).toEqual(inputConfigs[i].protocol);
|
|
104
|
+
expect(_ssrShared.proxyConfigs[i].host).toEqual(inputConfigs[i].host);
|
|
105
|
+
expect(_ssrShared.proxyConfigs[i].path).toEqual(inputConfigs[i].path);
|
|
106
|
+
expect(_ssrShared.proxyConfigs[i].proxyPath).toBe(`/mobify/proxy/${inputConfigs[i].path}`);
|
|
107
|
+
expect(_ssrShared.proxyConfigs[i].cachingPath).toBe(`/mobify/caching/${inputConfigs[i].path}`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}, {
|
|
111
|
+
name: 'new format proxy configs with overrides',
|
|
112
|
+
mobify: {
|
|
113
|
+
ssrParameters: {
|
|
114
|
+
proxyConfigs: [{
|
|
115
|
+
protocol: 'https',
|
|
116
|
+
host: 'www.merlinspotions.com'
|
|
117
|
+
}, {
|
|
118
|
+
protocol: 'https',
|
|
119
|
+
host: 'api.merlinspotions.com',
|
|
120
|
+
caching: true
|
|
121
|
+
}, {
|
|
122
|
+
host: 'extra.merlinspotions.com',
|
|
123
|
+
path: 'base3'
|
|
124
|
+
}]
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
environment: {
|
|
128
|
+
SSR_PROXY1: 'http://somewhere.else',
|
|
129
|
+
SSR_PROXY2: 'http://another.place/base9',
|
|
130
|
+
SSR_PROXY3: 'http://far.away/base8/caching'
|
|
131
|
+
},
|
|
132
|
+
validate: () => {
|
|
133
|
+
expect(_ssrShared.proxyConfigs).toHaveLength(3);
|
|
134
|
+
expect(_ssrShared.proxyConfigs.every(config => config.protocol === 'http')).toBe(true);
|
|
135
|
+
expect(_ssrShared.proxyConfigs[0].host).toBe('somewhere.else');
|
|
136
|
+
expect(_ssrShared.proxyConfigs[1].host).toBe('another.place');
|
|
137
|
+
expect(_ssrShared.proxyConfigs[2].host).toBe('far.away');
|
|
138
|
+
expect(_ssrShared.proxyConfigs[0].path).toBe('base');
|
|
139
|
+
expect(_ssrShared.proxyConfigs[1].path).toBe('base9');
|
|
140
|
+
expect(_ssrShared.proxyConfigs[2].path).toBe('base8');
|
|
141
|
+
expect(_ssrShared.proxyConfigs[0].proxyPath).toBe('/mobify/proxy/base');
|
|
142
|
+
expect(_ssrShared.proxyConfigs[1].proxyPath).toBe('/mobify/proxy/base9');
|
|
143
|
+
expect(_ssrShared.proxyConfigs[2].proxyPath).toBe('/mobify/proxy/base8');
|
|
144
|
+
expect(_ssrShared.proxyConfigs[0].cachingPath).toBe('/mobify/caching/base');
|
|
145
|
+
expect(_ssrShared.proxyConfigs[1].cachingPath).toBe('/mobify/caching/base9');
|
|
146
|
+
expect(_ssrShared.proxyConfigs[2].cachingPath).toBe('/mobify/caching/base8');
|
|
147
|
+
}
|
|
148
|
+
}, {
|
|
149
|
+
name: 'old format proxy configs',
|
|
150
|
+
mobify: {
|
|
151
|
+
ssrParameters: {
|
|
152
|
+
proxyProtocol1: 'https',
|
|
153
|
+
proxyHost1: 'www.merlinspotions.com',
|
|
154
|
+
proxyPath1: 'base',
|
|
155
|
+
proxyProtocol2: 'https',
|
|
156
|
+
proxyHost2: 'api.merlinspotions.com',
|
|
157
|
+
proxyPath2: 'base2'
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
validate: () => {
|
|
161
|
+
const inputConfigs = baseMobify.ssrParameters.proxyConfigs;
|
|
162
|
+
expect(_ssrShared.proxyConfigs).toHaveLength(inputConfigs.length);
|
|
163
|
+
for (let i = 0; i < inputConfigs.length; i++) {
|
|
164
|
+
expect(_ssrShared.proxyConfigs[i].protocol).toEqual(inputConfigs[i].protocol);
|
|
165
|
+
expect(_ssrShared.proxyConfigs[i].host).toEqual(inputConfigs[i].host);
|
|
166
|
+
expect(_ssrShared.proxyConfigs[i].path).toEqual(inputConfigs[i].path);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}, {
|
|
170
|
+
name: 'too many proxy configs',
|
|
171
|
+
mobify: {
|
|
172
|
+
ssrParameters: {
|
|
173
|
+
// Create an array that is one entry too long
|
|
174
|
+
proxyConfigs: Array.apply(null, {
|
|
175
|
+
length: _ssrShared.MAX_PROXY_CONFIGS + 1
|
|
176
|
+
}).map(Number.call, Number).map(index => ({
|
|
177
|
+
host: `www.${index}.com`
|
|
178
|
+
}))
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}, {
|
|
182
|
+
name: 'no proxy configs',
|
|
183
|
+
mobify: {
|
|
184
|
+
ssrParameters: {}
|
|
185
|
+
},
|
|
186
|
+
validate: () => {
|
|
187
|
+
expect(_ssrShared.proxyConfigs).toHaveLength(0);
|
|
188
|
+
}
|
|
189
|
+
}, {
|
|
190
|
+
name: 'conflicting proxy configs',
|
|
191
|
+
mobify: {
|
|
192
|
+
ssrParameters: {
|
|
193
|
+
proxyProtocol1: 'https',
|
|
194
|
+
proxyHost1: 'www.merlinspotions.com',
|
|
195
|
+
proxyPath1: 'base',
|
|
196
|
+
proxyConfigs: [{
|
|
197
|
+
protocol: 'https',
|
|
198
|
+
host: 'www.merlinspotions.com',
|
|
199
|
+
path: 'base'
|
|
200
|
+
}]
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}, {
|
|
204
|
+
name: 'bad protocol',
|
|
205
|
+
mobify: {
|
|
206
|
+
ssrParameters: {
|
|
207
|
+
proxyConfigs: [{
|
|
208
|
+
protocol: 'file'
|
|
209
|
+
}]
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}, {
|
|
213
|
+
name: 'missing host',
|
|
214
|
+
mobify: {
|
|
215
|
+
ssrParameters: {
|
|
216
|
+
proxyConfigs: [{
|
|
217
|
+
protocol: 'https'
|
|
218
|
+
}]
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}, {
|
|
222
|
+
name: 'duplicate path',
|
|
223
|
+
mobify: {
|
|
224
|
+
ssrParameters: {
|
|
225
|
+
proxyConfigs: [{
|
|
226
|
+
protocol: 'https',
|
|
227
|
+
host: '1.com',
|
|
228
|
+
path: 'base'
|
|
229
|
+
}, {
|
|
230
|
+
protocol: 'https',
|
|
231
|
+
host: '2.com',
|
|
232
|
+
path: 'base'
|
|
233
|
+
}]
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}];
|
|
237
|
+
testCases.forEach(testCase => test(`${testCase.name}`, () => {
|
|
238
|
+
const newMobify = _extends({}, baseMobify, testCase.mobify || {});
|
|
239
|
+
if (testCase.environment) {
|
|
240
|
+
Object.entries(testCase.environment).forEach(([key, value]) => {
|
|
241
|
+
process.env[key] = value;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
if (testCase.validate) {
|
|
245
|
+
(0, _ssrShared.updatePackageMobify)(newMobify);
|
|
246
|
+
testCase.validate((0, _ssrShared.getPackageMobify)());
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
expect(() => (0, _ssrShared.updatePackageMobify)(newMobify)).toThrow();
|
|
250
|
+
}));
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
describe('utils/ssr-shared tests', () => {
|
|
254
|
+
beforeEach(_ssrShared.reset);
|
|
255
|
+
test('Initial setup', () => {
|
|
256
|
+
expect((0, _ssrShared.getPackageMobify)()).toEqual({});
|
|
257
|
+
expect((0, _ssrShared.getSSRParameters)()).toEqual({});
|
|
258
|
+
(0, _ssrShared.updatePackageMobify)();
|
|
259
|
+
expect((0, _ssrShared.getPackageMobify)()).toEqual({});
|
|
260
|
+
expect((0, _ssrShared.getSSRParameters)()).toEqual({});
|
|
261
|
+
});
|
|
262
|
+
test('Mobify update works', () => {
|
|
263
|
+
(0, _ssrShared.updatePackageMobify)(baseMobify);
|
|
264
|
+
expect((0, _ssrShared.getPackageMobify)()).toEqual(baseMobify);
|
|
265
|
+
expect((0, _ssrShared.getSSRParameters)()).toEqual(baseMobify.ssrParameters);
|
|
266
|
+
});
|
|
267
|
+
test('Mobify update validates and sets proxies', () => {
|
|
268
|
+
const mobify = _extends({}, baseMobify);
|
|
269
|
+
mobify.ssrParameters = {};
|
|
270
|
+
(0, _ssrShared.updatePackageMobify)(mobify);
|
|
271
|
+
expect((0, _ssrShared.getSSRParameters)()).toEqual(mobify.ssrParameters);
|
|
272
|
+
expect(_ssrShared.proxyConfigs).toEqual([]);
|
|
273
|
+
mobify.ssrParameters = {
|
|
274
|
+
proxyHost1: '1.merlinspotions.com',
|
|
275
|
+
proxyHost2: '2.merlinspotions.com'
|
|
276
|
+
};
|
|
277
|
+
(0, _ssrShared.updatePackageMobify)(mobify);
|
|
278
|
+
expect((0, _ssrShared.getSSRParameters)()).toEqual(mobify.ssrParameters);
|
|
279
|
+
expect(_ssrShared.proxyConfigs).toEqual([{
|
|
280
|
+
protocol: 'https',
|
|
281
|
+
host: '1.merlinspotions.com',
|
|
282
|
+
path: 'base',
|
|
283
|
+
proxyPath: '/mobify/proxy/base',
|
|
284
|
+
cachingPath: '/mobify/caching/base'
|
|
285
|
+
}, {
|
|
286
|
+
protocol: 'https',
|
|
287
|
+
host: '2.merlinspotions.com',
|
|
288
|
+
path: 'base2',
|
|
289
|
+
proxyPath: '/mobify/proxy/base2',
|
|
290
|
+
cachingPath: '/mobify/caching/base2'
|
|
291
|
+
}]);
|
|
292
|
+
mobify.ssrParameters.proxyConfigs = [{
|
|
293
|
+
host: '3.merlinspotions.com'
|
|
294
|
+
}];
|
|
295
|
+
expect(() => (0, _ssrShared.updatePackageMobify)(mobify)).toThrow('Cannot use both proxyConfigs and old proxy declarations');
|
|
296
|
+
mobify.ssrParameters.proxyConfigs = [{
|
|
297
|
+
protocol: 'file',
|
|
298
|
+
host: '4.merlinspotions.com'
|
|
299
|
+
}];
|
|
300
|
+
mobify.ssrParameters.proxyHost1 = undefined;
|
|
301
|
+
mobify.ssrParameters.proxyHost2 = undefined;
|
|
302
|
+
expect(() => (0, _ssrShared.updatePackageMobify)(mobify)).toThrow('has invalid protocol');
|
|
303
|
+
});
|
|
304
|
+
test('Mobify update sets ssrFiles', () => {
|
|
305
|
+
const mobify = _extends({}, baseMobify);
|
|
306
|
+
mobify.ssrShared = mobify.ssrOnly = undefined;
|
|
307
|
+
(0, _ssrShared.updatePackageMobify)(mobify);
|
|
308
|
+
expect(_ssrShared.ssrFiles).toEqual([]);
|
|
309
|
+
mobify.ssrShared = ['main.js'];
|
|
310
|
+
(0, _ssrShared.updatePackageMobify)(mobify);
|
|
311
|
+
expect(_ssrShared.ssrFiles).toEqual(['main.js']);
|
|
312
|
+
mobify.ssrOnly = ['ssr.js'];
|
|
313
|
+
(0, _ssrShared.updatePackageMobify)(mobify);
|
|
314
|
+
expect(_ssrShared.ssrFiles).toEqual(['ssr.js', 'main.js']);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
test('escapeJSText', () => {
|
|
318
|
+
expect((0, _ssrServer.escapeJSText)()).toBeUndefined();
|
|
319
|
+
expect((0, _ssrServer.escapeJSText)('</script>')).toBe('\\x3c\\x2fscript>');
|
|
320
|
+
});
|
|
321
|
+
describe('MetricsSender', () => {
|
|
322
|
+
const sandbox = _sinon.default.createSandbox();
|
|
323
|
+
afterEach(() => {
|
|
324
|
+
sandbox.restore();
|
|
325
|
+
});
|
|
326
|
+
test('MetricsSender.send', () => {
|
|
327
|
+
const sender = _ssrServer.MetricsSender.getSender();
|
|
328
|
+
const now = new Date();
|
|
329
|
+
const nowISO = now.toISOString();
|
|
330
|
+
const metrics1 = [{
|
|
331
|
+
name: 'abc',
|
|
332
|
+
value: 123,
|
|
333
|
+
timestamp: now
|
|
334
|
+
}, {
|
|
335
|
+
name: 'def',
|
|
336
|
+
dimensions: {
|
|
337
|
+
project: 'whatever',
|
|
338
|
+
xyzzy: 'plugh'
|
|
339
|
+
},
|
|
340
|
+
timestamp: now
|
|
341
|
+
}];
|
|
342
|
+
const metrics2 = [{
|
|
343
|
+
name: 'xyz',
|
|
344
|
+
value: 1.23,
|
|
345
|
+
timestamp: now
|
|
346
|
+
}];
|
|
347
|
+
const metrics3 = [];
|
|
348
|
+
|
|
349
|
+
// Add metrics until we get enough to cause multiple batches to
|
|
350
|
+
// be sent.
|
|
351
|
+
while (metrics1.length + metrics2.length + metrics3.length < 30) {
|
|
352
|
+
metrics3.push({
|
|
353
|
+
name: 'whatever',
|
|
354
|
+
value: Date.now(),
|
|
355
|
+
timestamp: now
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Set up a fake CloudWatch client
|
|
360
|
+
const calledParams = [];
|
|
361
|
+
let callCount = 0;
|
|
362
|
+
sender._CW = {
|
|
363
|
+
putMetricData: (params, callback) => {
|
|
364
|
+
callCount += 1;
|
|
365
|
+
// This returns a fake error on the first call, and then
|
|
366
|
+
// accepts all subsequent calls.
|
|
367
|
+
const err = calledParams.length ? null : new Error('imaginary error');
|
|
368
|
+
params.MetricData.forEach(metric => calledParams.push(metric));
|
|
369
|
+
callback(err, null);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// Send the params.
|
|
374
|
+
sender.send(metrics1);
|
|
375
|
+
sender.send(metrics2);
|
|
376
|
+
sender.send(metrics3);
|
|
377
|
+
expect(sender.queueLength).toEqual(metrics1.length + metrics2.length + metrics3.length);
|
|
378
|
+
return sender.flush().then(() => {
|
|
379
|
+
// Expect that the MetricsSender is now empty
|
|
380
|
+
expect(sender.queueLength).toBe(0);
|
|
381
|
+
|
|
382
|
+
// Expect that there was a console warning
|
|
383
|
+
expect(consoleWarn.called).toBe(true);
|
|
384
|
+
|
|
385
|
+
// Expect that two batches were sent
|
|
386
|
+
expect(callCount).toBe(2);
|
|
387
|
+
|
|
388
|
+
// Expect that the correct values were sent
|
|
389
|
+
const expected = metrics1.concat(metrics2).concat(metrics3);
|
|
390
|
+
expect(calledParams).toHaveLength(expected.length);
|
|
391
|
+
expected.forEach((metric, index) => {
|
|
392
|
+
const actual = calledParams[index];
|
|
393
|
+
expect(actual).toBeDefined();
|
|
394
|
+
expect(actual.MetricName).toEqual(metric.name);
|
|
395
|
+
expect(actual.Value).toEqual(metric.value || 0);
|
|
396
|
+
expect(actual.Timestamp).toEqual(nowISO);
|
|
397
|
+
if (metric.dimensions) {
|
|
398
|
+
expect(actual.Dimensions).toBeDefined();
|
|
399
|
+
actual.Dimensions.forEach(dimension => {
|
|
400
|
+
expect(dimension.Value).toEqual(metric.dimensions[dimension.Name]);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
test('MetricsSender.throttling', () => {
|
|
407
|
+
const sender = _ssrServer.MetricsSender.getSender();
|
|
408
|
+
const now = new Date();
|
|
409
|
+
const metrics1 = [{
|
|
410
|
+
name: 'abc',
|
|
411
|
+
value: 123,
|
|
412
|
+
timestamp: now
|
|
413
|
+
}, {
|
|
414
|
+
name: 'def',
|
|
415
|
+
dimensions: {
|
|
416
|
+
project: 'whatever',
|
|
417
|
+
xyzzy: 'plugh'
|
|
418
|
+
},
|
|
419
|
+
timestamp: now
|
|
420
|
+
}];
|
|
421
|
+
|
|
422
|
+
// Set up a fake CloudWatch client that will return a Throttling
|
|
423
|
+
// error every time it's called.
|
|
424
|
+
sender._CW = {
|
|
425
|
+
putMetricData: (params, callback) => {
|
|
426
|
+
const err = new Error('Throttled');
|
|
427
|
+
err.code = 'Throttling';
|
|
428
|
+
callback(err);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// Allow spying on the putMetricData calls
|
|
433
|
+
sender._CW.putMetricData = sandbox.spy(sender._CW, 'putMetricData');
|
|
434
|
+
|
|
435
|
+
// Send the params.
|
|
436
|
+
sender.send(metrics1);
|
|
437
|
+
expect(sender.queueLength).toEqual(metrics1.length);
|
|
438
|
+
return sender.flush().then(() => {
|
|
439
|
+
// Expect that we retried the correct number of times
|
|
440
|
+
expect(sender._CW.putMetricData.callCount).toBe(1);
|
|
441
|
+
expect(sender.queueLength).toBe(0);
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
test('MetricsSender is a singleton', () => {
|
|
445
|
+
_ssrServer.MetricsSender._instance = null;
|
|
446
|
+
const sender = _ssrServer.MetricsSender.getSender();
|
|
447
|
+
expect(_ssrServer.MetricsSender.getSender()).toBe(sender);
|
|
448
|
+
});
|
|
449
|
+
});
|
|
450
|
+
describe('processLambdaResponse', () => {
|
|
451
|
+
test('processLambdaResponse with no parameter', () => {
|
|
452
|
+
expect((0, _ssrServer.processLambdaResponse)()).toBeUndefined();
|
|
453
|
+
expect((0, _ssrServer.processLambdaResponse)(null)).toBeNull();
|
|
454
|
+
});
|
|
455
|
+
const testCases = [{
|
|
456
|
+
name: 'valid correlation id header in event object',
|
|
457
|
+
event: {
|
|
458
|
+
headers: {
|
|
459
|
+
'x-correlation-id': 'e46cd109-39b7-4173-963e-2c5de78ba087'
|
|
460
|
+
}
|
|
461
|
+
},
|
|
462
|
+
validate: headers => {
|
|
463
|
+
expect(headers['x-correlation-id']).toBe('e46cd109-39b7-4173-963e-2c5de78ba087');
|
|
464
|
+
}
|
|
465
|
+
}, {
|
|
466
|
+
name: 'no correlation id header in event object',
|
|
467
|
+
event: {
|
|
468
|
+
headers: {}
|
|
469
|
+
},
|
|
470
|
+
validate: headers => {
|
|
471
|
+
expect(headers['x-correlation-id']).toBeFalsy();
|
|
472
|
+
}
|
|
473
|
+
}];
|
|
474
|
+
testCases.forEach(testCase => {
|
|
475
|
+
test(`${testCase.name}`, () => {
|
|
476
|
+
const response = {};
|
|
477
|
+
const res = (0, _ssrServer.processLambdaResponse)(response, testCase.event);
|
|
478
|
+
testCase.validate(res.headers);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
describe('processExpressResponse', () => {
|
|
483
|
+
const testCases = [{
|
|
484
|
+
name: 'no change',
|
|
485
|
+
headers: {
|
|
486
|
+
[_constants.CONTENT_TYPE]: 'text/plain'
|
|
487
|
+
},
|
|
488
|
+
validate: headers => {
|
|
489
|
+
expect(headers[_constants.CONTENT_TYPE]).toBe('text/plain');
|
|
490
|
+
expect(headers[_constants.X_ORIGINAL_CONTENT_TYPE]).toBeFalsy();
|
|
491
|
+
}
|
|
492
|
+
}, {
|
|
493
|
+
name: 'flip content-type',
|
|
494
|
+
headers: {
|
|
495
|
+
[_constants.CONTENT_TYPE]: 'text/plain',
|
|
496
|
+
[_constants.CONTENT_ENCODING]: 'gzip'
|
|
497
|
+
},
|
|
498
|
+
validate: headers => {
|
|
499
|
+
expect(headers[_constants.CONTENT_TYPE]).toEqual(_constants.APPLICATION_OCTET_STREAM);
|
|
500
|
+
expect(headers[_constants.CONTENT_ENCODING]).toBe('gzip');
|
|
501
|
+
expect(headers[_constants.X_ORIGINAL_CONTENT_TYPE]).toBe('text/plain');
|
|
502
|
+
}
|
|
503
|
+
}, {
|
|
504
|
+
name: 'already flipped',
|
|
505
|
+
headers: {
|
|
506
|
+
[_constants.CONTENT_TYPE]: _constants.APPLICATION_OCTET_STREAM,
|
|
507
|
+
[_constants.CONTENT_ENCODING]: 'gzip',
|
|
508
|
+
[_constants.X_ORIGINAL_CONTENT_TYPE]: 'text/plain'
|
|
509
|
+
},
|
|
510
|
+
validate: headers => {
|
|
511
|
+
expect(headers[_constants.CONTENT_TYPE]).toEqual(_constants.APPLICATION_OCTET_STREAM);
|
|
512
|
+
expect(headers[_constants.CONTENT_ENCODING]).toBe('gzip');
|
|
513
|
+
expect(headers[_constants.X_ORIGINAL_CONTENT_TYPE]).toBe('text/plain');
|
|
514
|
+
}
|
|
515
|
+
}];
|
|
516
|
+
const responseTypes = [{
|
|
517
|
+
name: 'http.ServerResponse',
|
|
518
|
+
create: headers => ({
|
|
519
|
+
getHeader: header => headers[header],
|
|
520
|
+
setHeader: (header, value) => {
|
|
521
|
+
headers[header] = value;
|
|
522
|
+
}
|
|
523
|
+
})
|
|
524
|
+
}, {
|
|
525
|
+
name: 'http.IncomingMessage',
|
|
526
|
+
create: headers => ({
|
|
527
|
+
headers
|
|
528
|
+
})
|
|
529
|
+
}];
|
|
530
|
+
responseTypes.forEach(responseType => testCases.forEach(testCase =>
|
|
531
|
+
// eslint-disable-next-line jest/expect-expect
|
|
532
|
+
test(`${testCase.name} (${responseType.name})`, () => {
|
|
533
|
+
const headers = _extends({}, testCase.headers);
|
|
534
|
+
const response = responseType.create(headers);
|
|
535
|
+
(0, _ssrServer.processExpressResponse)(response);
|
|
536
|
+
testCase.validate(headers);
|
|
537
|
+
})));
|
|
538
|
+
test('null response', () => {
|
|
539
|
+
expect((0, _ssrServer.processExpressResponse)()).toBeUndefined();
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
describe('outgoingRequestHook tests', () => {
|
|
543
|
+
const sandbox = _sinon.default.createSandbox();
|
|
544
|
+
const mockRequest = sandbox.stub();
|
|
545
|
+
beforeEach(() => {
|
|
546
|
+
mockRequest.reset();
|
|
547
|
+
});
|
|
548
|
+
const appHostname = 'localhost:3444';
|
|
549
|
+
const otherHost = 'somewhere.com';
|
|
550
|
+
const accessKey = 'abcdefghijklm';
|
|
551
|
+
const fakeCallback = () => false;
|
|
552
|
+
test('fall-through when no getAppHost', () => {
|
|
553
|
+
const hook = (0, _ssrServer.outgoingRequestHook)(mockRequest);
|
|
554
|
+
const args = ['a', {
|
|
555
|
+
a: 123
|
|
556
|
+
}, () => false];
|
|
557
|
+
hook(...args);
|
|
558
|
+
expect(mockRequest.calledWith(...args)).toBe(true);
|
|
559
|
+
});
|
|
560
|
+
test('fall-though when empty appHost', () => {
|
|
561
|
+
const hook = (0, _ssrServer.outgoingRequestHook)(mockRequest, {});
|
|
562
|
+
const args = ['a', {
|
|
563
|
+
a: 123
|
|
564
|
+
}, () => false];
|
|
565
|
+
hook(...args);
|
|
566
|
+
expect(mockRequest.calledWith(...args)).toBe(true);
|
|
567
|
+
});
|
|
568
|
+
test('fall-though when no access key', () => {
|
|
569
|
+
const hook = (0, _ssrServer.outgoingRequestHook)(mockRequest, {
|
|
570
|
+
appHostname
|
|
571
|
+
});
|
|
572
|
+
const args = ['a', {
|
|
573
|
+
a: 123
|
|
574
|
+
}, () => false];
|
|
575
|
+
process.env.X_MOBIFY_ACCESS_KEY = undefined;
|
|
576
|
+
hook(...args);
|
|
577
|
+
expect(mockRequest.calledWith(...args)).toBe(true);
|
|
578
|
+
});
|
|
579
|
+
const baseTestCases = [{
|
|
580
|
+
name: 'non-loopback',
|
|
581
|
+
hostname: otherHost,
|
|
582
|
+
expectHeader: false
|
|
583
|
+
}, {
|
|
584
|
+
name: 'loopback',
|
|
585
|
+
hostname: appHostname,
|
|
586
|
+
expectHeader: true
|
|
587
|
+
}];
|
|
588
|
+
const testMethods = ['url', 'host', 'hostname'];
|
|
589
|
+
const withHeaders = [true, false];
|
|
590
|
+
const withCallback = withHeaders;
|
|
591
|
+
const withProxyKeepAliveAgent = withCallback;
|
|
592
|
+
const testCases = [];
|
|
593
|
+
baseTestCases.forEach(baseTestCase => testMethods.forEach(testMethod => withHeaders.forEach(addHeaders => withCallback.forEach(addCallback => {
|
|
594
|
+
withProxyKeepAliveAgent.forEach(addProxyKeepAliveAgent => {
|
|
595
|
+
const testCase = _objectSpread({}, baseTestCase);
|
|
596
|
+
testCase.name = `${testCase.name} via ${testMethod} ` + `${addHeaders ? 'with' : 'without'} headers, ` + `${addCallback ? 'with' : 'without'} callback, ` + `${addProxyKeepAliveAgent ? 'with' : 'without'} KeepAliveAgent`;
|
|
597
|
+
testCase.testMethod = testMethod;
|
|
598
|
+
testCase.addHeaders = addHeaders;
|
|
599
|
+
testCase.addCallback = addCallback;
|
|
600
|
+
testCase.addProxyKeepAliveAgent = addProxyKeepAliveAgent;
|
|
601
|
+
testCases.push(testCase);
|
|
602
|
+
});
|
|
603
|
+
}))));
|
|
604
|
+
testCases.forEach(testCase => test(`${testCase.name}`, () => {
|
|
605
|
+
const createAppOptions = {
|
|
606
|
+
appHostname
|
|
607
|
+
};
|
|
608
|
+
createAppOptions.proxyKeepAliveAgent = testCase.addProxyKeepAliveAgent;
|
|
609
|
+
const hook = (0, _ssrServer.outgoingRequestHook)(mockRequest, createAppOptions);
|
|
610
|
+
const args = [];
|
|
611
|
+
const hookOptions = {};
|
|
612
|
+
let expectUrl;
|
|
613
|
+
let optionsPushed = false;
|
|
614
|
+
switch (testCase.testMethod) {
|
|
615
|
+
case 'url':
|
|
616
|
+
args.push(`//${testCase.hostname}`);
|
|
617
|
+
expectUrl = true;
|
|
618
|
+
break;
|
|
619
|
+
case 'host':
|
|
620
|
+
hookOptions.host = testCase.hostname;
|
|
621
|
+
args.push(hookOptions);
|
|
622
|
+
optionsPushed = true;
|
|
623
|
+
break;
|
|
624
|
+
case 'hostname':
|
|
625
|
+
hookOptions.hostname = testCase.hostname;
|
|
626
|
+
args.push(hookOptions);
|
|
627
|
+
optionsPushed = true;
|
|
628
|
+
break;
|
|
629
|
+
default:
|
|
630
|
+
throw new Error(`Bad testMethod ${testCase.testMethod}`);
|
|
631
|
+
}
|
|
632
|
+
if (testCase.addHeaders) {
|
|
633
|
+
hookOptions.headers = {
|
|
634
|
+
'x-extra': '123'
|
|
635
|
+
};
|
|
636
|
+
if (!optionsPushed) {
|
|
637
|
+
args.push(hookOptions);
|
|
638
|
+
optionsPushed = true;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
if (testCase.addCallback) {
|
|
642
|
+
args.push(fakeCallback);
|
|
643
|
+
}
|
|
644
|
+
process.env.X_MOBIFY_ACCESS_KEY = accessKey;
|
|
645
|
+
hook(...args);
|
|
646
|
+
expect(mockRequest.calledOnce).toBe(true);
|
|
647
|
+
const called = mockRequest.getCall(0).args.slice();
|
|
648
|
+
if (expectUrl) {
|
|
649
|
+
const url = called.shift();
|
|
650
|
+
expect(url).toBe(`//${testCase.hostname}`);
|
|
651
|
+
}
|
|
652
|
+
let calledOptions;
|
|
653
|
+
if (optionsPushed || testCase.expectHeader) {
|
|
654
|
+
calledOptions = called.shift();
|
|
655
|
+
}
|
|
656
|
+
const calledHeaders = calledOptions && calledOptions.headers;
|
|
657
|
+
if (testCase.addHeaders) {
|
|
658
|
+
expect(calledHeaders).toBeDefined();
|
|
659
|
+
expect(calledHeaders['x-extra']).toBe('123');
|
|
660
|
+
}
|
|
661
|
+
if (testCase.expectHeader) {
|
|
662
|
+
expect(calledHeaders).toBeDefined();
|
|
663
|
+
expect(calledHeaders['x-mobify-access-key']).toEqual(accessKey);
|
|
664
|
+
} else {
|
|
665
|
+
if (calledHeaders) {
|
|
666
|
+
expect(calledHeaders['x-mobify-access-key']).toBeUndefined();
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
if (testCase.addCallback) {
|
|
670
|
+
expect(called[0]).toBe(fakeCallback);
|
|
671
|
+
}
|
|
672
|
+
if (testCase.name.startsWith('loopback')) {
|
|
673
|
+
if (testCase.addProxyKeepAliveAgent) {
|
|
674
|
+
expect(calledOptions.agent).toBeDefined();
|
|
675
|
+
expect(calledOptions.agent.keepAlive).toBe(true);
|
|
676
|
+
} else {
|
|
677
|
+
expect(calledOptions.agent).toBeUndefined();
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
}));
|
|
681
|
+
});
|
|
682
|
+
describe('updateGlobalAgentOptions', () => {
|
|
683
|
+
test('no-op', () => {
|
|
684
|
+
const to = {
|
|
685
|
+
options: {}
|
|
686
|
+
};
|
|
687
|
+
(0, _ssrServer.updateGlobalAgentOptions)(undefined, to);
|
|
688
|
+
expect(to.options).toEqual({});
|
|
689
|
+
});
|
|
690
|
+
test('overwrites', () => {
|
|
691
|
+
const to = {};
|
|
692
|
+
const from = {
|
|
693
|
+
options: {}
|
|
694
|
+
};
|
|
695
|
+
_ssrServer.AGENT_OPTIONS_TO_COPY.forEach(key => {
|
|
696
|
+
from.options[key] = key;
|
|
697
|
+
});
|
|
698
|
+
(0, _ssrServer.updateGlobalAgentOptions)(from, to);
|
|
699
|
+
expect(to.options).toEqual(from.options);
|
|
700
|
+
});
|
|
701
|
+
test('preserves', () => {
|
|
702
|
+
const to = {
|
|
703
|
+
options: {
|
|
704
|
+
xyzzy: 1
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
const from = {
|
|
708
|
+
options: {}
|
|
709
|
+
};
|
|
710
|
+
_ssrServer.AGENT_OPTIONS_TO_COPY.forEach(key => {
|
|
711
|
+
from.options[key] = key;
|
|
712
|
+
});
|
|
713
|
+
(0, _ssrServer.updateGlobalAgentOptions)(from, to);
|
|
714
|
+
expect(to.options.xyzzy).toBe(1);
|
|
715
|
+
Object.entries(from.options).forEach(([key, value]) => expect(to.options[key]).toEqual(value));
|
|
716
|
+
});
|
|
717
|
+
});
|
|
718
|
+
describe('parseCacheControl', () => {
|
|
719
|
+
test('accepts undefined', () => {
|
|
720
|
+
const result = (0, _ssrServer.parseCacheControl)();
|
|
721
|
+
expect(result).toBeDefined();
|
|
722
|
+
expect(result['s-maxage']).toBeUndefined();
|
|
723
|
+
expect(result['max-age']).toBeUndefined();
|
|
724
|
+
});
|
|
725
|
+
test('parses when no age values', () => {
|
|
726
|
+
const result = (0, _ssrServer.parseCacheControl)('nocache, nostore');
|
|
727
|
+
expect(result).toBeDefined();
|
|
728
|
+
expect(result['s-maxage']).toBeUndefined();
|
|
729
|
+
expect(result['max-age']).toBeUndefined();
|
|
730
|
+
});
|
|
731
|
+
test('parses max-age', () => {
|
|
732
|
+
const result = (0, _ssrServer.parseCacheControl)('max-age=123, nocache, nostore, must-revalidate');
|
|
733
|
+
expect(result).toBeDefined();
|
|
734
|
+
expect(result['s-maxage']).toBeUndefined();
|
|
735
|
+
expect(result['max-age']).toBe('123');
|
|
736
|
+
});
|
|
737
|
+
test('parses s-maxage', () => {
|
|
738
|
+
const result = (0, _ssrServer.parseCacheControl)('s-maxage=123, nocache, nostore, must-revalidate');
|
|
739
|
+
expect(result).toBeDefined();
|
|
740
|
+
expect(result['max-age']).toBeUndefined();
|
|
741
|
+
expect(result['s-maxage']).toBe('123');
|
|
742
|
+
});
|
|
743
|
+
test('parses max-age and s-maxage', () => {
|
|
744
|
+
const result = (0, _ssrServer.parseCacheControl)('s-maxage=123, nocache, max-age=456, nostore, must-revalidate');
|
|
745
|
+
expect(result).toBeDefined();
|
|
746
|
+
expect(result['max-age']).toBe('456');
|
|
747
|
+
expect(result['s-maxage']).toBe('123');
|
|
748
|
+
});
|
|
749
|
+
});
|
|
750
|
+
describe('parseEndParameters', () => {
|
|
751
|
+
test('no parameters', () => {
|
|
752
|
+
expect((0, _ssrServer.parseEndParameters)()).toEqual({
|
|
753
|
+
data: undefined,
|
|
754
|
+
encoding: undefined,
|
|
755
|
+
callback: undefined
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
test('data', () => {
|
|
759
|
+
expect((0, _ssrServer.parseEndParameters)([123])).toEqual({
|
|
760
|
+
data: 123,
|
|
761
|
+
encoding: undefined,
|
|
762
|
+
callback: undefined
|
|
763
|
+
});
|
|
764
|
+
});
|
|
765
|
+
test('data, encoding', () => {
|
|
766
|
+
expect((0, _ssrServer.parseEndParameters)([123, 456])).toEqual({
|
|
767
|
+
data: 123,
|
|
768
|
+
encoding: 456,
|
|
769
|
+
callback: undefined
|
|
770
|
+
});
|
|
771
|
+
});
|
|
772
|
+
test('data, callback', () => {
|
|
773
|
+
const f = () => true;
|
|
774
|
+
expect((0, _ssrServer.parseEndParameters)([123, f])).toEqual({
|
|
775
|
+
data: 123,
|
|
776
|
+
encoding: undefined,
|
|
777
|
+
callback: f
|
|
778
|
+
});
|
|
779
|
+
});
|
|
780
|
+
test('data, encoding, callback', () => {
|
|
781
|
+
const f = () => true;
|
|
782
|
+
expect((0, _ssrServer.parseEndParameters)([123, 456, f])).toEqual({
|
|
783
|
+
data: 123,
|
|
784
|
+
encoding: 456,
|
|
785
|
+
callback: f
|
|
786
|
+
});
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
describe('wrapResponseWrite', () => {
|
|
790
|
+
test('captures chunks', () => {
|
|
791
|
+
const write = _sinon.default.stub();
|
|
792
|
+
const response = {
|
|
793
|
+
locals: {
|
|
794
|
+
responseCaching: {}
|
|
795
|
+
},
|
|
796
|
+
write
|
|
797
|
+
};
|
|
798
|
+
const caching = response.locals.responseCaching;
|
|
799
|
+
(0, _ssrServer.wrapResponseWrite)(response);
|
|
800
|
+
expect(caching.chunks).toEqual([]);
|
|
801
|
+
const chunks = caching.chunks;
|
|
802
|
+
|
|
803
|
+
// String without encoding
|
|
804
|
+
response.write('12345');
|
|
805
|
+
expect(chunks).toHaveLength(1);
|
|
806
|
+
expect(chunks[0].toString()).toBe('12345');
|
|
807
|
+
expect(write.calledWith('12345')).toBe(true);
|
|
808
|
+
write.reset();
|
|
809
|
+
|
|
810
|
+
// String with encoding
|
|
811
|
+
response.write('67890', 'utf-8');
|
|
812
|
+
expect(chunks).toHaveLength(2);
|
|
813
|
+
expect(chunks[1].toString()).toBe('67890');
|
|
814
|
+
expect(write.calledWith('67890', 'utf-8')).toBe(true);
|
|
815
|
+
write.reset();
|
|
816
|
+
|
|
817
|
+
// Buffer
|
|
818
|
+
const chunk1 = Buffer.from('abcde');
|
|
819
|
+
response.write(chunk1);
|
|
820
|
+
expect(chunks).toHaveLength(3);
|
|
821
|
+
expect(chunks[2].toString()).toBe('abcde');
|
|
822
|
+
expect(write.calledWith(chunk1)).toBe(true);
|
|
823
|
+
write.reset();
|
|
824
|
+
expect(() => response.write({})).toThrow('unexpected type');
|
|
825
|
+
});
|
|
826
|
+
});
|
|
827
|
+
describe('CachedResponse', () => {
|
|
828
|
+
test('Empty create', () => {
|
|
829
|
+
const cached = new _ssrServer.CachedResponse({});
|
|
830
|
+
expect(cached.found).toBe(false);
|
|
831
|
+
expect(cached.data).toBeUndefined();
|
|
832
|
+
expect(cached.status).toBe(200);
|
|
833
|
+
expect(cached.headers).toEqual({});
|
|
834
|
+
expect(cached.expiration).toBeUndefined();
|
|
835
|
+
});
|
|
836
|
+
test('Create', () => {
|
|
837
|
+
const req = {
|
|
838
|
+
a: 1
|
|
839
|
+
};
|
|
840
|
+
const res = {
|
|
841
|
+
b: 2
|
|
842
|
+
};
|
|
843
|
+
const entry = {
|
|
844
|
+
found: true,
|
|
845
|
+
key: 'key',
|
|
846
|
+
namespace: 'namespace',
|
|
847
|
+
data: Buffer.from('123'),
|
|
848
|
+
expiration: Date.now(),
|
|
849
|
+
metadata: {
|
|
850
|
+
status: 201,
|
|
851
|
+
headers: {
|
|
852
|
+
'x-special': '1'
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
const cached = new _ssrServer.CachedResponse({
|
|
857
|
+
entry,
|
|
858
|
+
req,
|
|
859
|
+
res
|
|
860
|
+
});
|
|
861
|
+
expect(cached.found).toBe(true);
|
|
862
|
+
expect(cached.key).toBe('key');
|
|
863
|
+
expect(cached.namespace).toBe('namespace');
|
|
864
|
+
expect(cached.data).toEqual(entry.data);
|
|
865
|
+
expect(cached.headers).toEqual(entry.metadata.headers);
|
|
866
|
+
expect(cached.status).toBe(201);
|
|
867
|
+
expect(cached.expiration).toEqual(new Date(entry.expiration));
|
|
868
|
+
});
|
|
869
|
+
});
|