2023-01-10 16:59:20 +00:00
|
|
|
import nodejs from '../dist/index.js';
|
2023-07-03 12:59:43 +00:00
|
|
|
import { loadFixture } from './test-utils.js';
|
2023-01-10 16:59:20 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as cheerio from 'cheerio';
|
2023-08-21 16:29:38 +00:00
|
|
|
import express from 'express';
|
2023-01-10 16:59:20 +00:00
|
|
|
|
2023-08-01 14:52:16 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import('../../../astro/test/test-utils').Fixture} Fixture
|
|
|
|
*/
|
|
|
|
|
|
|
|
async function load() {
|
2023-08-01 14:55:54 +00:00
|
|
|
const mod = await import(
|
|
|
|
`./fixtures/node-middleware/dist/server/entry.mjs?dropcache=${Date.now()}`
|
|
|
|
);
|
2023-08-01 14:52:16 +00:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:29:38 +00:00
|
|
|
describe('behavior from middleware, standalone', () => {
|
2023-08-01 14:52:16 +00:00
|
|
|
/** @type {import('./test-utils').Fixture} */
|
2023-01-10 16:59:20 +00:00
|
|
|
let fixture;
|
2023-08-01 14:52:16 +00:00
|
|
|
let server;
|
|
|
|
|
2023-01-10 16:59:20 +00:00
|
|
|
before(async () => {
|
2023-08-01 14:52:16 +00:00
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
|
|
|
process.env.PRERENDER = false;
|
2023-01-10 16:59:20 +00:00
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/node-middleware/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'standalone' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
2023-08-01 14:52:16 +00:00
|
|
|
const { startServer } = await load();
|
|
|
|
let res = startServer();
|
|
|
|
server = res.server;
|
2023-01-10 16:59:20 +00:00
|
|
|
});
|
|
|
|
|
2023-08-01 14:52:16 +00:00
|
|
|
after(async () => {
|
|
|
|
await server.stop();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
2023-08-01 14:55:54 +00:00
|
|
|
});
|
2023-08-01 14:52:16 +00:00
|
|
|
|
|
|
|
describe('404', async () => {
|
2023-01-10 16:59:20 +00:00
|
|
|
it('when mode is standalone', async () => {
|
2023-08-01 14:52:16 +00:00
|
|
|
const res = await fetch(`http://${server.host}:${server.port}/error-page`);
|
2023-01-10 16:59:20 +00:00
|
|
|
|
|
|
|
expect(res.status).to.equal(404);
|
|
|
|
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
2023-08-01 14:52:16 +00:00
|
|
|
const body = $('body');
|
|
|
|
expect(body.text()).to.equal('Page does not exist');
|
2023-01-10 16:59:20 +00:00
|
|
|
});
|
2023-01-10 17:01:27 +00:00
|
|
|
});
|
|
|
|
});
|
2023-08-21 16:29:38 +00:00
|
|
|
|
|
|
|
describe('behavior from middleware, middleware', () => {
|
|
|
|
/** @type {import('./test-utils').Fixture} */
|
|
|
|
let fixture;
|
|
|
|
let server;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
|
|
|
process.env.PRERENDER = false;
|
|
|
|
fixture = await loadFixture({
|
|
|
|
root: './fixtures/node-middleware/',
|
|
|
|
output: 'server',
|
|
|
|
adapter: nodejs({ mode: 'middleware' }),
|
|
|
|
});
|
|
|
|
await fixture.build();
|
|
|
|
const { handler } = await load();
|
|
|
|
const app = express();
|
|
|
|
app.use(handler);
|
|
|
|
server = app.listen(8888);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
server.close();
|
|
|
|
await fixture.clean();
|
|
|
|
delete process.env.PRERENDER;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when mode is standalone', async () => {
|
|
|
|
const res = await fetch(`http://localhost:8888/ssr`);
|
|
|
|
|
|
|
|
expect(res.status).to.equal(200);
|
|
|
|
|
|
|
|
const html = await res.text();
|
|
|
|
const $ = cheerio.load(html);
|
|
|
|
|
|
|
|
const body = $('body');
|
|
|
|
expect(body.text()).to.contain("Here's a random number");
|
|
|
|
});
|
|
|
|
});
|