This commit is contained in:
Rishi Raj Jain 2023-09-24 10:38:49 +05:30
parent 5b124f3b81
commit 6499c9b3da
4 changed files with 110 additions and 0 deletions

View file

@ -0,0 +1,9 @@
{
"name": "@test/nodejs-trailing-slash",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}

View file

@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>One</title>
</head>
<body>
<h1>One</h1>
</body>
</html>

View file

@ -0,0 +1,82 @@
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
/**
* @typedef {import('astro/test/test-utils.js').Fixture} Fixture
*/
async function load() {
const mod = await import(
`./fixtures/trailing-slash/dist/server/entry.mjs?dropcache=${Date.now()}`
);
return mod;
}
describe('Trailing Slash In Production', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
let server;
describe('By default does add the trailing slash', async () => {
before(async () => {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.PRERENDER = true;
fixture = await loadFixture({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
output: 'hybrid',
root: './fixtures/trailing-slash/',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('redirects each route to have trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
expect(res.url).to.contain('one/');
});
});
describe('Does not add trailing slash when set to never', async () => {
before(async () => {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
process.env.PRERENDER = true;
fixture = await loadFixture({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
output: 'hybrid',
trailingSlash: 'never',
root: './fixtures/trailing-slash/',
adapter: nodejs({ mode: 'standalone' }),
});
await fixture.build();
const { startServer } = await load();
let res = startServer();
server = res.server;
});
after(async () => {
await server.stop();
await fixture.clean();
delete process.env.PRERENDER;
});
it('redirects each route to have trailing slash', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one`);
expect(res.url).to.not.contain('one/');
});
});
});

View file

@ -4465,6 +4465,15 @@ importers:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/node/test/fixtures/trailing-slash:
dependencies:
'@astrojs/node':
specifier: workspace:*
version: link:../../..
astro:
specifier: workspace:*
version: link:../../../../../astro
packages/integrations/node/test/fixtures/url-protocol:
dependencies:
'@astrojs/node':