|
| 1 | +import { WorkaroundCachedResponse } from "../utils/dont-cache-responses-in-browser" |
| 2 | + |
| 3 | +describe("Headers", () => { |
| 4 | + const defaultHeaders = { |
| 5 | + "x-xss-protection": "1; mode=block", |
| 6 | + "x-content-type-options": "nosniff", |
| 7 | + "referrer-policy": "same-origin", |
| 8 | + "x-frame-options": "DENY", |
| 9 | + } |
| 10 | + |
| 11 | + // DRY for repeated assertions in multple tests |
| 12 | + const expectedHeadersByRouteAlias = { |
| 13 | + "@app-data": { |
| 14 | + ...defaultHeaders, |
| 15 | + "cache-control": "public,max-age=0,must-revalidate", |
| 16 | + }, |
| 17 | + "@page-data": { |
| 18 | + ...defaultHeaders, |
| 19 | + "cache-control": "public,max-age=0,must-revalidate", |
| 20 | + }, |
| 21 | + "@slice-data": { |
| 22 | + ...defaultHeaders, |
| 23 | + "cache-control": "public,max-age=0,must-revalidate", |
| 24 | + }, |
| 25 | + "@static-query-result": { |
| 26 | + ...defaultHeaders, |
| 27 | + "cache-control": "public,max-age=0,must-revalidate", |
| 28 | + }, |
| 29 | + "@img-webpack-import": { |
| 30 | + ...defaultHeaders, |
| 31 | + "cache-control": "public,max-age=31536000,immutable", |
| 32 | + }, |
| 33 | + "@js": { |
| 34 | + ...defaultHeaders, |
| 35 | + "cache-control": "public,max-age=31536000,immutable", |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // `ntl serve` and actual deploy seem to have possible slight differences around header value formatting |
| 40 | + // so this just remove spaces around commas to make it easier to compare |
| 41 | + function normalizeHeaderValue(value: string | undefined): string | undefined { |
| 42 | + if (typeof value === "undefined") { |
| 43 | + return value |
| 44 | + } |
| 45 | + // Remove spaces around commas |
| 46 | + return value.replace(/\s*,\s*/gm, `,`) |
| 47 | + } |
| 48 | + function checkHeaders( |
| 49 | + routeAlias: string, |
| 50 | + expectedHeaders?: Record<string, string> |
| 51 | + ) { |
| 52 | + if (!expectedHeaders) { |
| 53 | + expectedHeaders = expectedHeadersByRouteAlias[routeAlias] |
| 54 | + } |
| 55 | + |
| 56 | + if (!expectedHeaders) { |
| 57 | + throw new Error(`No expected headers provided for "${routeAlias}`) |
| 58 | + } |
| 59 | + |
| 60 | + cy.wait(routeAlias).then(interception => { |
| 61 | + Object.keys(expectedHeaders).forEach(headerKey => { |
| 62 | + const headers = interception.response.headers[headerKey] |
| 63 | + |
| 64 | + const firstHeader: string = Array.isArray(headers) |
| 65 | + ? headers[0] |
| 66 | + : headers |
| 67 | + |
| 68 | + expect(normalizeHeaderValue(firstHeader)).to.eq( |
| 69 | + normalizeHeaderValue(expectedHeaders[headerKey]) |
| 70 | + ) |
| 71 | + }) |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + cy.intercept("/", WorkaroundCachedResponse).as("index") |
| 77 | + cy.intercept("routes/ssr/static", WorkaroundCachedResponse).as("ssr") |
| 78 | + cy.intercept("routes/dsg/static", WorkaroundCachedResponse).as("dsg") |
| 79 | + |
| 80 | + cy.intercept("**/page-data.json", WorkaroundCachedResponse).as("page-data") |
| 81 | + cy.intercept("**/app-data.json", WorkaroundCachedResponse).as("app-data") |
| 82 | + cy.intercept("**/slice-data/*.json", WorkaroundCachedResponse).as( |
| 83 | + "slice-data" |
| 84 | + ) |
| 85 | + cy.intercept("**/page-data/sq/d/*.json", WorkaroundCachedResponse).as( |
| 86 | + "static-query-result" |
| 87 | + ) |
| 88 | + |
| 89 | + cy.intercept("/static/astro-**.png", WorkaroundCachedResponse).as( |
| 90 | + "img-webpack-import" |
| 91 | + ) |
| 92 | + cy.intercept("*.js", WorkaroundCachedResponse).as("js") |
| 93 | + }) |
| 94 | + |
| 95 | + it("should contain correct headers for index page", () => { |
| 96 | + cy.visit("/").waitForRouteChange() |
| 97 | + |
| 98 | + checkHeaders("@index", { |
| 99 | + ...defaultHeaders, |
| 100 | + "x-custom-header": "my custom header value", |
| 101 | + "cache-control": "public,max-age=0,must-revalidate", |
| 102 | + }) |
| 103 | + |
| 104 | + checkHeaders("@app-data") |
| 105 | + checkHeaders("@page-data") |
| 106 | + checkHeaders("@slice-data") |
| 107 | + checkHeaders("@static-query-result") |
| 108 | + |
| 109 | + // index page is only one showing webpack imported image |
| 110 | + checkHeaders("@img-webpack-import") |
| 111 | + checkHeaders("@js") |
| 112 | + }) |
| 113 | + |
| 114 | + it("should contain correct headers for ssr page", () => { |
| 115 | + cy.visit("routes/ssr/static").waitForRouteChange() |
| 116 | + |
| 117 | + checkHeaders("@ssr", { |
| 118 | + ...defaultHeaders, |
| 119 | + "x-custom-header": "my custom header value", |
| 120 | + "x-ssr-header": "my custom header value from config", |
| 121 | + "x-ssr-header-getserverdata": "my custom header value from getServerData", |
| 122 | + "x-ssr-header-overwrite": "getServerData wins", |
| 123 | + }) |
| 124 | + |
| 125 | + checkHeaders("@app-data") |
| 126 | + // page-data is baked into SSR page so it's not fetched and we don't assert it |
| 127 | + checkHeaders("@slice-data") |
| 128 | + checkHeaders("@static-query-result") |
| 129 | + checkHeaders("@js") |
| 130 | + }) |
| 131 | + |
| 132 | + it("should contain correct headers for dsg page", () => { |
| 133 | + cy.visit("routes/dsg/static").waitForRouteChange() |
| 134 | + |
| 135 | + checkHeaders("@dsg", { |
| 136 | + ...defaultHeaders, |
| 137 | + "x-custom-header": "my custom header value", |
| 138 | + "x-dsg-header": "my custom header value", |
| 139 | + }) |
| 140 | + |
| 141 | + checkHeaders("@app-data") |
| 142 | + checkHeaders("@page-data") |
| 143 | + checkHeaders("@slice-data") |
| 144 | + checkHeaders("@static-query-result") |
| 145 | + checkHeaders("@js") |
| 146 | + }) |
| 147 | +}) |
0 commit comments