build: update reader server for Node 22
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const { once } = require('node:events');
|
||||
const test = require('node:test');
|
||||
const { createApp } = require('../main');
|
||||
|
||||
async function startServer(t, fetchImpl) {
|
||||
const server = createApp(fetchImpl).listen(0, '127.0.0.1');
|
||||
await once(server, 'listening');
|
||||
t.after(() => new Promise((resolve, reject) => {
|
||||
server.close((error) => error ? reject(error) : resolve());
|
||||
server.closeAllConnections();
|
||||
}));
|
||||
return `http://127.0.0.1:${server.address().port}`;
|
||||
}
|
||||
|
||||
function postUrl(url, form = { url: 'https://example.test/article' }) {
|
||||
return fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
||||
body: new URLSearchParams(form),
|
||||
});
|
||||
}
|
||||
|
||||
test('GET / renders the submission form', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
throw new Error('GET should not fetch an article');
|
||||
});
|
||||
const response = await fetch(`${baseUrl}/`);
|
||||
assert.equal(response.status, 200);
|
||||
assert.match(await response.text(), /<form method="POST"/);
|
||||
});
|
||||
|
||||
test('POST / fetches and extracts article content', async (t) => {
|
||||
const articleText = 'Distinctive article text for reader view. '.repeat(40);
|
||||
const html = `<html><head><title>Test article</title></head><body>
|
||||
<nav>Navigation should not appear</nav>
|
||||
<article><h1>Readable title</h1><p>${articleText}</p></article>
|
||||
</body></html>`;
|
||||
let requestedUrl;
|
||||
let requestOptions;
|
||||
const baseUrl = await startServer(t, async (url, options) => {
|
||||
requestedUrl = url;
|
||||
requestOptions = options;
|
||||
return { status: 200, text: async () => html };
|
||||
});
|
||||
|
||||
const response = await postUrl(baseUrl);
|
||||
const content = await response.text();
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(requestedUrl, 'https://example.test/article');
|
||||
assert.equal(requestOptions.headers['User-Agent'], 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0');
|
||||
assert.equal(requestOptions.headers['X-Forwarded-For'], '66.249.66.1');
|
||||
assert.match(content, /Distinctive article text for reader view/);
|
||||
assert.doesNotMatch(content, /Navigation should not appear/);
|
||||
});
|
||||
|
||||
test('POST / returns 404 when Readability cannot find an article', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => ({
|
||||
status: 200,
|
||||
text: async () => '<html><body></body></html>',
|
||||
}));
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 404);
|
||||
});
|
||||
|
||||
test('POST / forwards upstream HTTP error statuses', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => ({ status: 503 }));
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 503);
|
||||
});
|
||||
|
||||
test('POST / returns 404 when the upstream request fails', async (t) => {
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
throw new Error('network unavailable');
|
||||
});
|
||||
const response = await postUrl(baseUrl);
|
||||
assert.equal(response.status, 404);
|
||||
});
|
||||
|
||||
test('POST / rejects requests without a URL without fetching', async (t) => {
|
||||
let fetched = false;
|
||||
const baseUrl = await startServer(t, async () => {
|
||||
fetched = true;
|
||||
throw new Error('should not fetch');
|
||||
});
|
||||
const response = await postUrl(baseUrl, {});
|
||||
assert.equal(response.status, 400);
|
||||
assert.equal(fetched, false);
|
||||
});
|
||||
Reference in New Issue
Block a user