-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |