Skip to content

Commit f1d4b2a

Browse files
authored
Merge pull request github#18670 from github/repo-sync
repo sync
2 parents fd5f1b6 + 2b6e40b commit f1d4b2a

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

.github/workflows/check-all-english-links.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ jobs:
1717
check_all_english_links:
1818
name: Check all links
1919
if: github.repository == 'github/docs-internal'
20-
runs-on: self-hosted
20+
runs-on: ubuntu-20.04-xl
2121
env:
2222
GITHUB_TOKEN: ${{ secrets.DOCUBOT_READORG_REPO_WORKFLOW_SCOPES }}
2323
FIRST_RESPONDER_PROJECT: Docs content first responder
2424
REPORT_AUTHOR: docubot
2525
REPORT_LABEL: broken link report
2626
REPORT_REPOSITORY: github/docs-content
2727
steps:
28+
- name: Check that gh CLI is installed
29+
run: gh --version
30+
2831
- name: Check out repo's default branch
2932
uses: actions/checkout@dcd71f646680f2efd8db4afa5ad64fdcba30e748
3033
- name: Setup Node
@@ -60,8 +63,8 @@ jobs:
6063
DISABLE_REWRITE_ASSET_URLS: true
6164
run: |
6265
node server.mjs > /tmp/stdout.log 2> /tmp/stderr.log &
63-
sleep 5
64-
curl --retry-connrefused --retry 4 -I http://localhost:4000/
66+
sleep 6
67+
curl --retry-connrefused --retry 5 -I http://localhost:4000/
6568
6669
- if: ${{ failure() }}
6770
name: Debug server outputs on errors

