-
Notifications
You must be signed in to change notification settings - Fork 86
Prevent infinite redirect for doc-router /doc[^/]
#751
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
terragrunt/modules/release-distribution/lambdas/doc-router/test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// A test that asserts that the redirects work correctly. | ||
// It can be run with | ||
// node --test test.mjs | ||
|
||
import { test } from "node:test"; | ||
import assert from "node:assert"; | ||
import lambda from "./index.js"; | ||
|
||
function invokeHandler(uri) { | ||
return new Promise((resolve) => { | ||
const callback = (_, response) => { | ||
resolve(response); | ||
}; | ||
|
||
// Invoke the lambda handler with an event containing the request URI | ||
lambda.handler( | ||
{ | ||
Records: [ | ||
{ | ||
cf: { | ||
request: { | ||
uri, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
undefined, | ||
callback | ||
); | ||
}); | ||
} | ||
|
||
test("example expected redirects", async (t) => { | ||
const redirects = [ | ||
// root | ||
{ from: "/", to: "/stable/" }, | ||
{ from: "/index.html", to: "/stable/" }, | ||
// crate redirects | ||
{ from: "/regex", to: "https://docs.rs/regex" }, | ||
{ from: "/log", to: "https://docs.rs/log" }, | ||
{ from: "/rand/0.9.1/rand/", to: "https://docs.rs/rand/0.9.1/rand/" }, | ||
// trpl | ||
{ from: "/trpl", to: "/stable/book" }, | ||
{ from: "/stable/trpl/hello", to: "/stable/book/hello" }, | ||
{ from: "/nightly/trpl", to: "/nightly/book" }, | ||
// adv-book | ||
{ from: "/adv-book", to: "/stable/nomicon" }, | ||
{ from: "/stable/adv-book/hello", to: "/stable/nomicon/hello" }, | ||
{ from: "/nightly/adv-book", to: "/nightly/nomicon" }, | ||
// master | ||
{ from: "/master/hello", to: "/nightly/hello" }, | ||
// /doc | ||
{ from: "/doc/std", to: "/std" }, | ||
{ from: "/doc/hello", to: "/hello" }, | ||
]; | ||
|
||
for (const redir of redirects) { | ||
await t.test(`redirect ${redir.from}`, async (t) => { | ||
const response = await invokeHandler(redir.from); | ||
|
||
assert(["301", "302"].includes(response.status)); | ||
|
||
const location = response.headers.location[0]; | ||
assert.strictEqual(location.key, "Location"); | ||
|
||
assert.strictEqual(location.value, redir.to); | ||
}); | ||
} | ||
}); | ||
|
||
test("ensure the default rewrite to /stable", async (t) => { | ||
const request = await invokeHandler("/std"); | ||
assert.strictEqual(request.uri, "/stable/std"); | ||
}); | ||
|
||
test("ensure no infinite redirect for /doc[^/]", async (t) => { | ||
const request = await invokeHandler("/docs/std"); | ||
assert.strictEqual(request.uri, "/stable/docs/std"); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.