0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00
astro/packages/integrations/node/test/prerender-404-500.test.js
Emanuele Stoppa d6edc75408
Adapter enhancements (#9661)
* quality of life updates for `App` (#9579)

* feat(app): writeResponse for node-based adapters

* add changeset

* Apply suggestions from code review

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* Apply suggestions from code review

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* add examples for NodeApp static methods

* unexpose createOutgoingHttpHeaders from public api

* move headers test to core

* clientAddress test

* cookies test

* destructure renderOptions right at the start

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* Fallback node standalone to localhost (#9545)

* Fallback node standalone to localhost

* Update .changeset/tame-squids-film.md

* quality of life updates for the node adapter (#9582)

* descriptive names for files and functions

* update tests

* add changeset

* appease linter

* Apply suggestions from code review

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>

* `server-entrypoint.js` -> `server.js`

* prevent crash on stream error (from PR 9533)

* Apply suggestions from code review

Co-authored-by: Luiz Ferraz <luiz@lferraz.com>

* `127.0.0.1` -> `localhost`

* add changeset for fryuni's fix

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

---------

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Luiz Ferraz <luiz@lferraz.com>
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>

* chore(vercel): delete request response conversion logic (#9583)

* refactor

* add changeset

* bump peer dependencies

* unexpose symbols (#9683)

* Update .changeset/tame-squids-film.md

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Arsh <69170106+lilnasy@users.noreply.github.com>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Luiz Ferraz <luiz@lferraz.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
2024-01-17 13:10:43 +00:00

282 lines
7.6 KiB
JavaScript

import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';
/**
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
*/
async function load() {
const mod = await import(
`./fixtures/prerender-404-500/dist/server/entry.mjs?dropcache=${Date.now()}`
);
return mod;
}
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({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
site: 'https://test.dev/',
base: '/some-base',
root: './fixtures/prerender-404-500/',
output: 'server',
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('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);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Hello world!');
});
it('Can handle prerendered 404', async () => {
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);
expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404);
const html1 = await res1.text();
const html2 = await res2.text();
const html3 = await res3.text();
expect(html1).to.equal(html2);
expect(html2).to.equal(html3);
const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist');
});
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);
expect(response1.status).to.equal(500);
const html1 = await response1.text();
const html2 = await response2.text();
const html3 = await response3.text();
expect(html1).to.contain('Something went wrong');
expect(html1).to.equal(html2);
expect(html2).to.equal(html3);
});
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);
// length will be 0 if the stylesheet does not get included
expect($('style')).to.have.a.lengthOf(1);
});
});
describe('Without base', async () => {
before(async () => {
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
site: 'https://test.info/',
root: './fixtures/prerender-404-500/',
output: 'server',
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('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/static`);
const html = await res.text();
const $ = cheerio.load(html);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Hello world!');
});
it('Can handle prerendered 404', async () => {
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);
expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404);
const html1 = await res1.text();
const html2 = await res2.text();
const html3 = await res3.text();
expect(html1).to.equal(html2);
expect(html2).to.equal(html3);
const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist');
});
});
});
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({
// inconsequential config that differs between tests
// to bust cache and prevent modules and their state
// from being reused
site: 'https://test.com/',
base: '/some-base',
root: './fixtures/prerender-404-500/',
output: 'hybrid',
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('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);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Hello world!');
});
it('Can handle prerendered 404', async () => {
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);
expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404);
const html1 = await res1.text();
const html2 = await res2.text();
const html3 = await res3.text();
expect(html1).to.equal(html2);
expect(html2).to.equal(html3);
const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist');
});
});
describe('Without base', async () => {
before(async () => {
process.env.PRERENDER = false;
fixture = await loadFixture({
// 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/',
output: 'hybrid',
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('Can render SSR route', async () => {
const res = await fetch(`http://${server.host}:${server.port}/static`);
const html = await res.text();
const $ = cheerio.load(html);
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('Hello world!');
});
it('Can handle prerendered 404', async () => {
const url = `http://${server.host}:${server.port}/missing`;
const res1 = await fetch(url);
const res2 = await fetch(url);
const res3 = await fetch(url);
expect(res1.status).to.equal(404);
expect(res2.status).to.equal(404);
expect(res3.status).to.equal(404);
const html1 = await res1.text();
const html2 = await res2.text();
const html3 = await res3.text();
expect(html1).to.equal(html2);
expect(html2).to.equal(html3);
const $ = cheerio.load(html1);
expect($('body').text()).to.equal('Page does not exist');
});
});
});