forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp-to-docs.js
71 lines (60 loc) · 2.51 KB
/
help-to-docs.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
const MockExpressResponse = require('mock-express-response')
const middleware = require('../../../../middleware/redirects/help-to-docs')
describe('help.github.com redirect middleware', () => {
it('redirects help.github.com homepage requests', async () => {
const req = {
hostname: 'help.github.com',
protocol: 'https',
originalUrl: '/'
}
const res = new MockExpressResponse()
const next = () => { /* no op */ }
await middleware(req, res, next)
expect(res._getString()).toEqual('<p>Moved Permanently. Redirecting to <a href="https://docs.github.com/">https://docs.github.com/</a></p>')
})
it('redirects help.github.com requests to deep pages', async () => {
const req = {
hostname: 'help.github.com',
protocol: 'https',
originalUrl: '/en/actions/configuring-and-managing-workflows/using-environment-variables'
}
const res = new MockExpressResponse()
const next = () => { /* no op */ }
await middleware(req, res, next)
expect(res._getString()).toEqual('<p>Moved Permanently. Redirecting to <a href="https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables">https://docs.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables</a></p>')
})
it('preserves query params', async () => {
const req = {
hostname: 'help.github.com',
protocol: 'https',
originalUrl: '/en?foo=bar'
}
const res = new MockExpressResponse()
const next = () => { /* no op */ }
await middleware(req, res, next)
expect(res._getString()).toEqual('<p>Moved Permanently. Redirecting to <a href="https://docs.github.com/en?foo=bar">https://docs.github.com/en?foo=bar</a></p>')
})
it('does not redirect docs.github.com requests', async () => {
const req = {
hostname: 'docs.github.com',
protocol: 'https',
originalUrl: '/'
}
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(next).toHaveBeenCalled()
})
it('only redirects to a docs.github.com path', async () => {
const req = {
hostname: 'help.github.com',
protocol: 'https',
originalUrl: '//evil.com//'
}
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
const expectedRedirect = 'https://docs.github.com/evil.com//'
expect(res._getString()).toEqual(`<p>Moved Permanently. Redirecting to <a href="${expectedRedirect}">${expectedRedirect}</a></p>`)
})
})