Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Dec 22, 2024
1 parent 70f9e7a commit bef9f45
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion scripts/build-r2-symlinks.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!/usr/bin/env node
'use strict';

import { join } from 'node:path';
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/subtituteMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/providers/r2Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export class R2Provider implements Provider {
options?: ReadDirectoryOptions
): Promise<ReadDirectoryResult | undefined> {
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) {
Expand Down
23 changes: 17 additions & 6 deletions tests/e2e/directory.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,7 +24,7 @@ async function startS3Mock(): Promise<http.Server> {
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/');

Expand Down Expand Up @@ -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'),
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/test-data/expected-html/dist.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html><html>
<head>
<title>Index of /dist/</title>
<title>Index of /dist/v1.0.0/</title>
<style>
@media (prefers-color-scheme: dark) {
body {
Expand All @@ -17,8 +17,8 @@
</style>
</head>
<body>
<h1>Index of /dist/</h1><hr><pre><a href="../">../</a>
<h1>Index of /dist/v1.0.0/</h1><hr><pre><a href="../">../</a>
<a href="latest/">latest/</a> - -
<a href="/dist/index.json">index.json</a> 12 Sept 2023, 05:43 18 B
<a href="/dist/v1.0.0/index.json">index.json</a> 12 Sept 2023, 05:43 18 B
</pre><hr /></body>
</html>
4 changes: 2 additions & 2 deletions tests/e2e/test-data/expected-s3/ListObjectsV2-exists.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<CommonPrefixes>
<Prefix>nodejs/release/latest/</Prefix>
<Prefix>nodejs/release/v1.0.0/latest/</Prefix>
</CommonPrefixes>
<Contents>
<ETag>"asd123"</ETag>
<Key>nodejs/release/index.json</Key>
<Key>nodejs/release/v1.0.0/index.json</Key>
<LastModified>2023-09-12T05:43:00.000Z</LastModified>
<Size>18</Size>
</Contents>
Expand Down

0 comments on commit bef9f45

Please sign in to comment.