tests/helpers/caching-headers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SURROGATE_ENUMS } from '../../middleware/set-fastly-surrogate-key.js'
33
export function checkCachingHeaders(res, defaultSurrogateKey = false, minMaxAge = 60 * 60) {
44
expect(res.headers['set-cookie']).toBeUndefined()
55
expect(res.headers['cache-control']).toContain('public')
6-
const maxAgeSeconds = parseInt(res.header['cache-control'].match(/max-age=(\d+)/)[1], 10)
6+
const maxAgeSeconds = parseInt(res.headers['cache-control'].match(/max-age=(\d+)/)[1], 10)
77
// Let's not be too specific in the tests, just as long as it's testing
88
// that it's a reasonably large number of seconds.
99
expect(maxAgeSeconds).toBeGreaterThanOrEqual(minMaxAge)

tests/unit/static-assets.js

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,51 @@
11
import nock from 'nock'
22
import { expect, jest } from '@jest/globals'
33

4-
import { get } from '../helpers/supertest.js'
54
import { checkCachingHeaders } from '../helpers/caching-headers.js'
5+
import { setDefaultFastlySurrogateKey } from '../../middleware/set-fastly-surrogate-key.js'
6+
import archivedEnterpriseVersionsAssets from '../../middleware/archived-enterprise-versions-assets.js'
7+
8+
function mockRequest(path, { headers }) {
9+
const _headers = Object.fromEntries(
10+
Object.entries(headers || {}).map(([key, value]) => [key.toLowerCase(), value])
11+
)
12+
return {
13+
path,
14+
url: path,
15+
get: (header) => {
16+
return _headers[header.toLowerCase()]
17+
},
18+
set: (header, value) => {
19+
_headers[header.toLowerCase()] = value
20+
},
21+
22+
headers,
23+
}
24+
}
25+
const mockResponse = () => {
26+
const res = {}
27+
res.status = 404
28+
res.statusCode = 404
29+
res.json = (payload) => {
30+
res._json = payload
31+
}
32+
res.send = (body) => {
33+
res.status = 200
34+
res.statusCode = 200
35+
res._send = body
36+
}
37+
res.headers = {}
38+
res.set = (key, value) => {
39+
if (typeof key === 'string') {
40+
res.headers[key.toLowerCase()] = value
41+
} else {
42+
for (const [k, value] of Object.entries(key)) {
43+
res.headers[k.toLowerCase()] = value
44+
}
45+
}
46+
}
47+
return res
48+
}
649

750
describe('archived enterprise static assets', () => {
851
// Sometimes static assets are proxied. The URL for the static asset
@@ -51,51 +94,87 @@ describe('archived enterprise static assets', () => {
5194
afterAll(() => nock.cleanAll())
5295

5396
it('should proxy if the static asset is prefixed', async () => {
54-
const res = await get('/enterprise/2.21/_next/static/foo.css', {
97+
const req = mockRequest('/enterprise/2.21/_next/static/foo.css', {
5598
headers: {
5699
Referrer: '/enterprise/2.21',
57100
},
58101
})
102+
const res = mockResponse()
103+
const next = () => {
104+
throw new Error('did not expect this to ever happen')
105+
}
106+
setDefaultFastlySurrogateKey(req, res, () => {})
107+
await archivedEnterpriseVersionsAssets(req, res, next)
59108
expect(res.statusCode).toBe(200)
60109
checkCachingHeaders(res, true, 60)
61110
})
111+
62112
it('should proxy if the Referrer header indicates so', async () => {
63-
const res = await get('/_next/static/only-on-proxy.css', {
113+
const req = mockRequest('/_next/static/only-on-proxy.css', {
64114
headers: {
65115
Referrer: '/enterprise/2.21',
66116
},
67117
})
118+
const res = mockResponse()
119+
const next = () => {
120+
throw new Error('did not expect this to ever happen')
121+
}
122+
setDefaultFastlySurrogateKey(req, res, () => {})
123+
await archivedEnterpriseVersionsAssets(req, res, next)
68124
expect(res.statusCode).toBe(200)
69125
checkCachingHeaders(res, true, 60)
70126
})
127+
71128
it('should proxy if the Referrer header indicates so', async () => {
72-
const res = await get('/_next/static/only-on-2.3.css', {
129+
const req = mockRequest('/_next/static/only-on-2.3.css', {
73130
headers: {
74131
Referrer: '/en/[email protected]/some/page',
75132
},
76133
})
134+
const res = mockResponse()
135+
const next = () => {
136+
throw new Error('did not expect this to ever happen')
137+
}
138+
setDefaultFastlySurrogateKey(req, res, () => {})
139+
await archivedEnterpriseVersionsAssets(req, res, next)
77140
expect(res.statusCode).toBe(200)
78141
checkCachingHeaders(res, true, 60)
79142
})
143+
80144
it('might still 404 even with the right referrer', async () => {
81-
const res = await get('/_next/static/fourofour.css', {
145+
const req = mockRequest('/_next/static/fourofour.css', {
82146
headers: {
83147
Referrer: '/en/[email protected]/some/page',
84148
},
85149
})
150+
const res = mockResponse()
151+
let nexted = false
152+
const next = () => {
153+
nexted = true
154+
}
155+
setDefaultFastlySurrogateKey(req, res, next)
156+
await archivedEnterpriseVersionsAssets(req, res, next)
86157
expect(res.statusCode).toBe(404)
87-
checkCachingHeaders(res, true, 60)
158+
// It did't exit in that middleware but called next() to move on
159+
// with any other middlewares.
160+
expect(nexted).toBe(true)
88161
})
89162

90163
it('404 on the proxy but actually present here', async () => {
91-
const res = await get('/assets/images/site/logo.png', {
164+
const req = mockRequest('/assets/images/site/logo.png', {
92165
headers: {
93166
Referrer: '/en/[email protected]/some/page',
94167
},
95168
})
169+
const res = mockResponse()
170+
let nexted = false
171+
const next = () => {
172+
nexted = true
173+
}
174+
setDefaultFastlySurrogateKey(req, res, () => {})
175+
await archivedEnterpriseVersionsAssets(req, res, next)
96176
// It tried to go via the proxy, but it wasn't there, but then it
97177
// tried "our disk" and it's eventually there.
98-
expect(res.statusCode).toBe(200)
99-
checkCachingHeaders(res, true, 60)
178+
expect(nexted).toBe(true)
100179
})
101180
})

0 commit comments

Comments
 (0)