Skip to content

Commit

Permalink
Always redirect http to https instead of doing work over http (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhdev authored Jan 5, 2025
1 parent 0313c04 commit 5058bde
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
5 changes: 4 additions & 1 deletion Caddyfile.base
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
admin off
}

{$DOMAIN:localhost}:80,
{$DOMAIN:localhost}:80 {
redir https://{host}{uri} permanent
}

{$DOMAIN:localhost}:443 {
redir /abc https://example.com
@snapshot path_regexp ^/snapshot/(\d\d\d\d-\d\d-\d\d)/(.*)$
Expand Down
128 changes: 64 additions & 64 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,73 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

const domain = "localhost" as const;
const delpaGitHubRawBaseUrl =
"https://raw.githubusercontent.com/delpa-org" as const;

// Test both http and https
for (const hostAddress of [
"http://localhost:3000",
"https://localhost:3001",
] as const) {
describe(`${hostAddress.slice(0, hostAddress.indexOf("/") - 1)}: /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",
},
);
test("Redirect http to https", async () => {
const hostAddress = `http://${domain}:3000`;
const response = await fetch(`${hostAddress}/some/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(`https://${domain}/some/path`);
});

for (const [name, path] of [
["non-existing with no subdir", "non-existing"],
[
"non-existing with no subdir but with a trailing slash",
"non-existing/",
],
["non-existing with a one-level subdir", "non-existing/a"],
[
"non-existing with a one-level subdir with a trailing slash",
"non-existing/a/",
],
["non-existing with a two-level subdir", "non-existing/a/b"],
[
"non-existing with a two-level subdir with a trailing slash",
"non-existing/a/b/",
],
] as const) {
test(`Return 404 with invalid URL under /shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotFirstPathComp}/${path}`,
{
redirect: "manual",
},
);
describe("/snapshot", () => {
const hostAddress = `https://${domain}:3001`;
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(404);
expect(response.headers.get("content-type")).toContain("text/plain");
expect(await response.text()).toBe("404 Not Found");
});
}
});
}
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
),
);
});
}

for (const [name, path] of [
["non-existing with no subdir", "non-existing"],
["non-existing with no subdir but with a trailing slash", "non-existing/"],
["non-existing with a one-level subdir", "non-existing/a"],
[
"non-existing with a one-level subdir with a trailing slash",
"non-existing/a/",
],
["non-existing with a two-level subdir", "non-existing/a/b"],
[
"non-existing with a two-level subdir with a trailing slash",
"non-existing/a/b/",
],
] as const) {
test(`Return 404 with invalid URL under /shapshot: ${name}`, async () => {
const response = await fetch(
`${hostAddress}/${snapshotFirstPathComp}/${path}`,
{
redirect: "manual",
},
);

expect(response.status).toBe(404);
expect(response.headers.get("content-type")).toContain("text/plain");
expect(await response.text()).toBe("404 Not Found");
});
}
});

0 comments on commit 5058bde

Please sign in to comment.