| 1 | import { assert, it } from "@effect/vitest" |
| 2 | import { FindMyWay as Router } from "effect/http" |
| 3 | |
| 4 | it("Test route with optional parameter", () => { |
| 5 | const router = Router.make<boolean>() |
| 6 | |
| 7 | router.on("GET", "/a/:param/b/:optional?", true) |
| 8 | |
| 9 | assert.deepStrictEqual(router.find("GET", "/a/foo-bar/b")?.params, { |
| 10 | param: "foo-bar" |
| 11 | }) |
| 12 | assert.deepStrictEqual(router.find("GET", "/a/foo-bar/b/foo")?.params, { |
| 13 | param: "foo-bar", |
| 14 | optional: "foo" |
| 15 | }) |
| 16 | }) |
| 17 | |
| 18 | it("Test for duplicate route with optional param", () => { |
| 19 | const router = Router.make<boolean>() |
| 20 | router.on("GET", "/foo/:bar?", true) |
| 21 | assert.throws(() => router.on("GET", "/foo", true)) |
| 22 | }) |
| 23 | |
| 24 | it("Test for param with ? not at the end", () => { |
| 25 | const router = Router.make<boolean>() |
| 26 | |
| 27 | assert.throws(() => router.on("GET", "/foo/:bar?/baz", true)) |
| 28 | }) |
| 29 | |
| 30 | it("Multi parametric route with optional param", () => { |
| 31 | const router = Router.make<boolean>() |
| 32 | |
| 33 | router.on("GET", "/a/:p1-:p2?", true) |
| 34 | |
| 35 | assert.deepStrictEqual(router.find("GET", "/a/foo-bar-baz")?.params, { |
| 36 | p1: "foo-bar", |
| 37 | p2: "baz" |
| 38 | }) |
| 39 | assert.deepStrictEqual(router.find("GET", "/a")?.params, {}) |
| 40 | }) |
| 41 | |
| 42 | it("Optional parameter at root", () => { |
| 43 | const router = Router.make<boolean>() |
| 44 | |
| 45 | router.on("GET", "/:optional?", true) |
| 46 | |
| 47 | assert.deepStrictEqual(router.find("GET", "/")?.params, {}) |
| 48 | assert.deepStrictEqual(router.find("GET", "/foo")?.params, { |
| 49 | optional: "foo" |
| 50 | }) |
| 51 | }) |
| 52 | |
| 53 | it("Optional Parameter with ignoreTrailingSlash = true", () => { |
| 54 | const router = Router.make<boolean>({ |
| 55 | ignoreTrailingSlash: true, |
| 56 | ignoreDuplicateSlashes: false |
| 57 | }) |
| 58 | |
| 59 | router.on("GET", "/test/hello/:optional?", true) |
| 60 | |
| 61 | assert.deepStrictEqual(router.find("GET", "/test/hello/")?.params, {}) |
| 62 | assert.deepStrictEqual(router.find("GET", "/test/hello")?.params, {}) |
| 63 | assert.deepStrictEqual(router.find("GET", "/test/hello/foo")?.params, { |
| 64 | optional: "foo" |
| 65 | }) |
| 66 | assert.deepStrictEqual(router.find("GET", "/test/hello/foo/")?.params, { |
| 67 | optional: "foo" |
| 68 | }) |
| 69 | }) |
| 70 | |
| 71 | it("Optional Parameter with ignoreTrailingSlash = false", () => { |
| 72 | const router = Router.make<boolean>({ |
| 73 | ignoreTrailingSlash: false, |
| 74 | ignoreDuplicateSlashes: false |
| 75 | }) |
| 76 | |
| 77 | router.on("GET", "/test/hello/:optional?", true) |
| 78 | |
| 79 | assert.deepStrictEqual(router.find("GET", "/test/hello/")?.params, { |
| 80 | optional: "" |
| 81 | }) |
| 82 | assert.deepStrictEqual(router.find("GET", "/test/hello")?.params, {}) |
| 83 | assert.deepStrictEqual(router.find("GET", "/test/hello/foo")?.params, { |
| 84 | optional: "foo" |
| 85 | }) |
| 86 | assert.isUndefined(router.find("GET", "/test/hello/foo/")) |
| 87 | }) |
| 88 | |
| 89 | it("Optional Parameter with ignoreDuplicateSlashes = true", () => { |
| 90 | const router = Router.make<boolean>({ |
| 91 | ignoreDuplicateSlashes: true |
| 92 | }) |
| 93 | |
| 94 | router.on("GET", "/test/hello/:optional?", true) |
| 95 | |
| 96 | assert.deepStrictEqual(router.find("GET", "/test//hello")?.params, {}) |
| 97 | assert.deepStrictEqual(router.find("GET", "/test/hello")?.params, {}) |
| 98 | assert.deepStrictEqual(router.find("GET", "/test/hello/foo")?.params, { |
| 99 | optional: "foo" |
| 100 | }) |
| 101 | assert.deepStrictEqual(router.find("GET", "/test//hello//foo")?.params, { |
| 102 | optional: "foo" |
| 103 | }) |
| 104 | }) |
| 105 | |
| 106 | it("Optional Parameter with ignoreDuplicateSlashes = false", () => { |
| 107 | const router = Router.make<boolean>({ |
| 108 | ignoreDuplicateSlashes: false |
| 109 | }) |
| 110 | |
| 111 | router.on("GET", "/test/hello/:optional?", true) |
| 112 | |
| 113 | assert.isUndefined(router.find("GET", "/test//hello")) |
| 114 | assert.deepStrictEqual(router.find("GET", "/test/hello")?.params, {}) |
| 115 | assert.deepStrictEqual(router.find("GET", "/test/hello/foo")?.params, { |
| 116 | optional: "foo" |
| 117 | }) |
| 118 | assert.isUndefined(router.find("GET", "/test//hello//foo")) |
| 119 | }) |