From 1928bfcb5ec01e8b01b35ff23b5932b1a8f645d7 Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Wed, 25 Jun 2025 20:24:51 -0700 Subject: [PATCH] fix: ensure typescript routes are hit most to least specific --- deno.json | 4 ++-- deno.lock | 1 + handlers/typescript.ts | 4 ++-- tests/07_test_route_sorting.test.ts | 27 ++++++++++++++++++++------- tests/www/echo/___input/index.ts | 3 ++- tests/www/echo/index.ts | 8 ++++++++ 6 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 tests/www/echo/index.ts diff --git a/deno.json b/deno.json index 389eed6..cf6a7d0 100644 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@andyburke/serverus", "description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.", - "version": "0.4.0", + "version": "0.5.0", "license": "MIT", "exports": { ".": "./serverus.ts", @@ -14,7 +14,7 @@ "tasks": { "lint": "deno lint", "fmt": "deno fmt", - "test": "DENO_ENV=test DATA_STORAGE_ROOT=./tests/data/$(date --iso-8601=seconds) deno test --allow-env --allow-read --allow-write --allow-net --trace-leaks --fail-fast ./tests/", + "test": "DENO_ENV=test DATA_STORAGE_ROOT=./tests/data/$(date --iso-8601=seconds) SERVERUS_TYPESCRIPT_IMPORT_LOGGING=1 deno test --allow-env --allow-read --allow-write --allow-net --trace-leaks --fail-fast ./tests/", "build": "deno compile --allow-env --allow-read --allow-write --allow-net --include handlers ./serverus.ts -o serverus", "serverus": "deno --allow-env --allow-read --allow-write --allow-net ./serverus.ts" }, diff --git a/deno.lock b/deno.lock index c3e0878..439f6c5 100644 --- a/deno.lock +++ b/deno.lock @@ -17,6 +17,7 @@ "jsr:@std/fs@^1.0.17": "1.0.18", "jsr:@std/html@^1.0.4": "1.0.4", "jsr:@std/http@^1.0.13": "1.0.17", + "jsr:@std/internal@*": "1.0.8", "jsr:@std/internal@^1.0.6": "1.0.8", "jsr:@std/internal@^1.0.8": "1.0.8", "jsr:@std/media-types@^1.1.0": "1.1.0", diff --git a/handlers/typescript.ts b/handlers/typescript.ts index d240818..4efb38a 100644 --- a/handlers/typescript.ts +++ b/handlers/typescript.ts @@ -96,7 +96,7 @@ export default async function handle_typescript(request: Request): Promise = { + '': 'echo! .... echo. ................... echo?', + hi: 'hello', + yo: 'yo', + 'whoa there': 'whoa there' + }; - const body = await response.text(); + for await (const key of Object.keys(echoes)) { + const response = await fetch(`http://${test_server_info.hostname}:${test_server_info.port}/echo/${key}`, { + method: 'GET' + }); - asserts.assert(response.ok); - asserts.assert(body); - asserts.assertEquals(body, 'hello'); + const body = await response.text(); + + asserts.assert(response.ok); + asserts.assert(body); + console.dir({ + body, + key + }); + asserts.assertEquals(body, echoes[key]); + } } finally { Deno.env.delete('SERVERUS_ROOT'); if (test_server_info) { diff --git a/tests/www/echo/___input/index.ts b/tests/www/echo/___input/index.ts index bdfe527..bd9fea3 100644 --- a/tests/www/echo/___input/index.ts +++ b/tests/www/echo/___input/index.ts @@ -1,5 +1,6 @@ export function GET(_req: Request, meta: Record): Response { - return new Response(meta.params.input ?? '', { + const input = decodeURIComponent(meta.params.input ?? ''); + return new Response(input, { status: 200, headers: { 'Content-Type': 'text/plain' diff --git a/tests/www/echo/index.ts b/tests/www/echo/index.ts new file mode 100644 index 0000000..8ea62b0 --- /dev/null +++ b/tests/www/echo/index.ts @@ -0,0 +1,8 @@ +export function GET(_req: Request, _meta: Record): Response { + return new Response('echo! .... echo. ................... echo?', { + status: 200, + headers: { + 'Content-Type': 'text/plain' + } + }); +}