aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/node/test/trailing-slash.test.js
diff options
context:
space:
mode:
authorGravatar Alexander Niebuhr <alexander@nbhr.io> 2024-08-29 07:50:22 +0200
committerGravatar Alexander Niebuhr <alexander@nbhr.io> 2024-08-29 07:51:04 +0200
commit29f97307e38f91e777c8d9be5d4d84bd006e992e (patch)
treefd678596b1a821a046ba15cd550289a9a85f62a1 /packages/integrations/node/test/trailing-slash.test.js
parent93a1db68cd9cf3bb2a4d9f7a8af13cbd881eb701 (diff)
parent3cea76e35c7633b0069dca45e9cb7b14a492ec64 (diff)
downloadastro-29f97307e38f91e777c8d9be5d4d84bd006e992e.tar.gz
astro-29f97307e38f91e777c8d9be5d4d84bd006e992e.tar.zst
astro-29f97307e38f91e777c8d9be5d4d84bd006e992e.zip
Merge branch 'node' into main
Diffstat (limited to 'packages/integrations/node/test/trailing-slash.test.js')
-rw-r--r--packages/integrations/node/test/trailing-slash.test.js452
1 files changed, 452 insertions, 0 deletions
diff --git a/packages/integrations/node/test/trailing-slash.test.js b/packages/integrations/node/test/trailing-slash.test.js
new file mode 100644
index 000000000..9ea8fcddd
--- /dev/null
+++ b/packages/integrations/node/test/trailing-slash.test.js
@@ -0,0 +1,452 @@
+import * as assert from 'node:assert/strict';
+import { after, before, describe, it } from 'node:test';
+import * as cheerio from 'cheerio';
+import nodejs from '../dist/index.js';
+import { loadFixture, waitServerListen } from './test-utils.js';
+
+/**
+ * @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
+ */
+
+describe('Trailing slash', () => {
+ /** @type {import('./test-utils').Fixture} */
+ let fixture;
+ let server;
+ describe('Always', async () => {
+ describe('With base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ base: '/some-base',
+ output: 'hybrid',
+ trailingSlash: 'always',
+ outDir: './dist/always-with-base',
+ build: {
+ client: './dist/always-with-base/client',
+ server: './dist/always-with-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with redirect', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/some-base/one/');
+ });
+
+ it('Can render prerendered route with redirect and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/some-base/one/?foo=bar');
+ });
+
+ it('Can render prerendered route with query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Does not add trailing slash to subresource urls', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one.css`);
+ const css = await res.text();
+
+ assert.equal(res.status, 200);
+ assert.equal(css, 'h1 { color: red; }\n');
+ });
+ });
+ describe('Without base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ output: 'hybrid',
+ trailingSlash: 'always',
+ outDir: './dist/always-without-base',
+ build: {
+ client: './dist/always-without-base/client',
+ server: './dist/always-without-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with redirect', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/one/');
+ });
+
+ it('Can render prerendered route with redirect and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/one/?foo=bar');
+ });
+
+ it('Can render prerendered route with query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Does not add trailing slash to subresource urls', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one.css`);
+ const css = await res.text();
+
+ assert.equal(res.status, 200);
+ assert.equal(css, 'h1 { color: red; }\n');
+ });
+ });
+ });
+ describe('Never', async () => {
+ describe('With base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ base: '/some-base',
+ output: 'hybrid',
+ trailingSlash: 'never',
+ outDir: './dist/never-with-base',
+ build: {
+ client: './dist/never-with-base/client',
+ server: './dist/never-with-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with redirect', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one/`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/some-base/one');
+ });
+
+ it('Can render prerendered route with redirect and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`, {
+ redirect: 'manual',
+ });
+
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/some-base/one?foo=bar');
+ });
+
+ it('Can render prerendered route with query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+ });
+ describe('Without base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ output: 'hybrid',
+ trailingSlash: 'never',
+ outDir: './dist/never-without-base',
+ build: {
+ client: './dist/never-without-base/client',
+ server: './dist/never-without-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with redirect', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one/`, {
+ redirect: 'manual',
+ });
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/one');
+ });
+
+ it('Can render prerendered route with redirect and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`, {
+ redirect: 'manual',
+ });
+
+ assert.equal(res.status, 301);
+ assert.equal(res.headers.get('location'), '/one?foo=bar');
+ });
+
+ it('Can render prerendered route and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+ });
+ });
+ describe('Ignore', async () => {
+ describe('With base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ base: '/some-base',
+ output: 'hybrid',
+ trailingSlash: 'ignore',
+ outDir: './dist/ignore-with-base',
+ build: {
+ client: './dist/ignore-with-base/client',
+ server: './dist/ignore-with-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with slash', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one/`, {
+ redirect: 'manual',
+ });
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route without slash', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one`, {
+ redirect: 'manual',
+ });
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route with slash and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one/?foo=bar`, {
+ redirect: 'manual',
+ });
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route without slash and with query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/some-base/one?foo=bar`, {
+ redirect: 'manual',
+ });
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+ });
+ describe('Without base', async () => {
+ before(async () => {
+ process.env.ASTRO_NODE_AUTOSTART = 'disabled';
+ process.env.PRERENDER = true;
+
+ fixture = await loadFixture({
+ root: './fixtures/trailing-slash/',
+ output: 'hybrid',
+ trailingSlash: 'ignore',
+ outDir: './dist/ignore-without-base',
+ build: {
+ client: './dist/ignore-without-base/client',
+ server: './dist/ignore-without-base/server',
+ },
+ adapter: nodejs({ mode: 'standalone' }),
+ });
+ await fixture.build();
+ const { startServer } = await fixture.loadAdapterEntryModule();
+ let res = startServer();
+ server = res.server;
+ await waitServerListen(server.server);
+ });
+
+ after(async () => {
+ await server.stop();
+ await fixture.clean();
+ delete process.env.PRERENDER;
+ });
+
+ it('Can render prerendered base route', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'Index');
+ });
+
+ it('Can render prerendered route with slash', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one/`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route without slash', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route with slash and query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one/?foo=bar`, {
+ redirect: 'manual',
+ });
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+
+ it('Can render prerendered route without slash and with query params', async () => {
+ const res = await fetch(`http://${server.host}:${server.port}/one?foo=bar`);
+ const html = await res.text();
+ const $ = cheerio.load(html);
+
+ assert.equal(res.status, 200);
+ assert.equal($('h1').text(), 'One');
+ });
+ });
+ });
+});