Skip to content

Commit

Permalink
handler-test-cleanup (#32)
Browse files Browse the repository at this point in the history
handler test cleanup
  • Loading branch information
wydengyre authored Jan 27, 2024
1 parent bfbbf7a commit ee85b6f
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions lib/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import expectedJson from "./test/lastoriaingiallo.parsed.json" with {
};
import { parseFeed } from "./test/parse-feed.js";

test("handler", async (t) => {
await t.test(indexSucccess);
await t.test(rssFeedSuccess);
await t.test(rssFeedFail404);
await t.test(rssFeedFail500);
await t.test(rssFeedFailCorrupt);
await t.test(notFound);
});

const baseUrl = new URL("https://test.dev/");
const raiBaseUrl = new URL("https://rai.dev/");

Expand All @@ -19,67 +28,70 @@ const fetchHandler = mkFetchHandler({
logger: logger.disabled,
});

test("index", async () => {
async function indexSucccess() {
const req = new Request("arbitrary://arbitrary/");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.headers.get("Content-Language"), "it");
const _text = await resp.text();
// TODO: check validity of HTML
});
}

test("rss feed success", async () => {
const req = new Request("https://test.dev/programmi/lastoriaingiallo.xml");
async function rssFeedSuccess() {
const req = new Request(
"arbitrary://arbitrary/programmi/lastoriaingiallo.xml",
);
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 200);
const feed = await resp.text();
const parsedFeed = parseFeed(feed);
assert.deepStrictEqual(parsedFeed, expectedJson);
});
}

test("rss feed failure: 404 from RAI server", async () => {
const req = new Request("https://test.dev/programmi/nonexistent.xml");
async function rssFeedFail404() {
const req = new Request("arbitrary://arbitrary/programmi/nonexistent.xml");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 404);
assert.strictEqual(resp.headers.get("Content-Type"), "application/xml");
const text = await resp.text();
assert.strictEqual(
text,
"<error><code>404</code><message>Not Found</message></error>",
);
});
}

test("rss feed failure: 500 from RAI server", async () => {
const req = new Request("https://test.dev/programmi/500.xml");
async function rssFeedFail500() {
const req = new Request("arbitrary://arbitrary/programmi/500.xml");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 500);
assert.strictEqual(resp.headers.get("Content-Type"), "application/xml");
const text = await resp.text();
assert.strictEqual(
text,
"<error><code>500</code><message>server error</message></error>",
);
});
}

test("rss feed failure: failure to process RAI json feed", async () => {
const req = new Request("https://test.dev/programmi/corrupt.xml");
async function rssFeedFailCorrupt() {
const req = new Request("arbitrary://arbitrary/programmi/corrupt.xml");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 500);
assert.strictEqual(resp.headers.get("Content-Type"), "application/xml");
const text = await resp.text();
assert.strictEqual(
text,
"<error><code>500</code><message>server error</message></error>",
);
});
}

test("404", async () => {
const req = new Request("https://test.dev/nonexistent");
async function notFound() {
const req = new Request("arbitrary://arbitrary/nonexistent");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 404);
const text = await resp.text();
assert.strictEqual(text, "Not found.");
});
}

0 comments on commit ee85b6f

Please sign in to comment.