|
1 | 1 | import nock from 'nock'
|
2 | 2 | import { expect, jest } from '@jest/globals'
|
3 | 3 |
|
4 |
| -import { get } from '../helpers/supertest.js' |
5 | 4 | 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 | +} |
6 | 49 |
|
7 | 50 | describe('archived enterprise static assets', () => {
|
8 | 51 | // Sometimes static assets are proxied. The URL for the static asset
|
@@ -51,51 +94,87 @@ describe('archived enterprise static assets', () => {
|
51 | 94 | afterAll(() => nock.cleanAll())
|
52 | 95 |
|
53 | 96 | 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', { |
55 | 98 | headers: {
|
56 | 99 | Referrer: '/enterprise/2.21',
|
57 | 100 | },
|
58 | 101 | })
|
| 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) |
59 | 108 | expect(res.statusCode).toBe(200)
|
60 | 109 | checkCachingHeaders(res, true, 60)
|
61 | 110 | })
|
| 111 | + |
62 | 112 | 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', { |
64 | 114 | headers: {
|
65 | 115 | Referrer: '/enterprise/2.21',
|
66 | 116 | },
|
67 | 117 | })
|
| 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) |
68 | 124 | expect(res.statusCode).toBe(200)
|
69 | 125 | checkCachingHeaders(res, true, 60)
|
70 | 126 | })
|
| 127 | + |
71 | 128 | 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', { |
73 | 130 | headers: {
|
74 | 131 | Referrer: '/en/[email protected]/some/page',
|
75 | 132 | },
|
76 | 133 | })
|
| 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) |
77 | 140 | expect(res.statusCode).toBe(200)
|
78 | 141 | checkCachingHeaders(res, true, 60)
|
79 | 142 | })
|
| 143 | + |
80 | 144 | 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', { |
82 | 146 | headers: {
|
83 | 147 | Referrer: '/en/[email protected]/some/page',
|
84 | 148 | },
|
85 | 149 | })
|
| 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) |
86 | 157 | 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) |
88 | 161 | })
|
89 | 162 |
|
90 | 163 | 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', { |
92 | 165 | headers: {
|
93 | 166 | Referrer: '/en/[email protected]/some/page',
|
94 | 167 | },
|
95 | 168 | })
|
| 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) |
96 | 176 | // It tried to go via the proxy, but it wasn't there, but then it
|
97 | 177 | // tried "our disk" and it's eventually there.
|
98 |
| - expect(res.statusCode).toBe(200) |
99 |
| - checkCachingHeaders(res, true, 60) |
| 178 | + expect(nexted).toBe(true) |
100 | 179 | })
|
101 | 180 | })
|
0 commit comments