Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't throw a 401 when hitting /docs/ #51

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading