From d68d21c3291d4126008e0d3d8035924e82bcc1ba Mon Sep 17 00:00:00 2001 From: Darsh Patel Date: Sat, 23 Jan 2021 20:45:13 +0530 Subject: [PATCH] Test: serverless target should set correct revalidation (`cache-control`) header (#15512) --- .../next.config.js | 5 ++ .../pages/revalidate.js | 16 +++++ .../serverless-trace-revalidate/server.js | 21 +++++++ .../test/index.test.js | 58 +++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 test/integration/serverless-trace-revalidate/next.config.js create mode 100644 test/integration/serverless-trace-revalidate/pages/revalidate.js create mode 100644 test/integration/serverless-trace-revalidate/server.js create mode 100644 test/integration/serverless-trace-revalidate/test/index.test.js diff --git a/test/integration/serverless-trace-revalidate/next.config.js b/test/integration/serverless-trace-revalidate/next.config.js new file mode 100644 index 0000000000..d15379d676 --- /dev/null +++ b/test/integration/serverless-trace-revalidate/next.config.js @@ -0,0 +1,5 @@ +module.exports = { + target: 'experimental-serverless-trace', + // make sure error isn't thrown from empty publicRuntimeConfig + publicRuntimeConfig: {}, +} diff --git a/test/integration/serverless-trace-revalidate/pages/revalidate.js b/test/integration/serverless-trace-revalidate/pages/revalidate.js new file mode 100644 index 0000000000..f810f624ed --- /dev/null +++ b/test/integration/serverless-trace-revalidate/pages/revalidate.js @@ -0,0 +1,16 @@ +export const getStaticProps = () => { + return { + props: { + hello: 'hello world', + random: Math.random(), + }, + revalidate: 10, + } +} + +export default ({ hello, random }) => ( + <> +

{hello}

+

{random}

+ +) diff --git a/test/integration/serverless-trace-revalidate/server.js b/test/integration/serverless-trace-revalidate/server.js new file mode 100644 index 0000000000..848c88de5a --- /dev/null +++ b/test/integration/serverless-trace-revalidate/server.js @@ -0,0 +1,21 @@ +const path = require('path') +const http = require('http') + +const server = http.createServer((req, res) => { + const pagePath = (page) => path.join('.next/serverless/pages/', page) + const render = (page) => { + require(`./${pagePath(page)}`).render(req, res) + } + + switch (req.url) { + case '/revalidate': { + return render('/revalidate') + } + default: { + return res.end('404') + } + } +}) +server.listen(process.env.PORT, () => { + console.log('ready on', process.env.PORT) +}) diff --git a/test/integration/serverless-trace-revalidate/test/index.test.js b/test/integration/serverless-trace-revalidate/test/index.test.js new file mode 100644 index 0000000000..340bd296df --- /dev/null +++ b/test/integration/serverless-trace-revalidate/test/index.test.js @@ -0,0 +1,58 @@ +/* eslint-env jest */ + +import fs from 'fs-extra' +import { join } from 'path' +import { + nextBuild, + findPort, + killApp, + initNextServerScript, +} from 'next-test-utils' + +const appDir = join(__dirname, '../') +jest.setTimeout(1000 * 60 * 2) + +let appPort +let app +let buildId + +const nextStart = async (appDir, appPort) => { + const scriptPath = join(appDir, 'server.js') + const env = Object.assign({ ...process.env }, { PORT: `${appPort}` }) + + return initNextServerScript( + scriptPath, + /ready on/i, + env, + /ReferenceError: options is not defined/ + ) +} + +describe('Serverless Trace', () => { + beforeAll(async () => { + await nextBuild(appDir) + appPort = await findPort() + app = await nextStart(appDir, appPort) + buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8') + }) + afterAll(() => killApp(app)) + + it('should have revalidate page in prerender-manifest with correct interval', async () => { + const data = await fs.readJSON( + join(appDir, '.next/prerender-manifest.json') + ) + expect(data.routes['/revalidate']).toEqual({ + initialRevalidateSeconds: 10, + dataRoute: `/_next/data/${buildId}/revalidate.json`, + srcRoute: null, + }) + }) + + it('should set correct Cache-Control header', async () => { + const url = `http://localhost:${appPort}/revalidate` + const res = await fetch(url) + expect(res.headers.get('Cache-Control')).toMatch( + 's-maxage=10, stale-while-revalidate' + ) + }) +}) -- GitLab