forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanchor-redirect.js
62 lines (60 loc) · 2.36 KB
/
anchor-redirect.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
import { describe, expect } from '@jest/globals'
import { get } from '../helpers/e2etest.js'
import {
SURROGATE_ENUMS,
makeLanguageSurrogateKey,
} from '../../middleware/set-fastly-surrogate-key.js'
import clientSideRedirects from '../../lib/redirects/static/client-side-rest-api-redirects.json'
describe('anchor-redirect middleware', () => {
test('returns correct redirect to url', async () => {
// test the first entry
const [key, value] = Object.entries(clientSideRedirects)[0]
const [path, hash] = key.split('#')
const sp = new URLSearchParams()
sp.set('path', path)
sp.set('hash', hash)
const res = await get('/anchor-redirect?' + sp)
expect(res.statusCode).toBe(200)
const { to } = JSON.parse(res.text)
expect(to).toBe(value)
})
test('errors when path is not passed', async () => {
// test the first entry
const key = Object.keys(clientSideRedirects)[0]
const hash = key.split('#')[1]
const sp = new URLSearchParams()
sp.set('hash', hash)
const res = await get('/anchor-redirect?' + sp)
expect(res.statusCode).toBe(400)
})
test('errors when path is not passed', async () => {
// test the first entry
const key = Object.keys(clientSideRedirects)[0]
const path = key.split('#')[0]
const sp = new URLSearchParams()
sp.set('path', path)
const res = await get('/anchor-redirect?' + sp)
expect(res.statusCode).toBe(400)
})
test('unfound redirect returns undefined', async () => {
const sp = new URLSearchParams()
sp.set('path', 'foo')
sp.set('hash', 'bar')
const res = await get('/anchor-redirect?' + sp)
const { to } = JSON.parse(res.text)
expect(to).toBe(undefined)
})
test('reasonably aggressive cache-control headers', async () => {
const sp = new URLSearchParams()
sp.set('path', 'foo')
sp.set('hash', 'bar')
const res = await get('/anchor-redirect?' + sp)
expect(res.headers['cache-control']).toContain('public')
expect(res.headers['cache-control']).toMatch(/max-age=[1-9]/)
expect(res.headers['surrogate-control']).toContain('public')
expect(res.headers['surrogate-control']).toMatch(/max-age=[1-9]/)
const surrogateKeySplit = res.headers['surrogate-key'].split(/\s/g)
expect(surrogateKeySplit.includes(SURROGATE_ENUMS.DEFAULT)).toBeTruthy()
expect(surrogateKeySplit.includes(makeLanguageSurrogateKey())).toBeTruthy()
})
})