1import { assert, it } from "@effect/vitest"
2import { FindMyWay as Router } from "effect/http"
3
4const make = () =>
5 Router.make<boolean>({
6 caseSensitive: false
7 })
8
9it("case insensitive static routes of level 1", () => {
10 const router = make()
11 router.on("GET", "/woo", true)
12 assert(router.find("GET", "/woo")?.handler)
13})
14
15it("case insensitive static routes of level 2", () => {
16 const router = make()
17 router.on("GET", "/foo/woo", true)
18 assert(router.find("GET", "/FoO/WOO")?.handler)
19})
20
21it("case insensitive static routes of level 3", () => {
22 const router = make()
23 router.on("GET", "/foo/bar/woo", true)
24 assert(router.find("GET", "/Foo/bAR/WoO")?.handler)
25})
26
27it("parametric case insensitive", () => {
28 const router = make()
29 router.on("GET", "/foo/:param", true)
30 const result = router.find("GET", "/Foo/bAR")
31 assert(result)
32 assert(result.handler)
33 assert(result.params.param === "bAR")
34})
35
36it("parametric case insensitive with a static part", () => {
37 const router = make()
38 router.on("GET", "/foo/my-:param", true)
39 const result = router.find("GET", "/Foo/MY-bAR")
40 assert(result)
41 assert(result.handler)
42 assert(result.params.param === "bAR")
43})
44
45it("parametric case insensitive with capital letter", () => {
46 const router = make()
47 router.on("GET", "/foo/:Param", true)
48 const result = router.find("GET", "/Foo/bAR")
49 assert(result)
50 assert(result.handler)
51 assert(result.params.Param === "bAR")
52})
53
54it("case insensitive with capital letter in static path with param", () => {
55 const router = make()
56 router.on("GET", "/Foo/bar/:param", true)
57 const result = router.find("GET", "/foo/bar/baZ")
58 assert(result)
59 assert(result.handler)
60 assert(result.params.param === "baZ")
61})
62
63it("case insensitive with multiple paths containing capital letter in static path with param", () => {
64 const router = make()
65 router.on("GET", "/Foo/bar/:param", true)
66 router.on("GET", "/Foo/baz/:param", true)
67
68 let result = router.find("GET", "/foo/bar/baZ")
69 assert(result)
70 assert(result.handler)
71 assert(result.params.param === "baZ")
72
73 result = router.find("GET", "/foo/bar/baR")
74 assert(result)
75 assert(result.handler)
76 assert(result.params.param === "baR")
77})
78
79it("case insensitive with multiple mixed-case params within same slash couple", () => {
80 const router = make()
81 router.on("GET", "/foo/:param1-:param2", true)
82
83 const result = router.find("GET", "/FOO/My-bAR")
84 assert(result)
85 assert(result.handler)
86 assert(result.params.param1 === "My")
87 assert(result.params.param2 === "bAR")
88})
89
90it("case insensitive with multiple mixed-case params", () => {
91 const router = make()
92 router.on("GET", "/foo/:param1/:param2", true)
93
94 const result = router.find("GET", "/FOO/My/bAR")
95 assert(result)
96 assert(result.handler)
97 assert(result.params.param1 === "My")
98 assert(result.params.param2 === "bAR")
99})
100
101it("case insensitive with wildcard", () => {
102 const router = make()
103 router.on("GET", "/foo/*", true)
104
105 const result = router.find("GET", "/FOO/bAR")
106 assert(result)
107 assert(result.handler)
108 assert(result.params["*"] === "bAR")
109})
110
111it("parametric case insensitive with multiple routes", () => {
112 const router = make()
113 const tests = [
114 [
115 "POST",
116 "/foo/:param/Static/:userId/Save",
117 "/foo/bAR/static/one/SAVE",
118 {
119 param: "bAR",
120 userId: "one"
121 }
122 ],
123 [
124 "POST",
125 "/foo/:param/Static/:userId/Update",
126 "/fOO/Bar/Static/two/update",
127 {
128 param: "Bar",
129 userId: "two"
130 }
131 ],
132 [
133 "POST",
134 "/foo/:param/Static/:userId/CANCEL",
135 "/Foo/bAR/STATIC/THREE/cAnCeL",
136 {
137 param: "bAR",
138 userId: "THREE"
139 }
140 ]
141 ] as const
142
143 tests.forEach(([method, path]) => {
144 router.on(method, path, true)
145 })
146
147 tests.forEach(([method, , url, params]) => {
148 const result = router.find(method, url)
149 assert(result)
150 assert(result.handler)
151 assert.deepStrictEqual(result.params, params)
152 })
153})