-
Notifications
You must be signed in to change notification settings - Fork 60.7k
/
Copy pathstatic-assets.js
186 lines (172 loc) · 5.72 KB
/
static-assets.js
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
185
186
import nock from 'nock'
import { expect, jest } from '@jest/globals'
import { checkCachingHeaders } from '../helpers/caching-headers.js'
import { setDefaultFastlySurrogateKey } from '../../middleware/set-fastly-surrogate-key.js'
import archivedEnterpriseVersionsAssets from '../../middleware/archived-enterprise-versions-assets.js'
function mockRequest(path, { headers }) {
const _headers = Object.fromEntries(
Object.entries(headers || {}).map(([key, value]) => [key.toLowerCase(), value])
)
return {
path,
url: path,
get: (header) => {
return _headers[header.toLowerCase()]
},
set: (header, value) => {
_headers[header.toLowerCase()] = value
},
headers,
}
}
const mockResponse = () => {
const res = {}
res.status = 404
res.statusCode = 404
res.json = (payload) => {
res._json = payload
}
res.send = (body) => {
res.status = 200
res.statusCode = 200
res._send = body
}
res.headers = {}
res.set = (key, value) => {
if (typeof key === 'string') {
res.headers[key.toLowerCase()] = value
} else {
for (const [k, value] of Object.entries(key)) {
res.headers[k.toLowerCase()] = value
}
}
}
res.removeHeader = (key) => {
delete res.headers[key]
}
res.hasHeader = (key) => {
return key in res.headers
}
return res
}
describe('archived enterprise static assets', () => {
// Sometimes static assets are proxied. The URL for the static asset
// might not indicate it's based on archived enterprise version.
jest.setTimeout(60 * 1000)
beforeAll(async () => {
// The first page load takes a long time so let's get it out of the way in
// advance to call out that problem specifically rather than misleadingly
// attributing it to the first test
// await get('/')
const sampleCSS = '/* nice CSS */'
nock('https://github.github.com')
.get('/help-docs-archived-enterprise-versions/2.21/_next/static/foo.css')
.reply(200, sampleCSS, {
'content-type': 'text/css',
'content-length': sampleCSS.length,
})
nock('https://github.github.com')
.get('/help-docs-archived-enterprise-versions/2.21/_next/static/only-on-proxy.css')
.reply(200, sampleCSS, {
'content-type': 'text/css',
'content-length': sampleCSS.length,
})
nock('https://github.github.com')
.get('/help-docs-archived-enterprise-versions/2.3/_next/static/only-on-2.3.css')
.reply(200, sampleCSS, {
'content-type': 'text/css',
'content-length': sampleCSS.length,
})
nock('https://github.github.com')
.get('/help-docs-archived-enterprise-versions/2.3/_next/static/fourofour.css')
.reply(404, 'Not found', {
'content-type': 'text/plain',
})
nock('https://github.github.com')
.get('/help-docs-archived-enterprise-versions/2.3/assets/images/site/logo.png')
.reply(404, 'Not found', {
'content-type': 'text/plain',
})
})
afterAll(() => nock.cleanAll())
it('should proxy if the static asset is prefixed', async () => {
const req = mockRequest('/enterprise/2.21/_next/static/foo.css', {
headers: {
Referrer: '/enterprise/2.21',
},
})
const res = mockResponse()
const next = () => {
throw new Error('did not expect this to ever happen')
}
setDefaultFastlySurrogateKey(req, res, () => {})
await archivedEnterpriseVersionsAssets(req, res, next)
expect(res.statusCode).toBe(200)
checkCachingHeaders(res, false, 60)
})
it('should proxy if the Referrer header indicates so on home page', async () => {
const req = mockRequest('/_next/static/only-on-proxy.css', {
headers: {
Referrer: '/enterprise/2.21',
},
})
const res = mockResponse()
const next = () => {
throw new Error('did not expect this to ever happen')
}
setDefaultFastlySurrogateKey(req, res, () => {})
await archivedEnterpriseVersionsAssets(req, res, next)
expect(res.statusCode).toBe(200)
checkCachingHeaders(res, false, 60)
})
it('should proxy if the Referrer header indicates so on sub-page', async () => {
const req = mockRequest('/_next/static/only-on-2.3.css', {
headers: {
Referrer: '/en/enterprise-server@2.3/some/page',
},
})
const res = mockResponse()
const next = () => {
throw new Error('did not expect this to ever happen')
}
setDefaultFastlySurrogateKey(req, res, () => {})
await archivedEnterpriseVersionsAssets(req, res, next)
expect(res.statusCode).toBe(200)
checkCachingHeaders(res, false, 60)
})
it('might still 404 even with the right referrer', async () => {
const req = mockRequest('/_next/static/fourofour.css', {
headers: {
Referrer: '/en/enterprise-server@2.3/some/page',
},
})
const res = mockResponse()
let nexted = false
const next = () => {
nexted = true
}
setDefaultFastlySurrogateKey(req, res, next)
await archivedEnterpriseVersionsAssets(req, res, next)
expect(res.statusCode).toBe(404)
// It did't exit in that middleware but called next() to move on
// with any other middlewares.
expect(nexted).toBe(true)
})
it('404 on the proxy but actually present here', async () => {
const req = mockRequest('/assets/images/site/logo.png', {
headers: {
Referrer: '/en/enterprise-server@2.3/some/page',
},
})
const res = mockResponse()
let nexted = false
const next = () => {
nexted = true
}
setDefaultFastlySurrogateKey(req, res, () => {})
await archivedEnterpriseVersionsAssets(req, res, next)
// It tried to go via the proxy, but it wasn't there, but then it
// tried "our disk" and it's eventually there.
expect(nexted).toBe(true)
})
})