Files
tf_docs/test/server.test.js
T

68 lines
1.8 KiB
JavaScript

'use strict';
const http = require('node:http');
const test = require('node:test');
const assert = require('node:assert');
let server;
const upstream = http.createServer((req, res) => {
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8',
'Content-Length': Buffer.byteLength('<html>ok</html>'),
});
res.end('<html>ok</html>');
});
test.before(async () => {
await new Promise((r) => upstream.listen(0, '127.0.0.1', r));
process.env.VM_DOCS_BASE_URL = `http://127.0.0.1:${upstream.address().port}`;
({ server } = require('../server.js'));
});
test.after(async () => {
await new Promise((r) => upstream.close(r));
});
async function withServer(fn) {
await new Promise((r) => server.listen(0, '127.0.0.1', r));
const base = `http://127.0.0.1:${server.address().port}`;
try {
await fn(base);
} finally {
await new Promise((r) => server.close(r));
}
}
test('GET /health -> 200 ok', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/health`);
assert.strictEqual(res.status, 200);
assert.strictEqual(await res.text(), 'ok');
});
});
test('GET /nubes-test/ -> proxies to upstream', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/nubes-test/`);
assert.strictEqual(res.status, 200);
assert.strictEqual(await res.text(), '<html>ok</html>');
});
});
test('GET / -> proxies to upstream root', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/`);
assert.strictEqual(res.status, 200);
assert.strictEqual(await res.text(), '<html>ok</html>');
});
});
test('POST -> 405', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/health`, { method: 'POST' });
assert.strictEqual(res.status, 405);
});
});