@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.
Files changed (52) hide show
  1. package/package.json +6 -5
  2. package/ssr/server/build-remote-server.js +1159 -0
  3. package/ssr/server/build-remote-server.test.js +41 -0
  4. package/ssr/server/constants.js +37 -0
  5. package/ssr/server/express.js +462 -0
  6. package/ssr/server/express.lambda.test.js +390 -0
  7. package/ssr/server/express.test.js +963 -0
  8. package/ssr/server/test_fixtures/favicon.ico +0 -0
  9. package/ssr/server/test_fixtures/loadable-stats.json +1 -0
  10. package/ssr/server/test_fixtures/localhost.pem +45 -0
  11. package/ssr/server/test_fixtures/main.js +7 -0
  12. package/ssr/server/test_fixtures/mobify.png +0 -0
  13. package/ssr/server/test_fixtures/server-renderer.js +12 -0
  14. package/ssr/server/test_fixtures/worker.js +7 -0
  15. package/ssr/server/test_fixtures/worker.js.map +1 -0
  16. package/utils/logger-factory.js +154 -0
  17. package/utils/logger-factory.test.js +71 -0
  18. package/utils/logger-instance.js +19 -0
  19. package/utils/middleware/index.js +16 -0
  20. package/utils/middleware/security.js +111 -0
  21. package/utils/middleware/security.test.js +110 -0
  22. package/utils/morgan-stream.js +28 -0
  23. package/utils/ssr-cache.js +177 -0
  24. package/utils/ssr-cache.test.js +64 -0
  25. package/utils/ssr-config.client.js +23 -0
  26. package/utils/ssr-config.client.test.js +25 -0
  27. package/utils/ssr-config.js +20 -0
  28. package/utils/ssr-config.server.js +98 -0
  29. package/utils/ssr-config.server.test.js +50 -0
  30. package/utils/ssr-namespace-paths.js +60 -0
  31. package/utils/ssr-namespace-paths.test.js +30 -0
  32. package/utils/ssr-paths.js +51 -0
  33. package/utils/ssr-paths.test.js +49 -0
  34. package/utils/ssr-proxying.js +859 -0
  35. package/utils/ssr-proxying.test.js +593 -0
  36. package/utils/ssr-request-processing.js +164 -0
  37. package/utils/ssr-request-processing.test.js +95 -0
  38. package/utils/ssr-server/cached-response.js +116 -0
  39. package/utils/ssr-server/configure-proxy.js +304 -0
  40. package/utils/ssr-server/metrics-sender.js +204 -0
  41. package/utils/ssr-server/outgoing-request-hook.js +139 -0
  42. package/utils/ssr-server/parse-end-parameters.js +38 -0
  43. package/utils/ssr-server/process-express-response.js +56 -0
  44. package/utils/ssr-server/process-lambda-response.js +43 -0
  45. package/utils/ssr-server/update-global-agent-options.js +41 -0
  46. package/utils/ssr-server/update-global-agent-options.test.js +28 -0
  47. package/utils/ssr-server/utils.js +124 -0
  48. package/utils/ssr-server/utils.test.js +69 -0
  49. package/utils/ssr-server/wrap-response-write.js +40 -0
  50. package/utils/ssr-server.js +115 -0
  51. package/utils/ssr-server.test.js +869 -0
  52. package/utils/ssr-shared.js +183 -0
