mirror of
https://github.com/withastro/astro.git
synced 2025-11-05 12:39:20 +08:00
Co-authored-by: Matt Kane <m@mk.gg> Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com> Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com> Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com> Co-authored-by: sarah11918 <5098874+sarah11918@users.noreply.github.com>
72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
import * as assert from 'node:assert/strict';
|
|
import { after, before, describe, it } from 'node:test';
|
|
import nodejs from '../dist/index.js';
|
|
import { loadFixture, waitServerListen } from './test-utils.js';
|
|
|
|
describe('Static headers', () => {
|
|
let fixture;
|
|
|
|
before(async () => {
|
|
fixture = await loadFixture({ root: './fixtures/static-headers' });
|
|
await fixture.build();
|
|
});
|
|
|
|
it('CSP headers are added when CSP is enabled', async () => {
|
|
const headers = JSON.parse(await fixture.readFile('../dist/_experimentalHeaders.json'));
|
|
|
|
const csp = headers
|
|
.find((x) => x.pathname === '/')
|
|
.headers.find((x) => x.key === 'Content-Security-Policy');
|
|
|
|
assert.notEqual(csp, undefined, 'the index must have CSP headers');
|
|
assert.ok(
|
|
csp.value.includes('script-src'),
|
|
'must contain the script-src directive because of the server island',
|
|
);
|
|
});
|
|
|
|
it('CSP headers are added to the request', async () => {});
|
|
});
|
|
|
|
describe('Static headers', () => {
|
|
/** @type {import('./test-utils').Fixture} */
|
|
let fixture;
|
|
let server;
|
|
|
|
before(async () => {
|
|
fixture = await loadFixture({
|
|
root: './fixtures/static-headers/',
|
|
output: 'server',
|
|
adapter: nodejs({ mode: 'standalone', experimentalStaticHeaders: true }),
|
|
});
|
|
await fixture.build();
|
|
const { startServer } = await fixture.loadAdapterEntryModule();
|
|
const res = startServer();
|
|
server = res.server;
|
|
await waitServerListen(server.server);
|
|
});
|
|
|
|
after(async () => {
|
|
await server.stop();
|
|
// await fixture.clean();
|
|
});
|
|
|
|
it('CSP headers are added to the request', async () => {
|
|
const res = await fetch(`http://${server.host}:${server.port}/`);
|
|
const cps = res.headers.get('Content-Security-Policy');
|
|
assert.ok(
|
|
cps.includes('script-src'),
|
|
'should contain script-src directive due to server island',
|
|
);
|
|
});
|
|
|
|
it('CSP headers are added to dynamic orute', async () => {
|
|
const res = await fetch(`http://${server.host}:${server.port}/one`);
|
|
const cps = res.headers.get('Content-Security-Policy');
|
|
assert.ok(
|
|
cps.includes('script-src'),
|
|
'should contain script-src directive due to server island',
|
|
);
|
|
});
|
|
});
|