forked from http4s/http4s
/
PathSpec.scala
335 lines (292 loc) · 8.84 KB
/
PathSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Copyright 2013-2020 http4s.org
*
* SPDX-License-Identifier: Apache-2.0
*
* Based on https://github.com/twitter/finagle/blob/6e2462acc32ac753bf4e9d8e672f9f361be6b2da/finagle-http/src/test/scala/com/twitter/finagle/http/path/PathSpec.scala
* Copyright 2017, Twitter Inc.
*/
package org.http4s
package dsl
import cats.effect.IO
import org.http4s.Uri.uri
import org.http4s.dsl.io._
import org.scalacheck.Arbitrary
import org.scalacheck.Arbitrary.arbitrary
import scala.util.Try
class PathSpec extends Http4sSpec {
implicit val arbitraryPath: Arbitrary[Path] =
Arbitrary {
arbitrary[List[String]].map(Path(_))
}
"Path" should {
"/foo/bar" in {
Path("/foo/bar") must_== Path("foo", "bar")
}
"foo/bar" in {
Path("foo/bar") must_== Path("foo", "bar")
}
"//foo/bar" in {
Path("//foo/bar") must_== Path("", "foo", "bar")
}
"~ extractor on Path" in {
(Path("/foo.json") match {
case Root / "foo" ~ "json" => true
case _ => false
}) must beTrue
}
"~ extractor on filename foo.json" in {
("foo.json" match {
case "foo" ~ "json" => true
case _ => false
}) must beTrue
}
"~ extractor on filename foo" in {
("foo" match {
case "foo" ~ "" => true
case _ => false
}) must beTrue
}
"-> extractor /test.json" in {
val req = Request[IO](method = Method.GET, uri = uri("/test.json"))
(req match {
case GET -> Root / "test.json" => true
case _ => false
}) must beTrue
}
"-> extractor /foo/test.json" in {
val req = Request[IO](method = Method.GET, uri = uri("/foo/test.json"))
(req match {
case GET -> Root / "foo" / "test.json" => true
case _ => false
}) must beTrue
}
"→ extractor /test.json" in {
val req = Request[IO](method = Method.GET, uri = uri("/test.json"))
(req match {
case GET → (Root / "test.json") => true
case _ => false
}) must beTrue
}
"request path info extractor for /" in {
val req = Request[IO](method = Method.GET, uri = uri("/"))
(req match {
case _ -> Root => true
case _ => false
}) must beTrue
}
"Root extractor" in {
(Path("/") match {
case Root => true
case _ => false
}) must beTrue
}
"Root extractor, no partial match" in {
(Path("/test.json") match {
case Root => true
case _ => false
}) must beFalse
}
"Root extractor, empty path" in {
(Path("") match {
case Root => true
case _ => false
}) must beTrue
}
"/ extractor" in {
(Path("/1/2/3/test.json") match {
case Root / "1" / "2" / "3" / "test.json" => true
case _ => false
}) must beTrue
}
"/: extractor" in {
(Path("/1/2/3/test.json") match {
case "1" /: "2" /: path => Some(path)
case _ => None
}) must_== Some(Path("/3/test.json"))
}
"/: should not crash without trailing slash" in {
// Bug reported on Gitter
Path("/cameras/1NJDOI") match {
case "cameras" /: _ /: "events" /: _ /: "exports" /: _ => false
case _ => true
}
}
"trailing slash" in {
(Path("/1/2/3/") match {
case Root / "1" / "2" / "3" / "" => true
case _ => false
}) must beTrue
}
"encoded chars" in {
(Path("/foo%20bar/and%2For/1%2F2") match {
case Root / "foo bar" / "and/or" / "1/2" => true
case _ => false
}) must beTrue
}
"encode chars in toString" in {
(Root / "foo bar" / "and/or" / "1/2").toString must_==
"/foo%20bar/and%2For/1%2F2"
}
"Int extractor" in {
(Path("/user/123") match {
case Root / "user" / IntVar(userId) => userId == 123
case _ => false
}) must beTrue
}
"Int extractor, invalid int" in {
(Path("/user/invalid") match {
case Root / "user" / IntVar(userId @ _) => true
case _ => false
}) must beFalse
}
"Int extractor, number format error" in {
(Path("/user/2147483648") match {
case Root / "user" / IntVar(userId @ _) => true
case _ => false
}) must beFalse
}
"Long extractor" >> {
"valid" >> {
"small positive number" in {
(Path("/user/123") match {
case Root / "user" / LongVar(userId) => userId == 123
case _ => false
}) must beTrue
}
"negative number" in {
(Path("/user/-432") match {
case Root / "user" / LongVar(userId) => userId == -432
case _ => false
}) must beTrue
}
}
"invalid" >> {
"a word" in {
(Path("/user/invalid") match {
case Root / "user" / LongVar(userId @ _) => true
case _ => false
}) must beFalse
}
"number but out of domain" in {
(Path("/user/9223372036854775808") match {
case Root / "user" / LongVar(userId @ _) => true
case _ => false
}) must beFalse
}
}
}
"UUID extractor" >> {
"valid" >> {
"a UUID" in {
(Path("/user/13251d88-7a73-4fcf-b935-54dfae9f023e") match {
case Root / "user" / UUIDVar(userId) =>
userId.toString == "13251d88-7a73-4fcf-b935-54dfae9f023e"
case _ => false
}) must beTrue
}
}
"invalid" >> {
"a number" in {
(Path("/user/123") match {
case Root / "user" / UUIDVar(userId @ _) => true
case _ => false
}) must beFalse
}
"a word" in {
(Path("/user/invalid") match {
case Root / "user" / UUIDVar(userId @ _) => true
case _ => false
}) must beFalse
}
"a bad UUID" in {
(Path("/user/13251d88-7a73-4fcf-b935") match {
case Root / "user" / UUIDVar(userId @ _) => true
case _ => false
}) must beFalse
}
}
}
"Matrix extractor" >> {
object BoardExtractor
extends impl.MatrixVar("square", List("x", "y"), s => Try(s.toInt).toOption)
object EmptyExtractor extends impl.MatrixVar[List, String]("square", List.empty, s => Some(s))
"valid" >> {
"a matrix var" in {
(Path("/board/square;x=42;y=0") match {
case Root / "board" / BoardExtractor(x, y) if x == 42 && y == 0 => true
case _ => false
}) must beTrue
}
"a matrix var mid path" in {
(Path("/board/square;x=42;y=0/piece") match {
case Root / "board" / BoardExtractor(x, y) / "piece" if x == 42 && y == 0 => true
case _ => false
}) must beTrue
}
"an empty matrix var but why?" in {
(Path("/board/square") match {
case Root / "board" / EmptyExtractor() => true
case _ => false
}) must beTrue
}
}
"invalid" >> {
"empty with semi" in {
(Path("/board/square;") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"empty without semi" in {
(Path("/board/square") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"empty with mismatched name" in {
(Path("/board/other") match {
case Root / "board" / EmptyExtractor() => true
case _ => false
}) must beFalse
}
"empty axis" in {
(Path("/board/square;;y=0") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"empty too many = in axis" in {
(Path("/board/square;x=42=0;y=9") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"unparseable" in {
(Path("/board/square;x=42;y=soda") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"not enough axes" in {
(Path("/board/square;x=42") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
"to many axes" in {
(Path("/board/square;x=42y=0;z=39") match {
case Root / "board" / BoardExtractor(x @ _, y @ _) => true
case _ => false
}) must beFalse
}
}
}
"consistent apply / toList" in prop { (p: Path) =>
Path(p.toList) must_== p
}
"Path.apply is stack safe" in {
Path("/" * 1000000) must beAnInstanceOf[Path]
}
}
}