From de1639186bddba489cec81fab62a3aa609ea4f33 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 19 Jul 2021 16:55:08 -0400 Subject: [PATCH] Add test for upstream headers --- .../image-optimizer/test/index.test.js | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/test/integration/image-optimizer/test/index.test.js b/test/integration/image-optimizer/test/index.test.js index ca99f967286c3..6a8145b3272f3 100644 --- a/test/integration/image-optimizer/test/index.test.js +++ b/test/integration/image-optimizer/test/index.test.js @@ -857,7 +857,7 @@ describe('Image Optimizer', () => { runTests({ w: size, isDev: false, domains }) }) - describe('Server support for minimumCacheTTL', () => { + describe('Server support for minimumCacheTTL in next.config.js', () => { const size = 96 // defaults defined in server/config.ts const ttl = 5 // super low ttl in seconds beforeAll(async () => { @@ -880,6 +880,58 @@ describe('Image Optimizer', () => { runTests({ w: size, isDev: false, ttl }) }) + describe('Server support for headers in next.config.js', () => { + const size = 96 // defaults defined in server/config.ts + beforeAll(async () => { + nextConfig.replace( + '{ /* replaceme */ }', + `{ + async headers() { + return [ + { + source: '/test.png', + headers: [ + { + key: 'Cache-Control', + value: 'public, max-age=86400, must-revalidate', + }, + ], + }, + ] + }, + }` + ) + await nextBuild(appDir) + appPort = await findPort() + app = await nextStart(appDir, appPort) + }) + afterAll(async () => { + await killApp(app) + nextConfig.restore() + await fs.remove(imagesDir) + }) + + it('should set max-age header from upstream when matching next.config.js', async () => { + const query = { url: '/test.png', w: size, q: 75 } + const opts = { headers: { accept: 'image/webp' } } + const res = await fetchViaHTTP(appPort, '/_next/image', query, opts) + expect(res.status).toBe(200) + expect(res.headers.get('Cache-Control')).toBe( + `public, max-age=86400, must-revalidate` + ) + }) + + it('should not set max-age header when not matching next.config.js', async () => { + const query = { url: '/test.jpg', w: size, q: 75 } + const opts = { headers: { accept: 'image/webp' } } + const res = await fetchViaHTTP(appPort, '/_next/image', query, opts) + expect(res.status).toBe(200) + expect(res.headers.get('Cache-Control')).toBe( + `public, max-age=0, must-revalidate` + ) + }) + }) + describe('Serverless support with next.config.js', () => { const size = 256 beforeAll(async () => {