Skip to content

Commit

Permalink
fix: don't throw a 401 when hitting /docs/
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Oct 18, 2023
1 parent 3fcba3e commit defa20c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ export function mapUrlPathToBucketPath(
env: Pick<Env, 'DIRECTORY_LISTING'>
): string | undefined {
const urlToBucketPathMap: Record<string, string> = {
dist: DIST_PATH_PREFIX + url.pathname.substring(5),
download: DOWNLOAD_PATH_PREFIX + url.pathname.substring(9),
api: API_PATH_PREFIX + (url.pathname.substring(4) || '/'),
dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),
download:
DOWNLOAD_PATH_PREFIX +
(url.pathname.substring('/download'.length) || '/'),
docs: DOCS_PATH_PREFIX + (url.pathname.substring('/docs'.length) || '/'),
api: API_PATH_PREFIX + (url.pathname.substring('/api'.length) || '/'),
metrics: url.pathname.substring(1), // substring to cut off the /
};

Expand Down
23 changes: 18 additions & 5 deletions tests/e2e/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ async function startS3Mock(): Promise<http.Server> {
// later tests. If so, return a S3 response indicating that
// the path exists. Otherwise return a S3 response indicating
// that the path doesn't exist
if (
['nodejs/release/', 'nodejs/', 'metrics/'].includes(
url.searchParams.get('prefix')!
)
) {
const r2Prefix = url.searchParams.get('prefix')!;

let doesFolderExist =
['nodejs/release/', 'nodejs/', 'nodejs/docs/', 'metrics/'].includes(
r2Prefix
) || r2Prefix.endsWith('/docs/api/');

if (doesFolderExist) {
xmlFilePath += 'ListObjectsV2-exists.xml';
} else {
xmlFilePath += 'ListObjectsV2-does-not-exist.xml';
Expand Down Expand Up @@ -107,6 +110,16 @@ describe('Directory Tests (Restricted Directory Listing)', () => {
assert.strictEqual(res.status, 200);
});

it('allows `/docs/`', async () => {
const res = await mf.dispatchFetch(`${url}docs/`);
assert.strictEqual(res.status, 200);
});

it('allows `/api/`', async () => {
const res = await mf.dispatchFetch(`${url}api/`);
assert.strictEqual(res.status, 200);
});

it('redirects `/metrics` to `/metrics/`', async () => {
const originalRes = await mf.dispatchFetch(`${url}metrics`, {
redirect: 'manual',
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('mapUrlPathToBucketPath', () => {
const result = mapUrlPathToBucketPath(new URL('http://localhost/dist'), {
DIRECTORY_LISTING: 'restricted',
});
assert.strictEqual(result, 'nodejs/release');
assert.strictEqual(result, 'nodejs/release/');
});

it('converts `/dist/latest` to `nodejs/release/latest`', () => {
Expand All @@ -64,7 +64,7 @@ describe('mapUrlPathToBucketPath', () => {
DIRECTORY_LISTING: 'restricted',
}
);
assert.strictEqual(result, 'nodejs');
assert.strictEqual(result, 'nodejs/');
});

it('converts `/download/releases` to `nodejs/releases`', () => {
Expand Down

0 comments on commit defa20c

Please sign in to comment.