From 7ccb9d7b6917e9609c540129cd566d853646020c Mon Sep 17 00:00:00 2001 From: Andy Burke Date: Fri, 8 Aug 2025 17:18:02 -0700 Subject: [PATCH] test: ensure PRECHECKS are working for static file handling --- deno.json | 2 +- tests/01_static_files.test.ts | 118 ++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/deno.json b/deno.json index cd9a55e..14d4ab1 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.11.2", + "version": "0.11.3", "license": "MIT", "exports": { ".": "./serverus.ts", diff --git a/tests/01_static_files.test.ts b/tests/01_static_files.test.ts index 5f501e7..800aed7 100644 --- a/tests/01_static_files.test.ts +++ b/tests/01_static_files.test.ts @@ -477,6 +477,124 @@ Deno.test({ } }); +Deno.test({ + name: 'allow overriding prechecks', + permissions: { + env: true, + read: true, + write: true, + net: true + }, + + sanitizeResources: false, + sanitizeOps: false, + + fn: async () => { + let test_server_info: EPHEMERAL_SERVER | null = null; + const cwd = Deno.cwd(); + + const static_file_handler = await import('../handlers/static.ts'); + const PREVIOUS_PRECHECKS = static_file_handler.PRECHECKS.PUT?.slice(0); + try { + Deno.chdir('./tests/www'); + test_server_info = await get_ephemeral_listen_server(); + + const PRECHECKS = static_file_handler.PRECHECKS.PUT = PREVIOUS_PRECHECKS ? [...PREVIOUS_PRECHECKS] : []; + PRECHECKS.push((request: Request): Response | undefined => { + const has_required_header = request.headers.get('x-required-header'); + + if (!has_required_header) { + return Response.json({ + error: { + message: 'Missing required header.', + cause: 'missing_required_header' + } + }, { + status: 400 + }); + } + }); + + const failed_put_body = new FormData(); + failed_put_body.append( + 'file', + new File(['this is the test content for precheck overrides'], 'test_put_upload_that_should_fail.txt') + ); + const failed_put_response = await fetch( + `http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_fail.txt`, + { + method: 'PUT', + body: failed_put_body + } + ); + + asserts.assert(!failed_put_response.ok); + + const failed_body = await failed_put_response.json(); + asserts.assert(failed_body); + asserts.assert(failed_body.error); + asserts.assertEquals(failed_body.error.cause, 'missing_required_header'); + + const successful_put_body = new FormData(); + successful_put_body.append( + 'file', + new File(['this is the test content for precheck overrides'], 'test_put_upload_that_should_not_fail.txt') + ); + const successful_put_response = await fetch( + `http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt`, + { + method: 'PUT', + headers: { + 'x-required-header': 'true' + }, + body: successful_put_body + } + ); + + asserts.assert(successful_put_response.ok); + + const successful_body = await successful_put_response.json(); + asserts.assert(successful_body); + asserts.assert(successful_body.written); + asserts.assertEquals( + successful_body.written[0], + `http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt` + ); + + const get_response = await fetch( + `http://${test_server_info.hostname}:${test_server_info.port}/files/test_put_upload_that_should_not_fail.txt`, + { + method: 'GET' + } + ); + + asserts.assert(get_response.ok); + asserts.assert(get_response.body); + + const local_download_path = path.join(Deno.cwd(), 'files', 'test_put_upload_that_should_not_fail.txt-downloaded'); + await ensureFile(local_download_path); + + const file = await Deno.open(local_download_path, { truncate: true, write: true }); + await get_response.body.pipeTo(file.writable); + + const download_content = await Deno.readTextFile(local_download_path); + asserts.assert(download_content); + asserts.assertEquals(download_content, 'this is the test content for precheck overrides'); + + await Deno.remove(local_download_path); + asserts.assert(!fs.existsSync(local_download_path)); + } finally { + if (PREVIOUS_PRECHECKS) { + static_file_handler.PRECHECKS.PUT = PREVIOUS_PRECHECKS; + } + Deno.chdir(cwd); + if (test_server_info) { + await test_server_info?.server?.stop(); + } + } + } +}); + Deno.test({ name: 'these methods should not work on static files', permissions: {