-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
UriHandlingSpec.scala
105 lines (96 loc) · 3.39 KB
/
UriHandlingSpec.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
/*
* Copyright (C) Lightbend Inc. <https://www.lightbend.com>
*/
package play.it.http
import org.specs2.execute.AsResult
import org.specs2.specification.core.Fragment
import play.api.BuiltInComponents
import play.api.mvc._
import play.api.routing.Router
import play.api.routing.sird
import play.api.test.ApplicationFactories
import play.api.test.PlaySpecification
import play.core.server.ServerEndpoint
import play.it.test._
class UriHandlingSpec
extends PlaySpecification
with EndpointIntegrationSpecification
with OkHttpEndpointSupport
with ApplicationFactories {
private def makeRequest[T: AsResult](path: String)(block: (ServerEndpoint, okhttp3.Response) => T): Fragment =
withRouter { (components: BuiltInComponents) =>
import components.{ defaultActionBuilder => Action }
import sird.UrlContext
Router.from {
case sird.GET(p"/path") =>
Action { (request: Request[_]) =>
Results.Ok(request.queryString)
}
case _ =>
Action { (request: Request[_]) =>
Results.Ok(request.path + queryToString(request.queryString))
}
}
}.withAllOkHttpEndpoints { (okEndpoint: OkHttpEndpoint) =>
val response: okhttp3.Response = okEndpoint.call(path)
block(okEndpoint.endpoint, response)
}
private def queryToString(qs: Map[String, Seq[String]]) = {
val queryString = qs.map { case (key, value) => key + "=" + value.sorted.mkString("|,|") }.mkString("&")
if (queryString.nonEmpty) "?" + queryString else ""
}
"Server" should {
"preserve order of repeated query string parameters" in makeRequest(
"/path?a=1&b=1&b=2&b=3&b=4&b=5"
) {
case (endpoint, response) => {
response.body.string must_== "a=1&b=1&b=2&b=3&b=4&b=5"
}
}
/*
"handle '/pat/resources/BodhiApplication?where={%22name%22:%22hsdashboard%22}' as a valid URI" in makeRequest(
"/pat/resources/BodhiApplication?where={%22name%22:%22hsdashboard%22}"
) {
case (endpoint, response) => {
response.body.string must_=== """/pat/resources/BodhiApplication?where={"name":"hsdashboard"}"""
}
}
*/
"handle '/dynatable/?queries%5Bsearch%5D=%7B%22condition%22%3A%22AND%22%2C%22rules%22%3A%5B%5D%7D&page=1&perPage=10&offset=0' as a URI" in makeRequest(
"/dynatable/?queries%5Bsearch%5D=%7B%22condition%22%3A%22AND%22%2C%22rules%22%3A%5B%5D%7D&page=1&perPage=10&offset=0"
) {
case (endpoint, response) => {
response.body.string must_=== """/dynatable/?queries[search]={"condition":"AND","rules":[]}&page=1&perPage=10&offset=0"""
}
}
"handle '/foo%20bar.txt' as a URI" in makeRequest(
"/foo%20bar.txt"
) {
case (endpoint, response) =>
response.body.string must_=== """/foo%20bar.txt"""
}
"handle '/?filter=a&filter=b' as a URI" in makeRequest(
"/?filter=a&filter=b"
) {
case (endpoint, response) => {
response.body.string must_=== """/?filter=a|,|b"""
}
}
"handle '/?filter=a,b' as a URI" in makeRequest(
"/?filter=a,b"
) {
case (endpoint, response) => {
response.body.string must_=== """/?filter=a,b"""
}
}
/*
"handle '/pat?param=%_D%' as a URI with an invalid query string" in makeRequest(
"/pat?param=%_D%"
) {
case (endpoint, response) => {
response.body.string must_=== """/pat"""
}
}
*/
}
}