forked from akka/akka-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathDirectivesSpec.scala
More file actions
593 lines (516 loc) · 22.4 KB
/
PathDirectivesSpec.scala
File metadata and controls
593 lines (516 loc) · 22.4 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.scaladsl.server.directives
import java.util.concurrent.atomic.AtomicInteger
import scala.collection.immutable.ListMap
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server._
import org.scalatest.Inside
class PathDirectivesSpec extends RoutingSpec with Inside {
val echoUnmatchedPath = extractUnmatchedPath { echoComplete }
def echoCaptureAndUnmatchedPath[T]: T => Route =
capture => ctx => ctx.complete(capture.toString + ":" + ctx.unmatchedPath)
"""path("foo")""" should {
val test = testFor(path("foo") { echoUnmatchedPath })
"reject [/bar]" inThe test()
"reject [/foobar]" inThe test()
"reject [/foo/bar]" inThe test()
"accept [/foo] and clear the unmatchedPath" inThe test("")
"reject [/foo/]" inThe test()
}
"""pathPrefix("")""" should {
val test = testFor(pathPrefix("") { echoUnmatchedPath })
// Should match everything because pathPrefix is used and "" is a neutral element.
"accept [/] and clear the unmatchedPath=" inThe test("")
"accept [/foo] and clear the unmatchedPath" inThe test("foo")
"accept [/foo/] and clear the unmatchedPath" inThe test("foo/")
"accept [/bar/] and clear the unmatchedPath" inThe test("bar/")
}
"""path("" | "foo")""" should {
val test = testFor(path("" | "foo") { echoUnmatchedPath })
// Should not match anything apart of "/", because path requires whole path being matched.
"accept [/] and clear the unmatchedPath=" inThe test("")
"reject [/foo]" inThe test()
"reject [/foo/]" inThe test()
"reject [/bar/]" inThe test()
}
"""path("") ~ path("foo")""" should {
val test = testFor(path("")(echoUnmatchedPath) ~ path("foo")(echoUnmatchedPath))
// Should match both because ~ operator is used for two exclusive routes.
"accept [/] and clear the unmatchedPath=" inThe test("")
"accept [/foo] and clear the unmatchedPath=" inThe test("")
}
"""path("foo"./)""" should {
val test = testFor(path("foo"./) { echoUnmatchedPath })
"reject [/foo]" inThe test()
"accept [/foo/] and clear the unmatchedPath" inThe test("")
}
"""path("")""" should {
val test = testFor(path("") { echoUnmatchedPath })
"reject [/foo]" inThe test()
"accept [/] and clear the unmatchedPath" in test("")
}
"""path(Map("a" -> 1, "aa" -> 2))""" should {
val test = testFor(path(Map("a" -> 1, "aa" -> 2)) { echoCaptureAndUnmatchedPath })
"accept [/a]" inThe test("1:")
"accept [/aa]" inThe test("2:")
"reject [/aaa]" inThe test()
}
"""path(Map("sv_SE" -> 3, "sv" -> 1, "sv_FI" -> 2))""" should {
val test = testFor(path(Map("sv_SE" -> 3, "sv" -> 1, "sv_FI" -> 2)) { echoCaptureAndUnmatchedPath })
"accept [/sv]" inThe test("1:")
"accept [/sv_FI]" inThe test("2:")
"accept [/sv_SE]" inThe test("3:")
"reject [/sv_DK]" inThe test()
}
"""path(Map("a" -> 1, "ab" -> 2, "ba" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "da" -> 7))""" should {
val test = testFor(path(Map("a" -> 1, "ab" -> 2, "ba" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "da" -> 7)) { echoCaptureAndUnmatchedPath })
"accept [/a]" inThe test("1:")
"accept [/ab]" inThe test("2:") // FAIL
"accept [/ba]" inThe test("3:")
"accept [/b]" inThe test("4:")
"accept [/c]" inThe test("5:")
"accept [/d]" inThe test("6:")
"accept [/da]" inThe test("7:")
"reject [/e]" inThe test()
"reject [/ac]" inThe test()
}
"""path(ListMap("a" -> 1, "ab" -> 2, "ba" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "da" -> 7))""" should {
val test = testFor(path(ListMap("a" -> 1, "ab" -> 2, "ba" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "da" -> 7)) { echoCaptureAndUnmatchedPath })
"accept [/a]" inThe test("1:")
"accept [/ab]" inThe test("2:")
"accept [/ba]" inThe test("3:")
"accept [/b]" inThe test("4:")
"accept [/c]" inThe test("5:")
"accept [/d]" inThe test("6:")
"accept [/da]" inThe test("7:")
"reject [/e]" inThe test()
"reject [/ac]" inThe test()
}
"""path(ListMap("a" -> 1, "aa" -> 2, "bb" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "dd" -> 7))""" should {
val test = testFor(path(ListMap("a" -> 1, "aa" -> 2, "bb" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "dd" -> 7)) { echoCaptureAndUnmatchedPath })
"accept [/a]" inThe test("1:")
"accept [/aa]" inThe test("2:")
"accept [/bb]" inThe test("3:")
"accept [/b]" inThe test("4:")
"accept [/c]" inThe test("5:")
"accept [/d]" inThe test("6:")
"accept [/dd]" inThe test("7:")
"reject [/e]" inThe test()
"reject [/ac]" inThe test()
}
"""path(Map("a" -> 1, "aa" -> 2, "bb" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "dd" -> 7))""" should {
val test = testFor(path(Map("a" -> 1, "aa" -> 2, "bb" -> 3, "b" -> 4, "c" -> 5, "d" -> 6, "dd" -> 7)) { echoCaptureAndUnmatchedPath })
"accept [/a]" inThe test("1:")
"accept [/aa]" inThe test("2:")
"accept [/bb]" inThe test("3:")
"accept [/b]" inThe test("4:")
"accept [/c]" inThe test("5:")
"accept [/d]" inThe test("6:")
"accept [/dd]" inThe test("7:")
"reject [/e]" inThe test()
"reject [/ac]" inThe test()
}
"""pathPrefix("foo")""" should {
val test = testFor(pathPrefix("foo") { echoUnmatchedPath })
"reject [/bar]" inThe test()
"accept [/foobar]" inThe test("bar")
"accept [/foo/bar]" inThe test("/bar")
"accept [/foo] and clear the unmatchedPath" inThe test("")
"accept [/foo/] and clear the unmatchedPath" inThe test("/")
}
"""pathPrefix("foo" / "bar")""" should {
val test = testFor(pathPrefix("foo" / "bar") { echoUnmatchedPath })
"reject [/bar]" inThe test()
"accept [/foo/bar]" inThe test("")
"accept [/foo/bar/baz]" inThe test("/baz")
}
"""pathPrefix(".*")""" should {
val test = testFor(pathPrefix(".*".r) { echoCaptureAndUnmatchedPath })
"accept [/]" inThe test(":")
"accept [/abc]" inThe test("abc:")
}
"""pathPrefix("(.)*.r")""" should {
val test = testFor(pathPrefix("(.)*".r) { echoCaptureAndUnmatchedPath })
"accept [/]" inThe test(":")
"accept [/abc]" inThe test("c:")
}
"""pathPrefix("ab[cd]+".r)""" should {
val test = testFor(pathPrefix("ab[cd]+".r) { echoCaptureAndUnmatchedPath })
"reject [/bar]" inThe test()
"reject [/ab/cd]" inThe test()
"accept [/abcdef]" inThe test("abcd:ef")
"accept [/abcdd/ef]" inThe test("abcdd:/ef")
}
"""pathPrefix("ab(cd)".r)""" should {
val test = testFor(pathPrefix("ab(cd)+".r) { echoCaptureAndUnmatchedPath })
"reject [/bar]" inThe test()
"reject [/ab/cd]" inThe test()
"accept [/abcdef]" inThe test("cd:ef")
"accept [/abcde/fg]" inThe test("cd:e/fg")
}
"pathPrefix(regex)" should {
"fail when the regex contains more than one group" in {
an[IllegalArgumentException] must be thrownBy path("a(b+)(c+)".r) { echoCaptureAndUnmatchedPath }
}
}
"pathPrefix(IntNumber)" should {
val test = testFor(pathPrefix(IntNumber) { echoCaptureAndUnmatchedPath })
"accept [/23]" inThe test("23:")
"accept [/12345yes]" inThe test("12345:yes")
"reject [/]" inThe test()
"reject [/abc]" inThe test()
"reject [/2147483648]" inThe test() // > Int.MaxValue
}
"pathPrefix(CustomShortNumber)" should {
object CustomShortNumber extends NumberMatcher[Short](Short.MaxValue, 10) {
def fromChar(c: Char) = fromDecimalChar(c)
}
val test = testFor(pathPrefix(CustomShortNumber) { echoCaptureAndUnmatchedPath })
"accept [/23]" inThe test("23:")
"accept [/12345yes]" inThe test("12345:yes")
"reject [/]" inThe test()
"reject [/abc]" inThe test()
"reject [/33000]" inThe test() // > Short.MaxValue
}
"pathPrefix(JavaUUID)" should {
val test = testFor(pathPrefix(JavaUUID) { echoCaptureAndUnmatchedPath })
"accept [/bdea8652-f26c-40ca-8157-0b96a2a8389d]" inThe test("bdea8652-f26c-40ca-8157-0b96a2a8389d:")
"accept [/bdea8652-f26c-40ca-8157-0b96a2a8389dyes]" inThe test("bdea8652-f26c-40ca-8157-0b96a2a8389d:yes")
"accept [/00000000-0000-0000-0000-000000000000]" inThe test("00000000-0000-0000-0000-000000000000:") // nil uuid
"accept [/733a018b-5f16-4699-0e1a-1d3f6f0d0315]" inThe test("733a018b-5f16-4699-0e1a-1d3f6f0d0315:") // variant 0
"accept [/733a018b-5f16-4699-fe1a-1d3f6f0d0315]" inThe test("733a018b-5f16-4699-fe1a-1d3f6f0d0315:") // future variant
"reject [/]" inThe test()
"reject [/abc]" inThe test()
}
"pathPrefix(Map(\"red\" -> 1, \"green\" -> 2, \"blue\" -> 3))" should {
val test = testFor(pathPrefix(Map("red" -> 1, "green" -> 2, "blue" -> 3)) { echoCaptureAndUnmatchedPath })
"accept [/green]" inThe test("2:")
"accept [/redsea]" inThe test("1:sea")
"reject [/black]" inThe test()
}
"pathPrefix(Map.empty)" should {
val test = testFor(pathPrefix(Map[String, Int]()) { echoCaptureAndUnmatchedPath })
"reject [/black]" inThe test()
}
"pathPrefix(Segment)" should {
val test = testFor(pathPrefix(Segment) { echoCaptureAndUnmatchedPath })
"accept [/abc]" inThe test("abc:")
"accept [/abc/]" inThe test("abc:/")
"accept [/abc/def]" inThe test("abc:/def")
"reject [/]" inThe test()
}
"pathPrefix(Segments)" should {
val test = testFor(pathPrefix(Segments) { echoCaptureAndUnmatchedPath })
"accept [/]" inThe test("List():")
"accept [/a/b/c]" inThe test("List(a, b, c):")
"accept [/a/b/c/]" inThe test("List(a, b, c):/")
}
"""pathPrefix(separateOnSlashes("a/b"))""" should {
val test = testFor(pathPrefix(separateOnSlashes("a/b")) { echoUnmatchedPath })
"accept [/a/b]" inThe test("")
"accept [/a/b/]" inThe test("/")
"reject [/a/c]" inThe test()
}
"""pathPrefix(separateOnSlashes("abc"))""" should {
val test = testFor(pathPrefix(separateOnSlashes("abc")) { echoUnmatchedPath })
"accept [/abc]" inThe test("")
"accept [/abcdef]" inThe test("def")
"reject [/ab]" inThe test()
}
"""pathPrefixTest("a" / Segment ~ Slash)""" should {
val test = testFor(pathPrefixTest("a" / Segment ~ Slash) { echoCaptureAndUnmatchedPath })
"accept [/a/bc/]" inThe test("bc:/a/bc/")
"reject [/a/bc]" inThe test()
"reject [/a/]" inThe test()
}
"""pathSuffix("edit" / Segment)""" should {
val test = testFor(pathSuffix("edit" / Segment) { echoCaptureAndUnmatchedPath })
"accept [/orders/123/edit]" inThe test("123:/orders/")
"reject [/orders/123/ed]" inThe test()
"reject [/edit]" inThe test()
}
"""pathSuffix("foo" / "bar" ~ "baz")""" should {
val test = testFor(pathSuffix("foo" / "bar" ~ "baz") { echoUnmatchedPath })
"accept [/orders/barbaz/foo]" inThe test("/orders/")
"reject [/orders/bazbar/foo]" inThe test()
}
"pathSuffixTest(Slash)" should {
val test = testFor(pathSuffixTest(Slash) { echoUnmatchedPath })
"accept [/]" inThe test("/")
"accept [/foo/]" inThe test("/foo/")
"reject [/foo]" inThe test()
}
"""pathPrefix("foo" | "bar")""" should {
val test = testFor(pathPrefix("foo" | "bar") { echoUnmatchedPath })
"accept [/foo]" inThe test("")
"accept [/foops]" inThe test("ps")
"accept [/bar]" inThe test("")
"reject [/baz]" inThe test()
}
"""pathPrefix(("foo" | "bar") / "example")""" should {
val test = testFor(pathPrefix(("foo" | "bar") / "example") { echoUnmatchedPath })
// nope: val test = testFor(pathPrefix("foo" | "bar" / "example") { echoUnmatchedPath })
"accept [/foo/example]" inThe test("")
"accept [/bar/example]" inThe test("")
"reject [/baz]" inThe test()
"reject [/bar/nein]" inThe test()
}
"""pathSuffix(!"foo")""" should {
val test = testFor(pathSuffix(!"foo") { echoUnmatchedPath })
"accept [/bar]" inThe test("/bar")
"reject [/foo]" inThe test()
}
"pathPrefix(IntNumber.?)" should {
val test = testFor(pathPrefix(IntNumber.?) { echoCaptureAndUnmatchedPath })
"accept [/12]" inThe test("Some(12):")
"accept [/12a]" inThe test("Some(12):a")
"accept [/foo]" inThe test("None:foo")
}
"""pathPrefix("foo".?)""" should {
val test = testFor(pathPrefix("foo".?) { echoUnmatchedPath })
"accept [/foo]" inThe test("")
"accept [/fool]" inThe test("l")
"accept [/bar]" inThe test("bar")
}
"""pathPrefix("foo") & pathEnd""" should {
val test = testFor((pathPrefix("foo") & pathEnd) { echoUnmatchedPath })
"reject [/foobar]" inThe test()
"reject [/foo/bar]" inThe test()
"accept [/foo] and clear the unmatchedPath" inThe test("")
"reject [/foo/]" inThe test()
}
"""pathPrefix("foo") & pathEndOrSingleSlash""" should {
val test = testFor((pathPrefix("foo") & pathEndOrSingleSlash) { echoUnmatchedPath })
"reject [/foobar]" inThe test()
"reject [/foo/bar]" inThe test()
"accept [/foo] and clear the unmatchedPath" inThe test("")
"accept [/foo/] and clear the unmatchedPath" inThe test("")
}
"""pathPrefix(IntNumber.repeat(separator = "."))""" should {
{
val test = testFor(pathPrefix(IntNumber.repeat(min = 2, max = 5, separator = ".")) { echoCaptureAndUnmatchedPath })
"reject [/foo]" inThe test()
"reject [/1foo]" inThe test()
"reject [/1.foo]" inThe test()
"accept [/1.2foo]" inThe test("List(1, 2):foo")
"accept [/1.2.foo]" inThe test("List(1, 2):.foo")
"accept [/1.2.3foo]" inThe test("List(1, 2, 3):foo")
"accept [/1.2.3.foo]" inThe test("List(1, 2, 3):.foo")
"accept [/1.2.3.4foo]" inThe test("List(1, 2, 3, 4):foo")
"accept [/1.2.3.4.foo]" inThe test("List(1, 2, 3, 4):.foo")
"accept [/1.2.3.4.5foo]" inThe test("List(1, 2, 3, 4, 5):foo")
"accept [/1.2.3.4.5.foo]" inThe test("List(1, 2, 3, 4, 5):.foo")
"accept [/1.2.3.4.5.6foo]" inThe test("List(1, 2, 3, 4, 5):.6foo")
"accept [/1.2.3.]" inThe test("List(1, 2, 3):.")
"accept [/1.2.3/]" inThe test("List(1, 2, 3):/")
"accept [/1.2.3./]" inThe test("List(1, 2, 3):./")
}
{
val test = testFor(pathPrefix(IntNumber.repeat(min = 1, max = Int.MaxValue, separator = ".")) { echoCaptureAndUnmatchedPath })
"reject [/baz]" inThe test()
"accept [/1baz]" inThe test("List(1):baz")
"accept [/1.baz]" inThe test("List(1):.baz")
"accept [/1.2baz]" inThe test("List(1, 2):baz")
"accept [/1.2.3.4.5.6.7]" inThe test("List(1, 2, 3, 4, 5, 6, 7):")
}
{
val test = testFor(pathPrefix(IntNumber.repeat(min = 0, max = 0, separator = ".")) { echoCaptureAndUnmatchedPath })
"accept [/qux]" inThe test("List():qux")
"accept [/1qux]" inThe test("List():1qux")
}
{
val test = testFor(pathPrefix(IntNumber.repeat(min = 0, max = 1, separator = ".")) { echoCaptureAndUnmatchedPath })
"accept [/xyz]" inThe test("List():xyz")
"accept [/1xyz]" inThe test("List(1):xyz")
"accept [/1]" inThe test("List(1):")
"accept [/1.xyz]" inThe test("List(1):.xyz")
"accept [/1.2.xyz]" inThe test("List(1):.2.xyz")
}
{
val test = testFor(path(IntNumber.repeat(min = 0, max = 2, separator = ".")) { echoCaptureAndUnmatchedPath })
"reject [/abc]" inThe test()
"reject [/1abc]" inThe test()
"reject [/1.abc]" inThe test()
"reject [/1.2.abc]" inThe test()
"reject [/1.2.3.abc]" inThe test()
"accept [/]" inThe test("List():")
"accept [/2]" inThe test("List(2):")
"accept [/3.4]" inThe test("List(3, 4):")
}
{
val test = testFor(pathPrefix(IntNumber.repeat(2, ".")) { echoCaptureAndUnmatchedPath })
"reject [/bar]" inThe test()
"reject [/1bar]" inThe test()
"reject [/1.bar]" inThe test()
"accept [/1.2bar]" inThe test("List(1, 2):bar")
"accept [/1.2.bar]" inThe test("List(1, 2):.bar")
"accept [/1.2.3bar]" inThe test("List(1, 2):.3bar")
}
}
"""rawPathPrefix(Slash ~ "a" / Segment ~ Slash)""" should {
val test = testFor(rawPathPrefix(Slash ~ "a" / Segment ~ Slash) { echoCaptureAndUnmatchedPath })
"accept [/a/bc/]" inThe test("bc:")
"reject [/a/bc]" inThe test()
"reject [/ab/]" inThe test()
}
"""rawPathPrefixTest(Slash ~ "a" / Segment ~ Slash)""" should {
val test = testFor(rawPathPrefixTest(Slash ~ "a" / Segment ~ Slash) { echoCaptureAndUnmatchedPath })
"accept [/a/bc/]" inThe test("bc:/a/bc/")
"reject [/a/bc]" inThe test()
"reject [/ab/]" inThe test()
}
"PathMatchers" should {
{
val test = testFor(path(Remaining.tmap { case Tuple1(s) => Tuple1(s.split('-').toList) }) { echoComplete })
"support the hmap modifier in accept [/yes-no]" inThe test("List(yes, no)")
}
{
val test = testFor(path(Remaining.map(_.split('-').toList)) { echoComplete })
"support the map modifier in accept [/yes-no]" inThe test("List(yes, no)")
}
{
val test = testFor(path(Remaining.tflatMap { case Tuple1(s) => Some(s).filter("yes".==).map(x => Tuple1(x)) }) { echoComplete })
"support the hflatMap modifier in accept [/yes]" inThe test("yes")
"support the hflatMap modifier in reject [/blub]" inThe test()
}
{
val test = testFor(path(Remaining.flatMap(s => Some(s).filter("yes".==))) { echoComplete })
"support the flatMap modifier in accept [/yes]" inThe test("yes")
"support the flatMap modifier reject [/blub]" inThe test()
}
}
implicit class WithIn(str: String) {
def inThe(f: String => Unit) = convertToWordSpecStringWrapper(str) in f(str)
def inThe(body: => Unit) = convertToWordSpecStringWrapper(str) in body
}
case class testFor(route: Route) {
def apply(expectedResponse: String = null): String => Unit = exampleString =>
"""(accept|reject)\s+\[([^\]]+)\]""".r.findFirstMatchIn(exampleString) match {
case Some(uri) =>
uri.group(1) match {
case "accept" if expectedResponse eq null =>
failTest("Example '" + exampleString + "' was missing an expectedResponse")
case "reject" if expectedResponse ne null =>
failTest("Example '" + exampleString + "' had an expectedResponse")
case _ =>
}
Get(uri.group(2)) ~> route ~> check {
if (expectedResponse eq null) handled shouldEqual false
else responseAs[String] shouldEqual expectedResponse
}
case None => failTest("Example '" + exampleString + "' doesn't contain a test uri")
}
}
import akka.http.scaladsl.model.StatusCodes._
"the Remaining path matcher" should {
"extract complete path if nothing previously consumed" in {
val route = path(Remaining) { echoComplete }
Get("/pets/afdaoisd/asda/sfasfasf/asf") ~> route ~> check { responseAs[String] shouldEqual "pets/afdaoisd/asda/sfasfasf/asf" }
}
"extract remaining path when parts of path already matched" in {
val route = path("pets" / Remaining) { echoComplete }
Get("/pets/afdaoisd/asda/sfasfasf/asf") ~> route ~> check { responseAs[String] shouldEqual "afdaoisd/asda/sfasfasf/asf" }
}
}
"the `redirectToTrailingSlashIfMissing` directive" should {
val route = redirectToTrailingSlashIfMissing(Found) { completeOk }
"pass if the request path already has a trailing slash" in {
Get("/foo/bar/") ~> route ~> check { response shouldEqual Ok }
}
"redirect if the request path doesn't have a trailing slash" in {
Get("/foo/bar") ~> route ~> checkRedirectTo("/foo/bar/")
}
"preserve the query and the frag when redirect" in {
Get("/foo/bar?query#frag") ~> route ~> checkRedirectTo("/foo/bar/?query#frag")
}
"redirect with the given redirection status code" in {
Get("/foo/bar") ~>
redirectToTrailingSlashIfMissing(MovedPermanently) { completeOk } ~>
check { status shouldEqual MovedPermanently }
Get("/foo/bar/") ~>
redirectToTrailingSlashIfMissing(MovedPermanently) { completeOk } ~>
check { status shouldEqual StatusCodes.OK }
}
}
"the `redirectToNoTrailingSlashIfPresent` directive" should {
val route = redirectToNoTrailingSlashIfPresent(Found) { completeOk }
"pass if the path is a single slash" in {
Get("/") ~> route ~> check { response shouldEqual Ok }
}
"pass if the request path already doesn't have a trailing slash" in {
Get("/foo/bar") ~> route ~> check { response shouldEqual Ok }
}
"redirect if the request path has a trailing slash" in {
Get("/foo/bar/") ~> route ~> checkRedirectTo("/foo/bar")
}
"preserve the query and the frag when redirect" in {
Get("/foo/bar/?query#frag") ~> route ~> checkRedirectTo("/foo/bar?query#frag")
}
"redirect with the given redirection status code" in {
Get("/foo/bar/") ~>
redirectToNoTrailingSlashIfPresent(MovedPermanently) { completeOk } ~>
check { status shouldEqual MovedPermanently }
}
}
"the `ignoreTrailingSlash` directive" should {
def route(counter: AtomicInteger = new AtomicInteger(0)) = ignoreTrailingSlash {
counter.incrementAndGet()
path("foo") {
complete(s"${counter.get()}")
} ~
(pathPrefix("bar") & pathEndOrSingleSlash) {
complete(s"${counter.get()}")
} ~
path("baz"./) {
complete(s"${counter.get()}")
}
}
"pass if the request path doesn't have a trailing slash" in {
Get("/foo") ~> route() ~> check {
responseAs[String] shouldEqual "1"
}
}
"pass if the request path has a trailing slash by retrying the inner route" in {
Get("/foo/") ~> route() ~> check {
responseAs[String] shouldEqual "2"
}
}
"pass when the request contains parameters and fragments" in {
Get("/foo/?query#frag") ~> route() ~> check {
responseAs[String] shouldEqual "2"
}
}
"retry the inner route only once if path already checks for an optional trailing slash" in {
Get("/bar/") ~> route() ~> check {
responseAs[String] shouldEqual "1"
}
}
"retry accordingly if the path expects a trailing slash" in {
Get("/baz") ~> route() ~> check {
responseAs[String] shouldEqual "2"
}
Get("/baz/") ~> route() ~> check {
responseAs[String] shouldEqual "1"
}
}
"reject if request can't be matched with nor without a trailing slash" in {
val counter = new AtomicInteger(0)
Get("/foz") ~> route(counter) ~> check {
counter.get() shouldEqual 2
handled shouldEqual false
}
}
}
import akka.http.scaladsl.model.headers.Location
import akka.http.scaladsl.model.Uri
private def checkRedirectTo(expectedUri: Uri) =
check {
status shouldBe a[Redirection]
inside(header[Location]) {
case Some(Location(uri)) =>
(if (expectedUri.isAbsolute) uri else uri.toRelative) shouldEqual expectedUri
}
}
}