2024-01-25 11:17:31 -05:00
|
|
|
import * as assert from 'node:assert/strict';
|
2024-02-21 09:08:19 -05:00
|
|
|
import { after, before, describe, it } from 'node:test';
|
|
|
|
import * as cheerio from 'cheerio';
|
2023-08-01 09:52:16 -05:00
|
|
|
import nodejs from '../dist/index.js';
|
2024-01-25 11:17:31 -05:00
|
|
|
import { loadFixture, waitServerListen } from './test-utils.js';
|
2023-08-01 09:52:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function load() {
|
2023-08-01 09:55:54 -05:00
|
|
|
const mod = await import(
|
2023-08-17 03:34:31 -05:00
|
|
|
`./fixtures/prerender-404-500/dist/server/entry.mjs?dropcache=${Date.now()}`
|
2023-08-01 09:55:54 -05:00
|
|
|
);
|
2023-08-01 09:52:16 -05:00
|
|
|
return mod;
|
|
|
|
}
|
2023-08-17 03:34:31 -05:00
|
|
|
|
2023-08-01 09:52:16 -05:00
|
|
|
describe('Prerender 404', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
|
|
|
describe('With base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
|
|
|
fixture = await loadFixture({
|
2023-08-17 03:34:31 -05:00
|
|
|
// inconsequential config that differs between tests
|
|
|
|
// to bust cache and prevent modules and their state
|
|
|
|
// from being reused
|
|
|
|
site: 'https://test.dev/',
|
2023-08-01 09:52:16 -05:00
|
|
|
base: '/some-base',
|
2023-08-17 03:34:31 -05:00
|
|
|
root: './fixtures/prerender-404-500/',
|
2023-08-01 09:52:16 -05:00
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { startServer } = await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/static`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Hello world!');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can handle prerendered 404', async () => {
|
2023-08-17 03:34:31 -05:00
|
|
|
const url = `http://${server.host}:${server.port}/some-base/missing`;
|
|
|
|
const res1 = await fetch(url);
|
|
|
|
const res2 = await fetch(url);
|
|
|
|
const res3 = await fetch(url);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res1.status, 404);
|
|
|
|
assert.equal(res2.status, 404);
|
|
|
|
assert.equal(res3.status, 404);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const html1 = await res1.text();
|
|
|
|
const html2 = await res2.text();
|
|
|
|
const html3 = await res3.text();
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1, html2);
|
|
|
|
assert.equal(html2, html3);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const $ = cheerio.load(html1);
|
2023-08-01 09:52:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('body').text(), 'Page does not exist');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
2023-08-17 03:34:31 -05:00
|
|
|
|
|
|
|
it(' Can handle prerendered 500 called indirectly', async () => {
|
|
|
|
const url = `http://${server.host}:${server.port}/some-base/fivehundred`;
|
|
|
|
const response1 = await fetch(url);
|
|
|
|
const response2 = await fetch(url);
|
|
|
|
const response3 = await fetch(url);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(response1.status, 500);
|
2023-08-17 03:34:31 -05:00
|
|
|
|
|
|
|
const html1 = await response1.text();
|
|
|
|
const html2 = await response2.text();
|
|
|
|
const html3 = await response3.text();
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1.includes('Something went wrong'), true);
|
2023-08-17 03:34:31 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1, html2);
|
|
|
|
assert.equal(html2, html3);
|
2023-08-17 03:34:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('prerendered 500 page includes expected styles', async () => {
|
|
|
|
const response = await fetch(`http://${server.host}:${server.port}/some-base/fivehundred`);
|
|
|
|
const html = await response.text();
|
|
|
|
const $ = cheerio.load(html);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
// length will be 0 if the stylesheet does not get included
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('style').length, 1);
|
2023-08-17 03:34:31 -05:00
|
|
|
});
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Without base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = true;
|
|
|
|
|
|
|
|
fixture = await loadFixture({
|
2023-08-17 03:34:31 -05:00
|
|
|
// inconsequential config that differs between tests
|
|
|
|
// to bust cache and prevent modules and their state
|
|
|
|
// from being reused
|
|
|
|
site: 'https://test.info/',
|
|
|
|
root: './fixtures/prerender-404-500/',
|
2023-08-01 09:52:16 -05:00
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2023-08-17 03:34:31 -05:00
|
|
|
const { startServer } = await load();
|
2023-08-01 09:52:16 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/static`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Hello world!');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can handle prerendered 404', async () => {
|
2023-08-17 03:37:38 -05:00
|
|
|
const url = `http://${server.host}:${server.port}/some-base/missing`;
|
2023-08-17 03:34:31 -05:00
|
|
|
const res1 = await fetch(url);
|
|
|
|
const res2 = await fetch(url);
|
|
|
|
const res3 = await fetch(url);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res1.status, 404);
|
|
|
|
assert.equal(res2.status, 404);
|
|
|
|
assert.equal(res3.status, 404);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const html1 = await res1.text();
|
|
|
|
const html2 = await res2.text();
|
|
|
|
const html3 = await res3.text();
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1, html2);
|
|
|
|
assert.equal(html2, html3);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const $ = cheerio.load(html1);
|
2023-08-01 09:52:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('body').text(), 'Page does not exist');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Hybrid 404', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
|
|
|
describe('With base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
2023-08-17 03:34:31 -05:00
|
|
|
// inconsequential config that differs between tests
|
|
|
|
// to bust cache and prevent modules and their state
|
|
|
|
// from being reused
|
|
|
|
site: 'https://test.com/',
|
2023-08-01 09:52:16 -05:00
|
|
|
base: '/some-base',
|
2023-08-17 03:34:31 -05:00
|
|
|
root: './fixtures/prerender-404-500/',
|
2023-08-01 09:52:16 -05:00
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2023-08-17 03:34:31 -05:00
|
|
|
const { startServer } = await load();
|
2023-08-01 09:52:16 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/static`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Hello world!');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can handle prerendered 404', async () => {
|
2023-08-17 03:37:38 -05:00
|
|
|
const url = `http://${server.host}:${server.port}/some-base/missing`;
|
2023-08-17 03:34:31 -05:00
|
|
|
const res1 = await fetch(url);
|
|
|
|
const res2 = await fetch(url);
|
|
|
|
const res3 = await fetch(url);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res1.status, 404);
|
|
|
|
assert.equal(res2.status, 404);
|
|
|
|
assert.equal(res3.status, 404);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const html1 = await res1.text();
|
|
|
|
const html2 = await res2.text();
|
|
|
|
const html3 = await res3.text();
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1, html2);
|
|
|
|
assert.equal(html2, html3);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const $ = cheerio.load(html1);
|
2023-08-01 09:52:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('body').text(), 'Page does not exist');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Without base', async () => {
|
|
|
|
before(async () => {
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
2023-08-17 03:34:31 -05:00
|
|
|
// inconsequential config that differs between tests
|
|
|
|
// to bust cache and prevent modules and their state
|
|
|
|
// from being reused
|
|
|
|
site: 'https://test.net/',
|
|
|
|
root: './fixtures/prerender-404-500/',
|
2023-08-01 09:52:16 -05:00
|
|
|
output: 'hybrid',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2023-08-17 03:34:31 -05:00
|
|
|
const { startServer } = await load();
|
2023-08-01 09:52:16 -05:00
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2024-01-25 11:17:31 -05:00
|
|
|
await waitServerListen(server.server);
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can render SSR route', async () => {
|
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/static`);
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res.status, 200);
|
|
|
|
assert.equal($('h1').text(), 'Hello world!');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Can handle prerendered 404', async () => {
|
2023-08-17 03:37:38 -05:00
|
|
|
const url = `http://${server.host}:${server.port}/missing`;
|
2023-08-17 03:34:31 -05:00
|
|
|
const res1 = await fetch(url);
|
|
|
|
const res2 = await fetch(url);
|
|
|
|
const res3 = await fetch(url);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(res1.status, 404);
|
|
|
|
assert.equal(res2.status, 404);
|
|
|
|
assert.equal(res3.status, 404);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const html1 = await res1.text();
|
|
|
|
const html2 = await res2.text();
|
|
|
|
const html3 = await res3.text();
|
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal(html1, html2);
|
|
|
|
assert.equal(html2, html3);
|
2023-08-17 03:37:38 -05:00
|
|
|
|
2023-08-17 03:34:31 -05:00
|
|
|
const $ = cheerio.load(html1);
|
2023-08-01 09:52:16 -05:00
|
|
|
|
2024-01-25 11:17:31 -05:00
|
|
|
assert.equal($('body').text(), 'Page does not exist');
|
2023-08-01 09:52:16 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|