forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearly-access-proxy.js
80 lines (70 loc) · 2.81 KB
/
early-access-proxy.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
const middleware = require('../../middleware/early-access-proxy')
const nock = require('nock')
const MockExpressResponse = require('mock-express-response')
describe('Early Access middleware', () => {
const OLD_EARLY_ACCESS_HOSTNAME = process.env.EARLY_ACCESS_HOSTNAME
beforeAll(() => {
process.env.EARLY_ACCESS_HOSTNAME = 'https://secret-website.com'
})
afterAll(() => {
process.env.EARLY_ACCESS_HOSTNAME = OLD_EARLY_ACCESS_HOSTNAME
})
const baseReq = {
context: {
earlyAccessPaths: ['/alpha-product/foo', '/beta-product/bar', '/baz']
}
}
test('are proxied from an obscured host', async () => {
const mock = nock('https://secret-website.com')
.get('/alpha-product/foo')
.reply(200, 'yay here is your proxied content', { 'content-type': 'text/html' })
const req = { ...baseReq, path: '/alpha-product/foo' }
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(mock.isDone()).toBe(true)
expect(res._getString()).toBe('yay here is your proxied content')
})
test('follows redirects', async () => {
const mock = nock('https://secret-website.com')
.get('/alpha-product/foo')
.reply(301, undefined, { Location: 'https://secret-website.com/alpha-product/foo2' })
.get('/alpha-product/foo2')
.reply(200, 'yay you survived the redirect', { 'content-type': 'text/html' })
const req = { ...baseReq, path: '/alpha-product/foo' }
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(mock.isDone()).toBe(true)
expect(res._getString()).toBe('yay you survived the redirect')
})
test('calls next() if no redirect is found', async () => {
const req = { ...baseReq, path: '/en' }
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(next).toHaveBeenCalled()
})
test('calls next() if proxy request respond with 404', async () => {
const mock = nock('https://secret-website.com')
.get('/beta-product/bar')
.reply(404, 'no dice', { 'content-type': 'text/html' })
const req = { ...baseReq, path: '/beta-product/bar' }
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(mock.isDone()).toBe(true)
expect(next).toHaveBeenCalled()
})
test('calls next() if proxy request responds with 500', async () => {
const mock = nock('https://secret-website.com')
.get('/beta-product/bar')
.reply(500, 'no dice', { 'content-type': 'text/html' })
const req = { ...baseReq, path: '/beta-product/bar' }
const res = new MockExpressResponse()
const next = jest.fn()
await middleware(req, res, next)
expect(mock.isDone()).toBe(true)
expect(next).toHaveBeenCalled()
})
})