From bef9f45a28b2f7a4fca026846c65905961014e75 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:02:37 -0800 Subject: [PATCH] fix tests Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com> --- scripts/build-r2-symlinks.mjs | 2 +- src/middleware/subtituteMiddleware.ts | 2 +- src/providers/r2Provider.ts | 3 ++- tests/e2e/directory.test.ts | 23 ++++++++++++++----- tests/e2e/test-data/expected-html/dist.txt | 6 ++--- .../expected-s3/ListObjectsV2-exists.xml | 4 ++-- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/scripts/build-r2-symlinks.mjs b/scripts/build-r2-symlinks.mjs index 29e02f3..38a36b0 100644 --- a/scripts/build-r2-symlinks.mjs +++ b/scripts/build-r2-symlinks.mjs @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/usr/bin/env node 'use strict'; import { join } from 'node:path'; diff --git a/src/middleware/subtituteMiddleware.ts b/src/middleware/subtituteMiddleware.ts index 7fb37fe..4258b48 100644 --- a/src/middleware/subtituteMiddleware.ts +++ b/src/middleware/subtituteMiddleware.ts @@ -39,6 +39,6 @@ export class SubtitutionMiddleware implements Middleware { new Request(request) ); - return this.router.handle(substitutedRequest, ctx); + return this.router.handle(substitutedRequest, ctx, request.urlObj); } } diff --git a/src/providers/r2Provider.ts b/src/providers/r2Provider.ts index 6c4b725..019c702 100644 --- a/src/providers/r2Provider.ts +++ b/src/providers/r2Provider.ts @@ -87,7 +87,8 @@ export class R2Provider implements Provider { options?: ReadDirectoryOptions ): Promise { if (path in CACHED_DIRECTORIES) { - // @ts-expect-error + // @ts-expect-error dates may not be parsed at this point, we take care + // of it below const result: ReadDirectoryResult = CACHED_DIRECTORIES[path]; for (const file of result.files) { diff --git a/tests/e2e/directory.test.ts b/tests/e2e/directory.test.ts index 1bbecfd..e529ca8 100644 --- a/tests/e2e/directory.test.ts +++ b/tests/e2e/directory.test.ts @@ -1,6 +1,6 @@ import { after, before, describe, it } from 'node:test'; import assert from 'node:assert'; -import { readFileSync, writeFileSync } from 'node:fs'; +import { readFileSync } from 'node:fs'; import { readFile } from 'node:fs/promises'; import http from 'http'; import { Miniflare } from 'miniflare'; @@ -24,7 +24,7 @@ async function startS3Mock(): Promise { const r2Prefix = url.searchParams.get('prefix')!; let doesFolderExist = - ['nodejs/release/', 'nodejs/', 'nodejs/docs/', 'metrics/'].includes( + ['nodejs/release/v1.0.0/', 'nodejs/', 'nodejs/docs/', 'metrics/'].includes( r2Prefix ) || r2Prefix.endsWith('/docs/api/'); @@ -76,15 +76,26 @@ describe('Directory Tests (Restricted Directory Listing)', () => { }); it('redirects `/dist` to `/dist/` and returns expected html', async () => { - const [originalRes, expectedHtml] = await Promise.all([ - mf.dispatchFetch(`${url}dist`, { redirect: 'manual' }), + const originalRes = await + mf.dispatchFetch(`${url}dist`, { redirect: 'manual' }) + + assert.strictEqual(originalRes.status, 301); + const res = await mf.dispatchFetch(originalRes.headers.get('location')!); + assert.strictEqual(res.status, 200); + assert.strictEqual( + res.headers.get('cache-control'), + 'public, max-age=3600, s-maxage=14400' + ); + }); + + it('`/dist/v1.0.0/` returns expected html', async () => { + const [res, expectedHtml] = await Promise.all([ + mf.dispatchFetch(`${url}dist/v1.0.0/`), readFile('./tests/e2e/test-data/expected-html/dist.txt', { encoding: 'utf-8', }), ]); - assert.strictEqual(originalRes.status, 301); - const res = await mf.dispatchFetch(originalRes.headers.get('location')!); assert.strictEqual(res.status, 200); assert.strictEqual( res.headers.get('cache-control'), diff --git a/tests/e2e/test-data/expected-html/dist.txt b/tests/e2e/test-data/expected-html/dist.txt index 0725de9..43a623e 100644 --- a/tests/e2e/test-data/expected-html/dist.txt +++ b/tests/e2e/test-data/expected-html/dist.txt @@ -1,6 +1,6 @@ - Index of /dist/ + Index of /dist/v1.0.0/ -

Index of /dist/


../
+

Index of /dist/v1.0.0/


../
 latest/                                                           -                   -
-index.json                                         12 Sept 2023, 05:43                 18 B
+index.json                                         12 Sept 2023, 05:43                 18 B
 

diff --git a/tests/e2e/test-data/expected-s3/ListObjectsV2-exists.xml b/tests/e2e/test-data/expected-s3/ListObjectsV2-exists.xml index 5bed3e2..6787ab9 100644 --- a/tests/e2e/test-data/expected-s3/ListObjectsV2-exists.xml +++ b/tests/e2e/test-data/expected-s3/ListObjectsV2-exists.xml @@ -5,11 +5,11 @@ 1000 false - nodejs/release/latest/ + nodejs/release/v1.0.0/latest/ "asd123" - nodejs/release/index.json + nodejs/release/v1.0.0/index.json 2023-09-12T05:43:00.000Z 18