|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert"; |
| 3 | +import lambda from "./index.js"; |
| 4 | + |
| 5 | +function invokeHandler(uri) { |
| 6 | + return new Promise((res) => { |
| 7 | + const cb = (_, response) => { |
| 8 | + res(response); |
| 9 | + }; |
| 10 | + |
| 11 | + lambda.handler( |
| 12 | + { |
| 13 | + Records: [ |
| 14 | + { |
| 15 | + cf: { |
| 16 | + request: { |
| 17 | + uri, |
| 18 | + }, |
| 19 | + }, |
| 20 | + }, |
| 21 | + ], |
| 22 | + }, |
| 23 | + undefined, |
| 24 | + cb |
| 25 | + ); |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +test("example expected redirects", async (t) => { |
| 30 | + const redirects = [ |
| 31 | + // root |
| 32 | + { from: "/", to: "https://www.rust-lang.org/learn" }, |
| 33 | + { from: "/index.html", to: "https://www.rust-lang.org/learn" }, |
| 34 | + // crate redirects |
| 35 | + { from: "/regex", to: "https://docs.rs/regex" }, |
| 36 | + { from: "/log", to: "https://docs.rs/log" }, |
| 37 | + { from: "/rand/0.9.1/rand/", to: "https://docs.rs/rand/0.9.1/rand/" }, |
| 38 | + // trpl |
| 39 | + { from: "/trpl", to: "/stable/book" }, |
| 40 | + { from: "/stable/trpl/hello", to: "/stable/book/hello" }, |
| 41 | + { from: "/nightly/trpl", to: "/nightly/book" }, |
| 42 | + // adv-book |
| 43 | + { from: "/adv-book", to: "/stable/nomicon" }, |
| 44 | + { from: "/stable/adv-book/hello", to: "/stable/nomicon/hello" }, |
| 45 | + { from: "/nightly/adv-book", to: "/nightly/nomicon" }, |
| 46 | + // master |
| 47 | + { from: "/master/hello", to: "/nightly/hello" }, |
| 48 | + // /doc |
| 49 | + { from: "/doc/std", to: "/std" }, |
| 50 | + { from: "/doc/hello", to: "/hello" }, |
| 51 | + ]; |
| 52 | + |
| 53 | + for (const redir of redirects) { |
| 54 | + await t.test(`redirect ${redir.from}`, async (t) => { |
| 55 | + const response = await invokeHandler(redir.from); |
| 56 | + |
| 57 | + assert(["301", "302"].includes(response.status)); |
| 58 | + |
| 59 | + const location = response.headers.location[0]; |
| 60 | + assert.strictEqual(location.key, "Location"); |
| 61 | + |
| 62 | + assert.strictEqual(location.value, redir.to); |
| 63 | + }); |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +test("ensure the default rewrite to /stable", async (t) => { |
| 68 | + const request = await invokeHandler("/std"); |
| 69 | + assert.strictEqual(request.uri, "/stable/std"); |
| 70 | +}); |
0 commit comments