Skip to content

Commit

Permalink
feat: http headers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeday committed Aug 14, 2023
1 parent 1269236 commit bf10b2f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
9 changes: 5 additions & 4 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ const withBundleAnalyzer = NextBundleAnalyzer({

// cache control
export const CACHE_CONTROL_HEADER = 'x-cache-control';
const CACHE_CONTROL_PAGES = [
export const CACHE_CONTROL_PAGES = [
'/manifest.json',
'/favicon:size*',
'/',
'/wrap',
'/wrap/unwrap',
'/rewards',
'/withdrawals',
'/referral',
'/withdrawals/request',
'/withdrawals/claim',
'/runtime/window-env.js',
];
const CACHE_CONTROL_VALUE =
'public,max-age=15, s-max-age=30, stale-if-error=604800, stale-while-revalidate=172800';
export const CACHE_CONTROL_VALUE =
'public, max-age=15, s-max-age=30, stale-if-error=604800, stale-while-revalidate=172800';

export default withBundleAnalyzer({
basePath,
Expand Down Expand Up @@ -109,6 +109,7 @@ export default withBundleAnalyzer({
cspReportUri,
cspReportOnly,
),
frameGuard: false,
referrerPolicy: 'same-origin',
}),
{
Expand Down
2 changes: 1 addition & 1 deletion server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CACHE_CONTROL_HEADER } from './next.config.mjs';

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3000;
const port = Number(process.env.PORT) || 3000;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
Expand Down
44 changes: 44 additions & 0 deletions test/headers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { expect, test } from '@playwright/test';
import { getAllPagesRoutes } from './utils/collect-next-pages.js';

import {
CACHE_CONTROL_HEADER,
CACHE_CONTROL_PAGES,
CACHE_CONTROL_VALUE,
} from 'next.config.mjs';

// case for only wildcard in config
const configPages = CACHE_CONTROL_PAGES;
configPages[CACHE_CONTROL_PAGES.indexOf('/favicon:size*')] = '/favicon.ico';

test.describe('Page Headers', () => {
test('Config should have all static pages', () => {
const pageRoutes = getAllPagesRoutes();
pageRoutes.forEach((foundPage) =>
expect(CACHE_CONTROL_PAGES.includes(foundPage)).toBe(true),
);
});

CACHE_CONTROL_PAGES.map((route) =>
test(`Page ${route} should have proper headers`, async ({ request }) => {
const resp = await request.get(route);
expect(resp.status()).toBe(200);
const headers = resp.headers();

expect(headers['cache-control']).toBe(CACHE_CONTROL_VALUE);

expect(headers['referrer-policy']).toBe('same-origin');
expect(headers['strict-transport-security']).toBe(
'max-age=63072000; includeSubDomains; preload',
);
expect(headers['x-content-type-options']).toBe('nosniff');
expect(headers['x-xss-protection']).toBe('1');
expect(headers['x-dns-prefetch-control']).toBe('on');
expect(headers['x-download-options']).toBe('noopen');
expect(headers).toHaveProperty('content-security-policy-report-only');

expect(headers['x-frame-options']).toBeUndefined();
expect(headers[CACHE_CONTROL_HEADER]).toBeUndefined();
}),
);
});
32 changes: 32 additions & 0 deletions test/utils/collect-next-pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

export const getAllPagesRoutes = () => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const directory = path.join(__dirname, '../../.next/server/pages');

const items = fs.readdirSync(directory);
const routes: string[] = [];
let item: string | undefined;
while ((item = items.pop())) {
const itemPath = path.join(directory, item);
const stats = fs.statSync(itemPath);
if (
stats.isFile() &&
path.extname(item) === '.html' &&
!item.endsWith('404.html') &&
!item.endsWith('500.html')
) {
//console.log(item, path.basename(item));
const routePath = path.join('/', item.replace(/(index)?\.html/, ''));
routes.push(routePath);
} else if (stats.isDirectory()) {
const files = fs
.readdirSync(itemPath)
.map((files) => path.join(item as string, files));
items.push(...files);
}
}
return routes;
};

0 comments on commit bf10b2f

Please sign in to comment.