Skip to content

Commit

Permalink
italian-only (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
wydengyre authored Jan 26, 2024
1 parent be16467 commit ee517c3
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 110 deletions.
25 changes: 7 additions & 18 deletions cf/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import expectedJson from "./test/lastoriaingiallo.parsed.json" with {

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const englishHtmlPath = path.join(__dirname, "../lib/english.html");
const italianHtmlPath = path.join(__dirname, "../lib/italian.html");
const indexHtmlPath = path.join(__dirname, "../lib/index.html");

let worker: UnstableDevWorker;

Expand All @@ -33,28 +32,18 @@ after(async () => {
await worker.stop();
});

test("english index", async () => {
const englishIndex = await readFile(englishHtmlPath, "utf8");
const resp = await worker.fetch();
const text = await resp.text();

assert(resp.ok);
assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.statusText, "OK");
assert.strictEqual(resp.headers.get("Content-Language"), "en");
assert.strictEqual(text, englishIndex);
});

test("italian index", async () => {
const italianIndex = await readFile(italianHtmlPath, "utf8");
const resp = await worker.fetch("", { headers: { "Accept-Language": "it" } });
const text = await resp.text();
const indexP = readFile(indexHtmlPath, "utf8");
const respP = worker.fetch("");
const [indexHtml, resp] = await Promise.all([indexP, respP]);

assert(resp.ok);
assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.statusText, "OK");
assert.strictEqual(resp.headers.get("Content-Language"), "it");
assert.strictEqual(text, italianIndex);

const text = await resp.text();
assert.strictEqual(text, indexHtml);
});

test("rss feed success", async () => {
Expand Down
6 changes: 2 additions & 4 deletions cf/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import englishIndexHtml from "../lib/english.html";
import { mkFetchHandler } from "../lib/handler.js";
import italianIndexHtml from "../lib/italian.html";
import indexHtml from "../lib/index.html";
import * as logger from "../lib/logger.js";

type Env = {
Expand All @@ -19,8 +18,7 @@ export default (<ExportedHandler<Env>>{

const fetchFn = fetch.bind(globalThis);
const fetchHandler = mkFetchHandler({
englishIndexHtml,
italianIndexHtml,
indexHtml,
baseUrl,
raiBaseUrl,
poolSize,
Expand Down
12 changes: 0 additions & 12 deletions lib/english.html

This file was deleted.

26 changes: 5 additions & 21 deletions lib/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { readFile } from "node:fs/promises";
import * as path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
Expand All @@ -14,44 +13,29 @@ import { parseFeed } from "./test/parse-feed.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const englishHtmlPath = path.join(__dirname, "english.html");
const italianHtmlPath = path.join(__dirname, "italian.html");
const indexHtmlPath = path.join(__dirname, "index.html");

const baseUrl = new URL("https://test.dev/");
const raiBaseUrl = new URL("https://rai.dev/");
const indexHtml = readFileSync(indexHtmlPath, "utf8");

const fetchHandler = mkFetchHandler({
englishIndexHtml: readFileSync(englishHtmlPath, "utf8"),
italianIndexHtml: readFileSync(italianHtmlPath, "utf8"),
indexHtml,
baseUrl,
raiBaseUrl,
poolSize: 1,
fetch: fetchFn,
logger: logger.disabled,
});

test("english index", async () => {
const englishIndex = await readFile(englishHtmlPath, "utf8");
test("index", async () => {
const req = new Request("http://localhost/");
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.headers.get("Content-Language"), "en");
const text = await resp.text();
assert.strictEqual(text, englishIndex);
});

test("italian index", async () => {
const italianIndex = await readFile(italianHtmlPath, "utf8");
const req = new Request("http://localhost/", {
headers: { "Accept-Language": "it" },
});
const resp = await fetchHandler(req);

assert.strictEqual(resp.status, 200);
assert.strictEqual(resp.headers.get("Content-Language"), "it");
const text = await resp.text();
assert.strictEqual(text, italianIndex);
assert.strictEqual(text, indexHtml);
});

test("rss feed success", async () => {
Expand Down
9 changes: 5 additions & 4 deletions lib/handler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Router, createResponse, error, html, text } from "itty-router";
import { feedHandler } from "./feed-handler.js";
import { indexHandler } from "./index-handler.js";
import { Logger } from "./logger.js";

export type FetchHandlerConfig = {
englishIndexHtml: string;
italianIndexHtml: string;
indexHtml: string;
baseUrl: URL;
raiBaseUrl: URL;
poolSize: number;
Expand All @@ -15,7 +13,7 @@ export type FetchHandlerConfig = {

type FetchHandler = (req: Request) => Promise<Response>;
export function mkFetchHandler(conf: FetchHandlerConfig): FetchHandler {
const fetchIndex = (request: Request) => indexHandler(conf, request);
const fetchIndex = () => index(conf.indexHtml);
const fetchFeed = (request: Request) => feedHandler(conf, request);

const router = Router()
Expand All @@ -30,4 +28,7 @@ export function mkFetchHandler(conf: FetchHandlerConfig): FetchHandler {
});
}

function index(indexHtml: string): Response {
return html(indexHtml, { headers: { "Content-Language": "it" } });
}
const notFound = () => new Response("Not found.", { status: 404 });
39 changes: 0 additions & 39 deletions lib/index-handler.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions lib/index-handler.ts

This file was deleted.

File renamed without changes.

0 comments on commit ee517c3

Please sign in to comment.