@squiz/edge-dev-render-runtime-lib 2.5.2 → 2.5.3

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/lib/index.js CHANGED
@@ -190,8 +190,8 @@ var import_component_lib = require("@squiz/component-lib");
190
190
  var import_dx_common_lib2 = require("@squiz/dx-common-lib");
191
191
 
192
192
  // src/utils/setHeadersOnExpressResponse.ts
193
- function setHeadersOnExpressResponse(headers, response) {
194
- for (const [headerName, headerValue] of Object.entries(headers)) {
193
+ function setHeadersOnExpressResponse(headers3, response) {
194
+ for (const [headerName, headerValue] of Object.entries(headers3)) {
195
195
  if (headerValue) {
196
196
  response.setHeader(headerName, headerValue);
197
197
  } else {
@@ -200,6 +200,9 @@ function setHeadersOnExpressResponse(headers, response) {
200
200
  }
201
201
  }
202
202
 
203
+ // src/webserver/controllers/utils/ReplaceWithMock.ts
204
+ var import_dx_json_schema_lib2 = require("@squiz/dx-json-schema-lib");
205
+
203
206
  // src/webserver/controllers/mocks/mockResources.ts
204
207
  var resources = [
205
208
  {
@@ -1449,6 +1452,12 @@ var resources = [
1449
1452
  ];
1450
1453
 
1451
1454
  // src/webserver/controllers/utils/ReplaceWithMock.ts
1455
+ var typeResolver = import_dx_json_schema_lib2.TypeResolverBuilder.new().addPrimitive(import_dx_json_schema_lib2.SquizImageType).addPrimitive(import_dx_json_schema_lib2.SquizLinkType).addPrimitive(import_dx_json_schema_lib2.FormattedTextType).addResolver(import_dx_json_schema_lib2.SquizImageType, import_dx_json_schema_lib2.MatrixAssetType, ({ matrixAssetId }) => {
1456
+ const resource = resources.find((resource2) => resource2.id == matrixAssetId);
1457
+ return {
1458
+ ...resource["squizImage"]
1459
+ };
1460
+ }).build();
1452
1461
  function hasSquizType(componentSchema) {
1453
1462
  if (!componentSchema || typeof componentSchema !== "object") {
1454
1463
  return false;
@@ -1461,7 +1470,7 @@ function hasSquizType(componentSchema) {
1461
1470
  (prop) => prop.type === "SquizImage" || prop.type === "SquizLink"
1462
1471
  );
1463
1472
  }
1464
- function replaceWithMock(componentSchema, data) {
1473
+ async function resolveMock(componentSchema, data) {
1465
1474
  if (!componentSchema || typeof componentSchema !== "object") {
1466
1475
  return componentSchema;
1467
1476
  }
@@ -1469,19 +1478,9 @@ function replaceWithMock(componentSchema, data) {
1469
1478
  if (!inputSchema || typeof inputSchema !== "object" || !inputSchema.properties) {
1470
1479
  return componentSchema;
1471
1480
  }
1472
- const squizImageKeys = Object.keys(inputSchema.properties).filter(
1473
- (key) => inputSchema.properties[key].type === "SquizImage"
1474
- );
1475
- squizImageKeys.forEach((key) => {
1476
- if (data[key]) {
1477
- const resourceToReplace = resources.find((resource) => resource.id = data[key]["matrixAssetId"]);
1478
- data = {
1479
- ...data,
1480
- [key]: resourceToReplace
1481
- };
1482
- }
1483
- });
1484
- return data;
1481
+ const jsonSchemaService = new import_dx_json_schema_lib2.JSONSchemaService(typeResolver, import_dx_json_schema_lib2.ComponentInputMetaSchema);
1482
+ const resolvedInput = await jsonSchemaService.resolveInput(data, inputSchema);
1483
+ return resolvedInput;
1485
1484
  }
1486
1485
 
1487
1486
  // src/webserver/controllers/DevelopmentController.ts
@@ -1520,7 +1519,7 @@ var DevelopmentController = class extends import_tsoa3.Controller {
1520
1519
  });
1521
1520
  return schemaResult;
1522
1521
  }
1523
- async TemporaryPreview(req, componentId, functionName, headers, envVars, data, contextMatrixAssetId, contextMatrixUrl) {
1522
+ async TemporaryPreview(req, componentId, functionName, headers3, envVars, data, contextMatrixAssetId, contextMatrixUrl) {
1524
1523
  this.checkDevMode();
1525
1524
  const { namespace, componentName, version } = getComponentInfoFromComponentId(componentId);
1526
1525
  const res = req.res;
@@ -1533,7 +1532,7 @@ var DevelopmentController = class extends import_tsoa3.Controller {
1533
1532
  const componentManifest = await this.manifestService.loadManifest(namespace, componentName, version);
1534
1533
  const componentFunction = componentManifest.getComponentFunctionByName(functionName);
1535
1534
  if (hasSquizType(componentFunction)) {
1536
- data = replaceWithMock(componentFunction, data);
1535
+ data = await resolveMock(componentFunction, data);
1537
1536
  }
1538
1537
  const defaultPreviewKey = Object.keys(componentManifest.getModel()?.previews ?? {}).at(0) ?? void 0;
1539
1538
  const componentTask = {
@@ -1555,7 +1554,7 @@ var DevelopmentController = class extends import_tsoa3.Controller {
1555
1554
  matrixUrl: contextMatrixUrl
1556
1555
  };
1557
1556
  let result = componentManifest.type === "edge" ? await this.componentRunnerService.executeEdgeComponentFunction(componentTask, ctx) : await this.componentRunnerService.executeServerComponentFunction(componentTask);
1558
- const responseHeaders = headers ?? {};
1557
+ const responseHeaders = headers3 ?? {};
1559
1558
  switch (componentFunction.output.responseType) {
1560
1559
  case "html":
1561
1560
  responseHeaders["content-type"] = "text/html";
@@ -1659,16 +1658,16 @@ var DevelopmentController = class extends import_tsoa3.Controller {
1659
1658
  return result;
1660
1659
  }
1661
1660
  setHeadersOnResponse(renderInput, response) {
1662
- const headers = renderInput.headers || {};
1661
+ const headers3 = renderInput.headers || {};
1663
1662
  switch (renderInput.componentFunction.output.responseType) {
1664
1663
  case "html":
1665
- headers["content-type"] = "text/html";
1664
+ headers3["content-type"] = "text/html";
1666
1665
  break;
1667
1666
  case "json":
1668
- headers["content-type"] = "application/json";
1667
+ headers3["content-type"] = "application/json";
1669
1668
  break;
1670
1669
  }
1671
- setHeadersOnExpressResponse(headers, response);
1670
+ setHeadersOnExpressResponse(headers3, response);
1672
1671
  let cacheControl = response.getHeader("cache-control");
1673
1672
  if (typeof cacheControl !== "string") {
1674
1673
  cacheControl = void 0;
@@ -1763,8 +1762,8 @@ var StaticController = class extends import_tsoa4.Controller {
1763
1762
  const componentStaticFileRoot = manifestModel.staticFiles.locationRoot;
1764
1763
  const manifestPath = await this.manifestService.getManifestPath(namespace, componentName, version);
1765
1764
  const staticFilePathRoot = import_path.default.join(import_path.default.dirname(manifestPath), componentStaticFileRoot);
1766
- const headers = await this.getHeadersForInputType(_componentSet, _previewKey, manifest);
1767
- this.setResponseHeaders(headers, response);
1765
+ const headers3 = await this.getHeadersForInputType(_componentSet, _previewKey, manifest);
1766
+ this.setResponseHeaders(headers3, response);
1768
1767
  response.sendFile(
1769
1768
  staticFile,
1770
1769
  {
@@ -1803,8 +1802,8 @@ var StaticController = class extends import_tsoa4.Controller {
1803
1802
  throw new import_dx_common_lib3.ResourceNotFoundError(`${componentName} ${version} doesn't expose any static resources`);
1804
1803
  }
1805
1804
  }
1806
- setResponseHeaders(headers, response) {
1807
- setHeadersOnExpressResponse(headers, response);
1805
+ setResponseHeaders(headers3, response) {
1806
+ setHeadersOnExpressResponse(headers3, response);
1808
1807
  let cacheControl = response.getHeader("cache-control");
1809
1808
  if (typeof cacheControl != "string") {
1810
1809
  cacheControl = import_dx_common_lib3.DEFAULT_CACHE_CONTROL_HEADER;
@@ -1823,8 +1822,8 @@ var StaticController = class extends import_tsoa4.Controller {
1823
1822
  if (_previewKey) {
1824
1823
  const previewDefinition = await this.getValidatedPreviewDefinition(_previewKey, manifest.getModel());
1825
1824
  const functionPreview = previewDefinition.functionData[manifest.getModel().mainFunction];
1826
- const headers = functionPreview?.headers ? functionPreview.headers : {};
1827
- return headers;
1825
+ const headers3 = functionPreview?.headers ? functionPreview.headers : {};
1826
+ return headers3;
1828
1827
  }
1829
1828
  throw new import_dx_common_lib3.BadRequestError(`neither componentSet or previewKey set`);
1830
1829
  }
@@ -2559,20 +2558,20 @@ function RegisterRoutes(app) {
2559
2558
  function promiseHandler(controllerObj, promise, response, successStatus, next) {
2560
2559
  return Promise.resolve(promise).then((data) => {
2561
2560
  let statusCode = successStatus;
2562
- let headers;
2561
+ let headers3;
2563
2562
  if (isController(controllerObj)) {
2564
- headers = controllerObj.getHeaders();
2563
+ headers3 = controllerObj.getHeaders();
2565
2564
  statusCode = controllerObj.getStatus() || statusCode;
2566
2565
  }
2567
- returnHandler(response, statusCode, data, headers);
2566
+ returnHandler(response, statusCode, data, headers3);
2568
2567
  }).catch((error) => next(error));
2569
2568
  }
2570
- function returnHandler(response, statusCode, data, headers = {}) {
2569
+ function returnHandler(response, statusCode, data, headers3 = {}) {
2571
2570
  if (response.headersSent) {
2572
2571
  return;
2573
2572
  }
2574
- Object.keys(headers).forEach((name) => {
2575
- response.set(name, headers[name]);
2573
+ Object.keys(headers3).forEach((name) => {
2574
+ response.set(name, headers3[name]);
2576
2575
  });
2577
2576
  if (data && typeof data.pipe === "function" && data.readable && typeof data._read === "function") {
2578
2577
  response.status(statusCode || 200);
@@ -2584,8 +2583,8 @@ function RegisterRoutes(app) {
2584
2583
  }
2585
2584
  }
2586
2585
  function responder(response) {
2587
- return function(status, data, headers) {
2588
- returnHandler(response, status, data, headers);
2586
+ return function(status, data, headers3) {
2587
+ returnHandler(response, status, data, headers3);
2589
2588
  };
2590
2589
  }
2591
2590
  ;
@@ -3040,7 +3039,7 @@ var RenderInputService = class {
3040
3039
  request.expressRequest.query["_previewEnvVars"]
3041
3040
  );
3042
3041
  const publicFacingUrl = this.rootUrlResolver(request.expressRequest, true);
3043
- let headers = {};
3042
+ let headers3 = {};
3044
3043
  let webManifest;
3045
3044
  let input = {};
3046
3045
  if (request.previewKey && request.componentSet) {
@@ -3055,14 +3054,14 @@ var RenderInputService = class {
3055
3054
  );
3056
3055
  const previewFunctionConfig = manifest.getFunctionPreviewConfiguration(request.previewKey, componentFunction);
3057
3056
  if (previewFunctionConfig && previewFunctionConfig.headers) {
3058
- headers = previewFunctionConfig.headers;
3057
+ headers3 = previewFunctionConfig.headers;
3059
3058
  }
3060
3059
  } else {
3061
3060
  if (!request.componentSet) {
3062
3061
  throw new import_dx_common_lib6.BadRequestError("You must provide either previewKey or a componentSet as input");
3063
3062
  }
3064
3063
  const set = await this.componentSetService.getComponentSet(request.componentSet);
3065
- headers = this.componentSetService.getComponentRuntimeHeaders(set, componentFunction);
3064
+ headers3 = this.componentSetService.getComponentRuntimeHeaders(set, componentFunction);
3066
3065
  webManifest = this.componentFunctionService.prepareManifestForWebResponse(
3067
3066
  manifest,
3068
3067
  runtimeSet.webPath,
@@ -3081,7 +3080,7 @@ var RenderInputService = class {
3081
3080
  manifest,
3082
3081
  manifestForWeb: webManifest,
3083
3082
  componentFullName: manifest.getName(),
3084
- headers,
3083
+ headers: headers3,
3085
3084
  runtimeSet,
3086
3085
  manifestPath: await manifest.getManifestPath(),
3087
3086
  queryParameters: this.getQueryParametersFromRequest(request, componentFunction)
@@ -3394,26 +3393,25 @@ function generateRandomName() {
3394
3393
  }
3395
3394
 
3396
3395
  // src/webserver/controllers/test/render-route-tests.ts
3396
+ var CLOUDFLARE_X_API_KEY_HEADER = "x-cloudflare-key";
3397
+ var CLOUDFLARE_X_API_KEY = "x-cloudflare-key";
3398
+ var headers = {
3399
+ [CLOUDFLARE_X_API_KEY_HEADER]: CLOUDFLARE_X_API_KEY
3400
+ };
3397
3401
  function renderRouteTests(request, _rootUrl) {
3398
3402
  describe("GET /dev/render/unit-test-components__test-component__1.0.10/?_componentSet=set", () => {
3399
3403
  it("should render the test component with html endpoint", async () => {
3400
- const response = await request().get(
3401
- "/dev/render/unit-test-components__test-component__1.0.10?_componentSet=set&something=unused"
3402
- );
3404
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.10?_componentSet=set&something=unused").set(headers);
3403
3405
  expect(response.text).toEqual("<div>hello - {}</div>");
3404
3406
  expect(response.statusCode).toEqual(200);
3405
3407
  }, 2e4);
3406
3408
  it("should render the test component promise endpoint with html endpoint", async () => {
3407
- const response = await request().get(
3408
- "/dev/render/unit-test-components__test-component__1.0.10/promise?_componentSet=set&something=unused"
3409
- );
3409
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.10/promise?_componentSet=set&something=unused").set(headers);
3410
3410
  expect(response.statusCode).toEqual(200);
3411
3411
  expect(response.text).toEqual("<h1>hello</h1>");
3412
3412
  }, 2e4);
3413
3413
  it("should fail validation if input contract is not met and helpful message returned", async () => {
3414
- const response = await request().get(
3415
- "/dev/render/unit-test-components__test-component__1.0.0?_componentSet=set&something=unused"
3416
- );
3414
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.0?_componentSet=set&something=unused").set(headers);
3417
3415
  expect(response.statusCode).toEqual(400);
3418
3416
  expect(response.body).toMatchInlineSnapshot(`
3419
3417
  {
@@ -3424,7 +3422,7 @@ function renderRouteTests(request, _rootUrl) {
3424
3422
  it("should 404 if requesting a non-existent function on an otherwise valid component", async () => {
3425
3423
  const response = await request().get(
3426
3424
  "/dev/render/unit-test-components__test-component__1.0.0/non-existent-entry-file?_componentSet=set&something=not-used"
3427
- );
3425
+ ).set(headers);
3428
3426
  expect(response.statusCode).toEqual(404);
3429
3427
  expect(response.body).toMatchInlineSnapshot(`
3430
3428
  {
@@ -3435,9 +3433,7 @@ function renderRouteTests(request, _rootUrl) {
3435
3433
  });
3436
3434
  describe("GET /dev/render/unit-test-components__test-component__1.0.3/main?_componentSet=set", () => {
3437
3435
  it("should fail validation when requesting a function with a missing entry file", async () => {
3438
- const response = await request().get(
3439
- "/dev/render/unit-test-components__test-component__1.0.3/main?_componentSet=set&something=not-used"
3440
- );
3436
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.3/main?_componentSet=set&something=not-used").set(headers);
3441
3437
  expect(response.body).toEqual({
3442
3438
  message: `"main's" entry file "missing-entry-file.js" is inaccessible`
3443
3439
  });
@@ -3446,7 +3442,7 @@ function renderRouteTests(request, _rootUrl) {
3446
3442
  });
3447
3443
  describe("GET /dev/render/unit-test-components__test-component__1.0.4?_componentSet=set", () => {
3448
3444
  it("should render a simple json endpoint", async () => {
3449
- const response = await request().get("/dev/render/unit-test-components__test-component__1.0.4?_componentSet=set");
3445
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.4?_componentSet=set").set(headers);
3450
3446
  expect(response.headers["content-type"]).toContain("application/json");
3451
3447
  expect(response.body).toEqual({
3452
3448
  "my-prop": "test value for my-prop",
@@ -3459,9 +3455,7 @@ function renderRouteTests(request, _rootUrl) {
3459
3455
  });
3460
3456
  }, 2e4);
3461
3457
  it("should report error if response does not match with the schema", async () => {
3462
- const response = await request().get(
3463
- "/dev/render/unit-test-components__test-component__1.0.4/render_json_invalid?_componentSet=set"
3464
- );
3458
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.4/render_json_invalid?_componentSet=set").set(headers);
3465
3459
  expect(response.statusCode).toEqual(500);
3466
3460
  expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8");
3467
3461
  expect(response.body).toEqual({
@@ -3471,7 +3465,7 @@ function renderRouteTests(request, _rootUrl) {
3471
3465
  it("should validate a complex json against a schema", async () => {
3472
3466
  const response = await request().get(
3473
3467
  "/dev/render/unit-test-components__test-component__1.0.4/render_json_nested_object_valid?_componentSet=set"
3474
- );
3468
+ ).set(headers);
3475
3469
  const myObject = {
3476
3470
  "my-object": {
3477
3471
  "a-string": "this is a string",
@@ -3487,7 +3481,7 @@ function renderRouteTests(request, _rootUrl) {
3487
3481
  it("should report errors if nested object json schema fails", async () => {
3488
3482
  const response = await request().get(
3489
3483
  "/dev/render/unit-test-components__test-component__1.0.4/render_json_nested_object_invalid?_componentSet=set"
3490
- );
3484
+ ).set(headers);
3491
3485
  expect(response.statusCode).toEqual(500);
3492
3486
  expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8");
3493
3487
  expect(response.body).toEqual({
@@ -3500,7 +3494,7 @@ Expected given value \`N\` in #/my-object/a-nested-object/an-enum\` to be one of
3500
3494
  it('should return input and info object with "query parameters"', async () => {
3501
3495
  const response = await request().get(
3502
3496
  "/dev/render/unit-test-components__test-component__1.0.7?_componentSet=set&something=hello-test&foo=foo-val&foo2=foo2-val"
3503
- );
3497
+ ).set(headers);
3504
3498
  expect(response.headers["content-type"]).toContain("application/json");
3505
3499
  const result = response.body;
3506
3500
  expect(result).toEqual([
@@ -3578,18 +3572,14 @@ Expected given value \`N\` in #/my-object/a-nested-object/an-enum\` to be one of
3578
3572
  });
3579
3573
  });
3580
3574
  it("should throw error if required query parameter is missing", async () => {
3581
- const response = await request().get(
3582
- "/dev/render/unit-test-components__test-component__1.0.7?_componentSet=set&foo2=foo2-val"
3583
- );
3575
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.7?_componentSet=set&foo2=foo2-val").set(headers);
3584
3576
  expect(response.body).toEqual({
3585
3577
  message: "Required query parameter 'foo' is missing"
3586
3578
  });
3587
3579
  expect(response.statusCode).toEqual(400);
3588
3580
  });
3589
3581
  it("should throw error if query parameter is not a string", async () => {
3590
- const response = await request().get(
3591
- "/dev/render/unit-test-components__test-component__1.0.7?_componentSet=set&foo=123&foo2[]=array-val"
3592
- );
3582
+ const response = await request().get("/dev/render/unit-test-components__test-component__1.0.7?_componentSet=set&foo=123&foo2[]=array-val").set(headers);
3593
3583
  expect(response.body).toEqual({
3594
3584
  message: "Value for query parameter 'foo2' must be a string"
3595
3585
  });
@@ -3597,60 +3587,48 @@ Expected given value \`N\` in #/my-object/a-nested-object/an-enum\` to be one of
3597
3587
  });
3598
3588
  describe("GET /dev/render/unit-test-components__test-headers__1.0.3?_componentSet=set", () => {
3599
3589
  it("should make sure max-age header is clamped between max value", async () => {
3600
- const response = await request().get("/dev/render/unit-test-components__test-headers__1.0.3/max_value?_componentSet=set").send().expect(200, "rendered");
3590
+ const response = await request().get("/dev/render/unit-test-components__test-headers__1.0.3/max_value?_componentSet=set").set({ "x-cloudflare-key": CLOUDFLARE_X_API_KEY }).send().expect(200, "rendered");
3601
3591
  expect(response.headers["cache-control"]).toEqual("max-age=1800, immutable");
3602
3592
  });
3603
3593
  it("should make sure max-age header is clamped between min value", async () => {
3604
- const response = await request().get("/dev/render/unit-test-components__test-headers__1.0.3/min_value?_componentSet=set").send().expect(200, "rendered");
3594
+ const response = await request().get("/dev/render/unit-test-components__test-headers__1.0.3/min_value?_componentSet=set").set({ "x-cloudflare-key": CLOUDFLARE_X_API_KEY }).send().expect(200, "rendered");
3605
3595
  expect(response.headers["cache-control"]).toEqual("max-age=30, public");
3606
3596
  });
3607
3597
  });
3608
3598
  describe("should be able to load external dependencies", () => {
3609
3599
  it("should be able to make web requests out", async () => {
3610
- const response = await request().get(
3611
- "/dev/render/unit-test-components__test-npm-modules__3.0.0?_componentSet=set"
3612
- );
3600
+ const response = await request().get("/dev/render/unit-test-components__test-npm-modules__3.0.0?_componentSet=set").set(headers);
3613
3601
  expect(response.statusCode).toEqual(200);
3614
3602
  expect(response.text).toContain(`<h1>Example Domain</h1>`);
3615
3603
  });
3616
3604
  });
3617
3605
  describe("GET /dev/render/unit-test-components__test-formatted-text__1.0.0", () => {
3618
3606
  it("should render a component formattedText in preview inline", async () => {
3619
- const response = await request().get(
3620
- "/dev/render/unit-test-components__test-formatted-text__1.0.0?_previewKey=valid-inline-preview"
3621
- );
3607
+ const response = await request().get("/dev/render/unit-test-components__test-formatted-text__1.0.0?_previewKey=valid-inline-preview").set(headers);
3622
3608
  expect(response.statusCode).toEqual(200);
3623
3609
  expect(response.text).toMatchInlineSnapshot(`"This is a test"`);
3624
3610
  });
3625
3611
  });
3626
3612
  describe("GET /dev/render/unit-test-components__test-npm-modules__3.1.0", () => {
3627
3613
  it("should render a component with uuid", async () => {
3628
- const response = await request().get(
3629
- "/dev/render/unit-test-components__test-npm-modules__3.1.0/uuid?_componentSet=set"
3630
- );
3614
+ const response = await request().get("/dev/render/unit-test-components__test-npm-modules__3.1.0/uuid?_componentSet=set").set(headers);
3631
3615
  expect(response.statusCode).toEqual(200);
3632
3616
  });
3633
3617
  it("should render a component with Buffer", async () => {
3634
- const response = await request().get(
3635
- "/dev/render/unit-test-components__test-npm-modules__3.1.0/buffer?_componentSet=set"
3636
- );
3618
+ const response = await request().get("/dev/render/unit-test-components__test-npm-modules__3.1.0/buffer?_componentSet=set").set(headers);
3637
3619
  expect(response.statusCode).toEqual(200);
3638
3620
  });
3639
3621
  });
3640
3622
  describe("GET /dev/render/unit-test-components__test-squiz-image__1.0.0", () => {
3641
3623
  it("should render the preview", async () => {
3642
- const response = await request().get(
3643
- "/dev/render/unit-test-components__test-squiz-image__1.0.0?_previewKey=valid-inline-preview"
3644
- );
3624
+ const response = await request().get("/dev/render/unit-test-components__test-squiz-image__1.0.0?_previewKey=valid-inline-preview").set(headers);
3645
3625
  expect(response.statusCode).toEqual(200);
3646
3626
  expect(response.text).toMatchInlineSnapshot(`"My Image Component"`);
3647
3627
  });
3648
3628
  });
3649
3629
  describe("GET /dev/render/unit-test-components__test-squiz-link__1.0.0", () => {
3650
3630
  it("should render the preview", async () => {
3651
- const response = await request().get(
3652
- "/dev/render/unit-test-components__test-squiz-link__1.0.0?_previewKey=valid-inline-preview"
3653
- );
3631
+ const response = await request().get("/dev/render/unit-test-components__test-squiz-link__1.0.0?_previewKey=valid-inline-preview").set(headers);
3654
3632
  expect(response.statusCode).toEqual(200);
3655
3633
  expect(response.text).toMatchInlineSnapshot(`"{"link":{"text":"My Link","url":"https://some.link/foo"}}"`);
3656
3634
  });
@@ -3658,16 +3636,21 @@ Expected given value \`N\` in #/my-object/a-nested-object/an-enum\` to be one of
3658
3636
  }
3659
3637
 
3660
3638
  // src/webserver/controllers/test/preview-route-tests.ts
3639
+ var CLOUDFLARE_X_API_KEY_HEADER2 = "x-cloudflare-key";
3640
+ var CLOUDFLARE_X_API_KEY2 = "x-cloudflare-key";
3641
+ var headers2 = {
3642
+ [CLOUDFLARE_X_API_KEY_HEADER2]: CLOUDFLARE_X_API_KEY2
3643
+ };
3661
3644
  function previewRouteTests(request, _rootUrl = "${rootUrl}") {
3662
3645
  describe("preview routes", () => {
3663
3646
  it("should render a preview of a component with no wrapper file", async () => {
3664
- await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4?_previewKey=test-preview").expect(200).expect("content-type", "text/html; charset=utf-8").expect("800 | this is nested content");
3647
+ await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4?_previewKey=test-preview").set(headers2).expect(200).expect("content-type", "text/html; charset=utf-8").expect("800 | this is nested content");
3665
3648
  });
3666
3649
  it("should render a preview of a component with a different preset", async () => {
3667
- await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4?_previewKey=another-preview").expect(200).expect("content-type", "text/html; charset=utf-8").expect("12300 | this is also nested content");
3650
+ await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4?_previewKey=another-preview").set(headers2).expect(200).expect("content-type", "text/html; charset=utf-8").expect("12300 | this is also nested content");
3668
3651
  });
3669
3652
  it("should render a preview of a component non-main function with a wrapper html file", async () => {
3670
- await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4/other?_previewKey=another-preview").expect(200).expect("content-type", "text/html; charset=utf-8").expect((a) => {
3653
+ await request().get("/dev/render/unit-test-components__test-preview-component__1.2.4/other?_previewKey=another-preview").set(headers2).expect(200).expect("content-type", "text/html; charset=utf-8").expect((a) => {
3671
3654
  expect(a.text).toMatchInlineSnapshot(`
3672
3655
  "<!DOCTYPE html>
3673
3656
  <html lang="en">
@@ -3686,30 +3669,24 @@ function previewRouteTests(request, _rootUrl = "${rootUrl}") {
3686
3669
  });
3687
3670
  });
3688
3671
  it("should load the defined preview for a given preview key", async () => {
3689
- const response = await request().get(
3690
- "/dev/render/unit-test-components__test-preview-component__1.0.3?_previewKey=second-preview"
3691
- );
3672
+ const response = await request().get("/dev/render/unit-test-components__test-preview-component__1.0.3?_previewKey=second-preview").set(headers2);
3692
3673
  expect(response.text).toContain(`<p>Second Preview</p>`);
3693
3674
  expect(response.statusCode).toEqual(200);
3694
3675
  });
3695
3676
  it("should give meaningful error when preview key is invalid", async () => {
3696
- const response = await request().get(
3697
- "/dev/render/unit-test-components__test-preview-component__1.0.3?_previewKey=invalid"
3698
- );
3677
+ const response = await request().get("/dev/render/unit-test-components__test-preview-component__1.0.3?_previewKey=invalid").set(headers2);
3699
3678
  expect(response.text).toEqual(
3700
3679
  `{"message":"Component has not defined preview with key \\"invalid\\", the defined preview keys for this component are \\"first-preview, second-preview, test-preview\\""}`
3701
3680
  );
3702
3681
  expect(response.statusCode).toEqual(404);
3703
3682
  });
3704
3683
  it("should render inline preview data through to component", async () => {
3705
- const response = await request().get(
3706
- "/dev/render/unit-test-components__test-preview-component__1.0.4?_previewKey=test-preview"
3707
- );
3684
+ const response = await request().get("/dev/render/unit-test-components__test-preview-component__1.0.4?_previewKey=test-preview").set(headers2);
3708
3685
  expect(response.text).toContain(`this is a result | 123`);
3709
3686
  expect(response.statusCode).toEqual(200);
3710
3687
  }, 1e4);
3711
3688
  it("should resolve URIs to mocked URI object in preview route", async () => {
3712
- await request().get("/dev/render/unit-test-components__test-preview-component__1.1.1?_previewKey=test-preview").expect(200).expect((a) => {
3689
+ await request().get("/dev/render/unit-test-components__test-preview-component__1.1.1?_previewKey=test-preview").set(headers2).expect(200).expect((a) => {
3713
3690
  expect(a.text).toMatchInlineSnapshot(`
3714
3691
  "
3715
3692
  inline:{"id":"id-inline","type":"root_folder","type_name":"Root Folder","version":"0.0.1","name":"Root Folder","short_name":"/","status":{"id":2,"code":"under_construction"},"created":{"date":"2020-02-27T09:47:27+11:00","user_id":null},"updated":{"date":"2020-02-27T09:47:27+11:00","user_id":""},"published":{"date":"2020-02-27T09:47:27+11:00","user_id":"null"},"status_changed":{"date":"2020-02-27T09:47:27+11:00","user_id":"null"},"url":"xxx","urls":[],"attributes":{"test":"test"},"additional":{}}