@@ -0,0 +1,593 @@
1
+ "use strict";
2
+
3
+ var _ssrProxying = require("./ssr-proxying");
4
+ 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); } /*
5
+ * Copyright (c) 2021, salesforce.com, inc.
6
+ * All rights reserved.
7
+ * SPDX-License-Identifier: BSD-3-Clause
8
+ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
9
+ */
10
+ describe('rfc1123 tests', () => {
11
+ const testCases = [{
12
+ date: new Date('2018-10-24T10:09:08Z'),
13
+ expected: 'Wed, 24 Oct 2018 10:09:08 GMT'
14
+ }, {
15
+ date: new Date('2018-10-24T23:22:21-01:00'),
16
+ expected: 'Thu, 25 Oct 2018 00:22:21 GMT'
17
+ }];
18
+ testCases.forEach(testCase => test(`Expecting ${testCase.expected}`, () => {
19
+ expect((0, _ssrProxying.rfc1123)(testCase.date)).toEqual(testCase.expected);
20
+ }));
21
+ });
22
+ describe('parseHost tests', () => {
23
+ const testCases = [{
24
+ name: 'localhost with port',
25
+ host: 'localhost:8080',
26
+ expected: {
27
+ host: 'localhost:8080',
28
+ hostname: 'localhost',
29
+ port: '8080',
30
+ isIPOrLocalhost: true
31
+ }
32
+ }, {
33
+ name: 'localhost without port',
34
+ host: 'localhost',
35
+ expected: {
36
+ host: 'localhost',
37
+ hostname: 'localhost',
38
+ isIPOrLocalhost: true
39
+ }
40
+ }, {
41
+ name: 'single-word host with port',
42
+ host: 'xyzzy:8080',
43
+ expected: {
44
+ host: 'xyzzy:8080',
45
+ hostname: 'xyzzy',
46
+ port: '8080',
47
+ isIPOrLocalhost: false
48
+ }
49
+ }, {
50
+ name: 'single-word host without port',
51
+ host: 'doobrie',
52
+ expected: {
53
+ host: 'doobrie',
54
+ hostname: 'doobrie',
55
+ isIPOrLocalhost: false
56
+ }
57
+ }, {
58
+ name: 'hostname without port',
59
+ host: 'www.customer.com',
60
+ expected: {
61
+ host: 'www.customer.com',
62
+ hostname: 'www.customer.com',
63
+ isIPOrLocalhost: false
64
+ }
65
+ }, {
66
+ name: 'hostname with port',
67
+ host: 'www.customer.com:1234',
68
+ expected: {
69
+ host: 'www.customer.com:1234',
70
+ hostname: 'www.customer.com',
71
+ port: '1234',
72
+ isIPOrLocalhost: false
73
+ }
74
+ }, {
75
+ name: 'ipv4 with port',
76
+ host: '1.2.3.4:1234',
77
+ expected: {
78
+ host: '1.2.3.4:1234',
79
+ hostname: '1.2.3.4',
80
+ port: '1234',
81
+ isIPOrLocalhost: true
82
+ }
83
+ }, {
84
+ name: 'ipv4 without port',
85
+ host: '192.168.1.128:1234',
86
+ expected: {
87
+ host: '192.168.1.128:1234',
88
+ hostname: '192.168.1.128',
89
+ port: '1234',
90
+ isIPOrLocalhost: true
91
+ }
92
+ }, {
93
+ name: 'ipv6 with port',
94
+ host: '[2001:db8::1]:8080',
95
+ expected: {
96
+ host: '[2001:db8::1]:8080',
97
+ hostname: '2001:db8::1',
98
+ port: '8080',
99
+ isIPOrLocalhost: true
100
+ }
101
+ }, {
102
+ name: 'ipv6 without port',
103
+ host: '2001:db8::1',
104
+ expected: {
105
+ host: '2001:db8::1',
106
+ hostname: '2001:db8::1',
107
+ isIPOrLocalhost: true
108
+ }
109
+ }, {
110
+ name: 'ipv4 without port',
111
+ host: '2001:db8::1',
112
+ expected: {
113
+ host: '2001:db8::1',
114
+ hostname: '2001:db8::1',
115
+ isIPOrLocalhost: true
116
+ }
117
+ }];
118
+ testCases.forEach(testCase => test(`${testCase.name} (${testCase.host})`, () => {
119
+ expect((0, _ssrProxying.parseHost)(testCase.host)).toEqual(testCase.expected);
120
+ }));
121
+ });
122
+ describe('cookieAsString tests', () => {
123
+ const testCases = [{
124
+ cookie: {
125
+ name: 'abc',
126
+ value: '123'
127
+ },
128
+ expected: 'abc=123'
129
+ }, {
130
+ cookie: {
131
+ name: 'def',
132
+ value: '456',
133
+ secure: true,
134
+ httpOnly: true,
135
+ sameSite: 'lax'
136
+ },
137
+ expected: 'def=456; Secure; HttpOnly; SameSite=lax'
138
+ }, {
139
+ cookie: {
140
+ name: 'abc',
141
+ value: '123',
142
+ domain: 'mobify.com',
143
+ maxAge: 123,
144
+ path: '/'
145
+ },
146
+ expected: 'abc=123; Path=/; Domain=mobify.com; Max-Age=123'
147
+ }, {
148
+ cookie: {
149
+ name: 'abc',
150
+ value: '123',
151
+ expires: new Date('2018-10-24T10:09:08Z')
152
+ },
153
+ expected: 'abc=123; Expires=Wed, 24 Oct 2018 10:09:08 GMT'
154
+ }];
155
+ testCases.forEach(testCase => test(`Expecting ${testCase.expected}`, () => {
156
+ expect((0, _ssrProxying.cookieAsString)(testCase.cookie)).toEqual(testCase.expected);
157
+ }));
158
+ });
159
+ describe('rewriteProxyResponseHeaders tests', () => {
160
+ const tooLongUrlBase = 'https://www.customer.com/xyz?xyz=';
161
+ // Because this string is based on tooLongUrlBase,
162
+ // it will exceed MAX_URL_LENGTH_BYTES in length.
163
+ const tooLongUrl = tooLongUrlBase.padEnd(_ssrProxying.MAX_URL_LENGTH_BYTES, 'x');
164
+ const testCases = [{
165
+ name: 'no changes expected',
166
+ appHostname: 'apphost.mobify.com',
167
+ targetHost: 'www.customer.com',
168
+ statusCode: 200,
169
+ input: {
170
+ 'access-control-allow-origin': '*',
171
+ location: '/xyz',
172
+ 'content-type': 'application/octet-stream'
173
+ },
174
+ expected: {
175
+ 'access-control-allow-origin': '*',
176
+ location: '/xyz',
177
+ 'content-type': 'application/octet-stream'
178
+ }
179
+ }, {
180
+ name: 'no domain rewrite of set-cookie',
181
+ appHostname: 'apphost.mobify.com',
182
+ targetHost: 'www.customer.com',
183
+ statusCode: 200,
184
+ input: {
185
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; secure; httpOnly'
186
+ },
187
+ expected: {
188
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Secure; HttpOnly']
189
+ }
190
+ }, {
191
+ name: 'rewrite set-cookie',
192
+ appHostname: 'apphost.mobify.com',
193
+ targetHost: 'www.customer.com',
194
+ statusCode: 200,
195
+ input: {
196
+ 'access-control-allow-origin': '*',
197
+ 'set-cookie': ['origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.www.customer.com', 'origin_dc=war; expires=Tue, 02-Oct-2018 00:13:20 GMT; path=/; domain=.someone.com']
198
+ },
199
+ expected: {
200
+ 'access-control-allow-origin': '*',
201
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=apphost.mobify.com', 'origin_dc=war; Path=/; Expires=Tue, 02 Oct 2018 00:13:20 GMT; Domain=.someone.com']
202
+ }
203
+ }, {
204
+ name: 'rewrite set-cookie (AWS format)',
205
+ format: 'aws',
206
+ appHostname: 'apphost.mobify.com',
207
+ targetHost: 'www.customer.com',
208
+ statusCode: 200,
209
+ input: {
210
+ 'set-cookie': [{
211
+ key: 'Set-Cookie',
212
+ value: 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.www.customer.com'
213
+ }]
214
+ },
215
+ expected: {
216
+ 'set-cookie': [{
217
+ key: 'Set-Cookie',
218
+ value: 'origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=apphost.mobify.com'
219
+ }]
220
+ }
221
+ }, {
222
+ name: 'rewrite set-cookie local',
223
+ appHostname: 'localhost:3443',
224
+ targetHost: 'www.customer.com',
225
+ statusCode: 200,
226
+ input: {
227
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.www.customer.com'
228
+ },
229
+ expected: {
230
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=localhost']
231
+ }
232
+ }, {
233
+ name: 'rewrite set-cookie local with subdomain',
234
+ appHostname: 'localhost:3443',
235
+ targetHost: 'www.customer.com',
236
+ statusCode: 200,
237
+ input: {
238
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.customer.com'
239
+ },
240
+ expected: {
241
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=localhost']
242
+ }
243
+ }, {
244
+ name: 'rewrite set-cookie third-party',
245
+ appHostname: 'localhost:3443',
246
+ targetHost: 'www.customer.com',
247
+ statusCode: 200,
248
+ input: {
249
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.someone.com'
250
+ },
251
+ expected: {
252
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=.someone.com']
253
+ }
254
+ }, {
255
+ name: 'rewrite access-control and set-cookie',
256
+ appHostname: 'apphost.mobify.com',
257
+ targetHost: 'www.customer.com',
258
+ statusCode: 200,
259
+ input: {
260
+ 'access-control-allow-origin': 'https://www.customer.com',
261
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.customer.com'
262
+ },
263
+ expected: {
264
+ 'access-control-allow-origin': 'https://apphost.mobify.com',
265
+ 'set-cookie': ['origin_dc=war; Path=/; Expires=Mon, 01 Oct 2018 00:13:20 GMT; Domain=mobify.com']
266
+ }
267
+ }, {
268
+ name: 'remove set-cookie',
269
+ caching: true,
270
+ appHostname: 'localhost:3443',
271
+ targetHost: 'www.customer.com',
272
+ statusCode: 200,
273
+ input: {
274
+ 'set-cookie': 'origin_dc=war; expires=Mon, 01-Oct-2018 00:13:20 GMT; path=/; domain=.www.customer.com'
275
+ },
276
+ expected: {
277
+ 'set-cookie': undefined
278
+ }
279
+ }, {
280
+ name: 'rewrite location',
281
+ appHostname: 'apphost.mobify.com',
282
+ targetHost: 'www.customer.com',
283
+ statusCode: 301,
284
+ input: {
285
+ location: 'https://www.customer.com/abc/def'
286
+ },
287
+ expected: {
288
+ location: 'https://apphost.mobify.com/mobify/proxy/base/abc/def'
289
+ }
290
+ }, {
291
+ name: "don't rewrite location",
292
+ appHostname: 'apphost.mobify.com',
293
+ targetHost: 'www.customer.com',
294
+ statusCode: 301,
295
+ input: {
296
+ location: 'https://www.elsewhere.com/abc/def'
297
+ },
298
+ expected: {
299
+ location: 'https://www.elsewhere.com/abc/def'
300
+ }
301
+ }, {
302
+ name: 'rewrite location (AWS format)',
303
+ format: 'aws',
304
+ appHostname: 'apphost.mobify.com',
305
+ targetHost: 'www.customer.com',
306
+ statusCode: 301,
307
+ input: {
308
+ location: [{
309
+ key: 'Location',
310
+ value: 'https://www.customer.com/abc/def'
311
+ }]
312
+ },
313
+ expected: {
314
+ location: [{
315
+ key: 'Location',
316
+ value: 'https://apphost.mobify.com/mobify/proxy/base/abc/def'
317
+ }]
318
+ }
319
+ }, {
320
+ name: 'x-proxy-request-url (full URL)',
321
+ appHostname: 'apphost.mobify.com',
322
+ targetHost: 'www.customer.com',
323
+ statusCode: 200,
324
+ requestUrl: 'https://www.customer.com/xyz?xyz=123',
325
+ expected: {
326
+ 'x-proxy-request-url': 'https://www.customer.com/xyz?xyz=123'
327
+ }
328
+ }, {
329
+ name: 'x-proxy-request-url (path-only URL)',
330
+ appHostname: 'apphost.mobify.com',
331
+ targetHost: 'www.customer.com',
332
+ statusCode: 200,
333
+ requestUrl: '/xyz?abc=def',
334
+ expected: {
335
+ 'x-proxy-request-url': 'https://www.customer.com/xyz?abc=def'
336
+ }
337
+ }, {
338
+ name: 'x-proxy-request-url (long URL)',
339
+ appHostname: 'apphost.mobify.com',
340
+ targetHost: 'www.customer.com',
341
+ statusCode: 200,
342
+ requestUrl: tooLongUrl,
343
+ expected: {
344
+ 'x-proxy-request-url': tooLongUrl.slice(0, _ssrProxying.MAX_URL_LENGTH_BYTES)
345
+ }
346
+ }];
347
+ testCases.forEach((testCase, testCaseIndex) =>
348
+ // eslint-disable-next-line jest/valid-title
349
+ test(testCase.name || `test ${testCaseIndex}`, () => {
350
+ const updatedHeaders = (0, _ssrProxying.rewriteProxyResponseHeaders)({
351
+ appHostname: testCase.appHostname,
352
+ proxyPath: '/mobify/proxy/base/',
353
+ statusCode: testCase.statusCode,
354
+ headers: testCase.input,
355
+ headerFormat: testCase.format || 'http',
356
+ caching: !!testCase.caching,
357
+ targetProtocol: 'https',
358
+ targetHost: testCase.targetHost,
359
+ logging: true,
360
+ requestUrl: testCase.requestUrl
361
+ });
362
+ Object.entries(testCase.expected).forEach(([key, value]) => {
363
+ const actual = updatedHeaders[key];
364
+ expect(actual).toEqual(value);
365
+ });
366
+ }));
367
+ test('missing headers', () => {
368
+ expect((0, _ssrProxying.rewriteProxyResponseHeaders)({
369
+ appHostname: '',
370
+ proxyPath: '/mobify/proxy/base',
371
+ targetProtocol: 'https',
372
+ targetHost: 'www.customer.com'
373
+ })).toEqual({});
374
+ });
375
+ test('Headers.modified', () => {
376
+ const headers = new _ssrProxying.Headers({
377
+ a: '1'
378
+ }, 'http');
379
+ expect(headers.modified).toBe(false);
380
+ headers.setHeader('a', '2');
381
+ expect(headers.modified).toBe(true);
382
+ });
383
+ test('Headers case handling', () => {
384
+ const headers = new _ssrProxying.Headers({}, 'aws');
385
+ headers.setHeader('cached_response', 'true');
386
+ const result = headers.toObject();
387
+ expect(result['cached_response']).toEqual([{
388
+ key: 'cached_response',
389
+ value: 'true'
390
+ }]);
391
+ });
392
+ test('bad Headers format', () => {
393
+ expect(() => new _ssrProxying.Headers({})).toThrow();
394
+ });
395
+ });
396
+ describe('rewriteProxyRequestHeaders tests', () => {
397
+ const testCases = [{
398
+ name: 'no changes expected',
399
+ targetHost: 'www.customer.com',
400
+ input: {
401
+ 'accept-encoding': 'deflate, gzip',
402
+ cookie: 'abc=123'
403
+ },
404
+ expected: {
405
+ 'accept-encoding': 'deflate, gzip',
406
+ cookie: 'abc=123'
407
+ }
408
+ }, {
409
+ name: 'rewrite host (AWS format)',
410
+ format: 'aws',
411
+ targetHost: 'www.customer.com',
412
+ input: {
413
+ host: [{
414
+ key: 'Host',
415
+ value: 'apphost.mobify.com'
416
+ }]
417
+ },
418
+ expected: {
419
+ host: [{
420
+ key: 'Host',
421
+ value: 'www.customer.com'
422
+ }]
423
+ }
424
+ }, {
425
+ name: 'rewrite origin (AWS format)',
426
+ format: 'aws',
427
+ targetHost: 'www.customer.com',
428
+ targetProtocol: 'http',
429
+ input: {
430
+ origin: [{
431
+ key: 'Origin',
432
+ value: 'https://apphost.mobify.com'
433
+ }]
434
+ },
435
+ expected: {
436
+ origin: [{
437
+ key: 'Origin',
438
+ value: 'http://www.customer.com'
439
+ }]
440
+ }
441
+ }, {
442
+ name: 'rewrite host (HTTP format)',
443
+ format: 'http',
444
+ targetHost: 'www.customer.com',
445
+ input: {
446
+ host: 'apphost.mobify.com'
447
+ },
448
+ expected: {
449
+ host: 'www.customer.com'
450
+ }
451
+ }, {
452
+ name: 'rewrite origin (HTTP format)',
453
+ format: 'http',
454
+ targetHost: 'www.customer.com',
455
+ targetProtocol: 'http',
456
+ input: {
457
+ 'accept-encoding': 'deflate, gzip',
458
+ cookie: 'abc=123',
459
+ origin: 'https://apphost.mobify.com'
460
+ },
461
+ expected: {
462
+ 'accept-encoding': 'deflate, gzip',
463
+ cookie: 'abc=123',
464
+ origin: 'http://www.customer.com'
465
+ }
466
+ }, {
467
+ name: 'strip out x-mobify-access-key keep apig headers',
468
+ targetHost: 'www.customer.com',
469
+ input: {
470
+ 'accept-encoding': 'deflate, gzip',
471
+ cookie: 'abc=123',
472
+ 'x-api-key': '1234567890',
473
+ 'x-mobify-access-key': 'abcdefghijk',
474
+ 'x-apigateway-event': '{}',
475
+ 'x-apigateway-context': '{}',
476
+ 'x-sfdc-access-control': '123456789'
477
+ },
478
+ expected: {
479
+ 'accept-encoding': 'deflate, gzip',
480
+ cookie: 'abc=123',
481
+ 'x-api-key': '1234567890',
482
+ 'x-mobify-access-key': undefined,
483
+ 'x-apigateway-event': '{}',
484
+ 'x-apigateway-context': '{}',
485
+ 'x-sfdc-access-control': undefined
486
+ }
487
+ }, {
488
+ name: 'caching-proxy processing GET 1',
489
+ caching: true,
490
+ targetHost: 'www.customer.com',
491
+ testAllowlist: true,
492
+ method: 'GET',
493
+ input: {
494
+ 'accept-encoding': 'deflate, gzip',
495
+ authorization: 'abc=123',
496
+ connection: 'keep-alive',
497
+ date: 'some-date-value',
498
+ 'user-agent': 'chrome'
499
+ },
500
+ expected: {
501
+ authorization: 'abc=123',
502
+ connection: undefined,
503
+ date: undefined,
504
+ host: 'www.customer.com',
505
+ origin: 'https://www.customer.com',
506
+ 'user-agent': 'Amazon CloudFront'
507
+ }
508
+ }, {
509
+ name: 'caching-proxy processing GET 2',
510
+ caching: true,
511
+ method: 'GET',
512
+ input: {
513
+ 'accept-encoding': 'deflate'
514
+ },
515
+ expected: {
516
+ 'accept-encoding': 'deflate'
517
+ }
518
+ }, {
519
+ name: 'caching-proxy processing no-op',
520
+ caching: true,
521
+ targetHost: 'www.customer.com',
522
+ method: 'GET',
523
+ input: {},
524
+ expected: {
525
+ 'user-agent': 'Amazon CloudFront'
526
+ }
527
+ }, {
528
+ name: 'add in x-headers',
529
+ targetHost: 'www.customer.com',
530
+ input: {
531
+ 'accept-encoding': 'deflate, gzip',
532
+ cookie: 'abc=123'
533
+ },
534
+ expected: {
535
+ 'accept-encoding': 'deflate, gzip',
536
+ cookie: 'abc=123',
537
+ 'x-mobify': 'true'
538
+ }
539
+ }];
540
+ testCases.forEach((testCase, testCaseIndex) =>
541
+ // eslint-disable-next-line jest/valid-title
542
+ test(testCase.name || `test ${testCaseIndex}`, () => {
543
+ const headers = _extends({}, testCase.input || {});
544
+
545
+ // If we're testing the allowlist, add a value for
546
+ // every allowed header.
547
+ if (testCase.testAllowlist) {
548
+ Object.keys(_ssrProxying.ALLOWED_CACHING_PROXY_REQUEST_HEADERS).forEach(key => {
549
+ if (!(key in headers)) {
550
+ headers[key] = key;
551
+ }
552
+ });
553
+ }
554
+ const updatedHeaders = (0, _ssrProxying.rewriteProxyRequestHeaders)({
555
+ caching: testCase.caching,
556
+ headers,
557
+ headerFormat: testCase.format || 'http',
558
+ method: testCase.method || 'GET',
559
+ targetProtocol: testCase.targetProtocol || 'https',
560
+ targetHost: testCase.targetHost,
561
+ logging: true
562
+ });
563
+ const expectedKeys = Object.keys(testCase.expected);
564
+ expectedKeys.forEach(key => {
565
+ const value = testCase.expected[key];
566
+ const actual = updatedHeaders[key];
567
+ expect(actual).toEqual(value);
568
+ });
569
+
570
+ // If we're testing the allowlist, verify that each
571
+ // allowed header that has a corresponding value
572
+ // in testCase.expected matches that expected value.
573
+ if (testCase.testAllowlist) {
574
+ Object.keys(_ssrProxying.ALLOWED_CACHING_PROXY_REQUEST_HEADERS).forEach(key => {
575
+ if (expectedKeys.indexOf(key) < 0) {
576
+ // eslint-disable-next-line jest/no-conditional-expect
577
+ expect(updatedHeaders[key]).toEqual(headers[key]);
578
+ }
579
+ });
580
+ }
581
+ }));
582
+ test('missing headers', () => {
583
+ expect((0, _ssrProxying.rewriteProxyRequestHeaders)({
584
+ appHostname: '',
585
+ proxyPath: '/mobify/proxy/base',
586
+ targetProtocol: 'https',
587
+ targetHost: 'www.customer.com'
588
+ })).toEqual({});
589
+ });
590
+ test('bad Headers format', () => {
591
+ expect(() => new _ssrProxying.Headers({})).toThrow();
592
+ });
593
+ });