Files
tf_docs/test/server.test.js
T

51 lines
1.5 KiB
JavaScript

'use strict';
process.env.VM_DOCS_BASE_URL = process.env.VM_DOCS_BASE_URL || 'http://vm.example';
const test = require('node:test');
const assert = require('node:assert');
const { server } = require('../server.js');
async function withServer(fn) {
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const base = `http://127.0.0.1:${server.address().port}`;
try {
await fn(base);
} finally {
await new Promise((resolve) => server.close(resolve));
}
}
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/akhq/ -> 302 to VM', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/nubes-test/akhq/`, { redirect: 'manual' });
assert.strictEqual(res.status, 302);
assert.strictEqual(res.headers.get('location'), 'http://vm.example/nubes-test/akhq/');
});
});
test('GET / -> 302 to VM root', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/`, { redirect: 'manual' });
assert.strictEqual(res.status, 302);
assert.strictEqual(res.headers.get('location'), 'http://vm.example/');
});
});
test('POST -> 405', async () => {
await withServer(async (base) => {
const res = await fetch(`${base}/health`, { method: 'POST' });
assert.strictEqual(res.status, 405);
});
});