Skip to content

Commit

Permalink
Test both http and https (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhdev authored Jan 5, 2025
1 parent 9842d1f commit e1583a7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ node_modules
*.tsbuildinfo
/coverage
README.md
/*.test.ts
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
FROM node:22.12.0-alpine as builder

COPY package.json package-lock.json ./
RUN npm install
RUN npm install -g npm && npm install
COPY . .
RUN npx tsx gen_caddy.ts < Caddyfile.base > Caddyfile

Expand Down
61 changes: 35 additions & 26 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,39 @@
const delpaGitHubRawBaseUrl =
"https://raw.githubusercontent.com/delpa-org" as const;

describe("/snapshot", () => {
const snapshotFirstPathComp = "snapshot" as const;
for (const [name, path] of [
["valid with a one-level subdir", "2025-01-02/a"],
["valid with a one-level subdir with a trailing slash", "2025-01-02/a/"],
["valid with a two-level subdir", "2025-01-02/a/b"],
["valid with a two-level subdir with a trailing slash", "2025-01-02/a/b/"],
] as const) {
test(`Redirect with valid URL under /shapshot: ${name}`, async () => {
const response = await fetch(
`http://localhost:3000/${snapshotFirstPathComp}/${path}`,
{
redirect: "manual",
},
);
// Test both http and https
for (const hostAddress of [
"http://localhost:3000",
"https://localhost:3001",
] as const) {
describe("/snapshot", () => {
const snapshotFirstPathComp = "snapshot" as const;
for (const [name, path] of [
["valid with a one-level subdir", "2025-01-02/a"],
["valid with a one-level subdir with a trailing slash", "2025-01-02/a/"],
["valid with a two-level subdir", "2025-01-02/a/b"],
[
"valid with a two-level subdir with a trailing slash",
"2025-01-02/a/b/",
],
] as const) {
test(`Redirect with valid URL under /shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotFirstPathComp}/${path}`,
{
redirect: "manual",
},
);

expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe(
delpaGitHubRawBaseUrl +
"/melpa-snapshot-2025-01-02/refs/heads/master/packages/" +
path.slice(
path.indexOf("/") + 1, // Remove the top-level folder in path
),
);
});
}
});
expect(response.status).toBe(301);
expect(response.headers.get("location")).toBe(
delpaGitHubRawBaseUrl +
"/melpa-snapshot-2025-01-02/refs/heads/master/packages/" +
path.slice(
path.indexOf("/") + 1, // Remove the top-level folder in path
),
);
});
}
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint": "eslint *.ts *.mjs && prettier . --check && caddy fmt --diff Caddyfile.base",
"build": "podman build . -t app",
"start": "podman run -it --rm -p 3000:80 -p 3001:443 app",
"test": "vitest --run index.test.ts",
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 vitest --run index.test.ts",
"format": "eslint --fix *.ts *.mjs && prettier . --write && caddy fmt --overwrite Caddyfile.base",
"compile": "tsc -p tsconfig.json"
},
Expand Down

0 comments on commit e1583a7

Please sign in to comment.