|
1 | 1 | import fs from 'fs' |
2 | 2 | import path from 'path' |
3 | 3 |
|
4 | | -import { expect } from '@jest/globals' |
| 4 | +import nock from 'nock' |
| 5 | +import { expect, jest } from '@jest/globals' |
5 | 6 |
|
6 | 7 | import { SURROGATE_ENUMS } from '../../middleware/set-fastly-surrogate-key.js' |
7 | 8 | import { get } from '../helpers/supertest.js' |
@@ -69,3 +70,99 @@ describe('static assets', () => { |
69 | 70 | checkCachingHeaders(res, true, 60) |
70 | 71 | }) |
71 | 72 | }) |
| 73 | + |
| 74 | +describe('archived enterprise static assets', () => { |
| 75 | + // Sometimes static assets are proxied. The URL for the static asset |
| 76 | + // might not indicate it's based on archived enterprise version. |
| 77 | + |
| 78 | + jest.setTimeout(60 * 1000) |
| 79 | + |
| 80 | + beforeAll(async () => { |
| 81 | + // The first page load takes a long time so let's get it out of the way in |
| 82 | + // advance to call out that problem specifically rather than misleadingly |
| 83 | + // attributing it to the first test |
| 84 | + // await get('/') |
| 85 | + |
| 86 | + const sampleCSS = '/* nice CSS */' |
| 87 | + |
| 88 | + nock('https://github.github.com') |
| 89 | + .get('/help-docs-archived-enterprise-versions/2.21/_next/static/foo.css') |
| 90 | + .reply(200, sampleCSS, { |
| 91 | + 'content-type': 'text/css', |
| 92 | + 'content-length': sampleCSS.length, |
| 93 | + }) |
| 94 | + nock('https://github.github.com') |
| 95 | + .get('/help-docs-archived-enterprise-versions/2.21/_next/static/only-on-proxy.css') |
| 96 | + .reply(200, sampleCSS, { |
| 97 | + 'content-type': 'text/css', |
| 98 | + 'content-length': sampleCSS.length, |
| 99 | + }) |
| 100 | + nock('https://github.github.com') |
| 101 | + .get('/help-docs-archived-enterprise-versions/2.3/_next/static/only-on-2.3.css') |
| 102 | + .reply(200, sampleCSS, { |
| 103 | + 'content-type': 'text/css', |
| 104 | + 'content-length': sampleCSS.length, |
| 105 | + }) |
| 106 | + nock('https://github.github.com') |
| 107 | + .get('/help-docs-archived-enterprise-versions/2.3/_next/static/fourofour.css') |
| 108 | + .reply(404, 'Not found', { |
| 109 | + 'content-type': 'text/plain', |
| 110 | + }) |
| 111 | + nock('https://github.github.com') |
| 112 | + .get('/help-docs-archived-enterprise-versions/2.3/assets/images/site/logo.png') |
| 113 | + .reply(404, 'Not found', { |
| 114 | + 'content-type': 'text/plain', |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + afterAll(() => nock.cleanAll()) |
| 119 | + |
| 120 | + it('should proxy if the static asset is prefixed', async () => { |
| 121 | + const res = await get('/enterprise/2.21/_next/static/foo.css', { |
| 122 | + headers: { |
| 123 | + Referrer: '/enterprise/2.21', |
| 124 | + }, |
| 125 | + }) |
| 126 | + expect(res.statusCode).toBe(200) |
| 127 | + checkCachingHeaders(res, true, 60) |
| 128 | + }) |
| 129 | + it('should proxy if the Referrer header indicates so', async () => { |
| 130 | + const res = await get('/_next/static/only-on-proxy.css', { |
| 131 | + headers: { |
| 132 | + Referrer: '/enterprise/2.21', |
| 133 | + }, |
| 134 | + }) |
| 135 | + expect(res.statusCode).toBe(200) |
| 136 | + checkCachingHeaders(res, true, 60) |
| 137 | + }) |
| 138 | + it('should proxy if the Referrer header indicates so', async () => { |
| 139 | + const res = await get('/_next/static/only-on-2.3.css', { |
| 140 | + headers: { |
| 141 | + Referrer: '/en/[email protected]/some/page', |
| 142 | + }, |
| 143 | + }) |
| 144 | + expect(res.statusCode).toBe(200) |
| 145 | + checkCachingHeaders(res, true, 60) |
| 146 | + }) |
| 147 | + it('might still 404 even with the right referrer', async () => { |
| 148 | + const res = await get('/_next/static/fourofour.css', { |
| 149 | + headers: { |
| 150 | + Referrer: '/en/[email protected]/some/page', |
| 151 | + }, |
| 152 | + }) |
| 153 | + expect(res.statusCode).toBe(404) |
| 154 | + checkCachingHeaders(res, true, 60) |
| 155 | + }) |
| 156 | + |
| 157 | + it('404 on the proxy but actually present here', async () => { |
| 158 | + const res = await get('/assets/images/site/logo.png', { |
| 159 | + headers: { |
| 160 | + Referrer: '/en/[email protected]/some/page', |
| 161 | + }, |
| 162 | + }) |
| 163 | + // It tried to go via the proxy, but it wasn't there, but then it |
| 164 | + // tried "our disk" and it's eventually there. |
| 165 | + expect(res.statusCode).toBe(200) |
| 166 | + checkCachingHeaders(res, true, 60) |
| 167 | + }) |
| 168 | +}) |
0 commit comments