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