-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControllerTests.scala
219 lines (172 loc) · 7.84 KB
/
ControllerTests.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
package com.springer.samatra.testing.asynchttp
import java.security.Principal
import javax.servlet._
import javax.servlet.http.HttpServletResponse
import com.springer.samatra.routing.Routings.Routes
import org.asynchttpclient.AsyncHttpClient
import org.scalatest.matchers.should.Matchers._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.funspec.AnyFunSpec
import org.scalatest.BeforeAndAfterAll
import scala.jdk.CollectionConverters.CollectionHasAsScala
class ControllerTests extends AnyFunSpec with ScalaFutures with RoutesFixtures with BeforeAndAfterAll with InMemoryBackend {
val http: AsyncHttpClient = client(new ServerConfig {
mount("/*", new Filter {
override def init(filterConfig: FilterConfig): Unit = ()
override def destroy(): Unit = ()
override def doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain): Unit = {
response.asInstanceOf[HttpServletResponse].addHeader("Content-Encoding", "gzip")
chain.doFilter(request, response)
}
}, userPrincipal = Some(new Principal() {
override def getName: String = "Sam"
}))
mount("/*", Routes(basic))
mount("/regex/*", Routes(regex))
mount("/caching/*", Routes(caching))
mount("/future/*", Routes(futures))
})
override protected def afterAll(): Unit = http.close()
describe("Caching") {
it("Adds no-store for noStore strategy") {
val noStore = http.prepareGet("/caching/no-store").execute().get
noStore.getHeader("Cache-Control") shouldBe "no-store"
}
it("Adds visibility and max-age headers for noRevalidate strategy") {
val noRevalidate = http.prepareGet("/caching/no-revalidate").execute().get
noRevalidate.getHeader("Cache-Control") shouldBe "public, max-age=600"
noRevalidate.getHeader("Expires") should not be null
}
it("Adds Weak ETag support") {
val res = http.prepareGet("/caching/weakEtag/sam").execute().get
res.getStatusCode shouldBe 200
res.getHeader("Cache-Control") shouldBe "no-cache, private"
val etag: String = res.getHeader("ETag")
etag should startWith("W/\"")
val res2 = http.prepareGet(s"/caching/weakEtag/andy").setHeaders(Map("If-None-Match" -> Seq(etag))).execute().get
res2.getStatusCode shouldBe 200
val res3 = http.prepareGet(s"/caching/weakEtag/sam").setHeaders(Map("If-None-Match" -> Seq(etag))).execute().get
res3.getStatusCode shouldBe 304
}
it("Adds Strong ETag support") {
val res = http.prepareGet("/caching/etag/sam").execute().get
res.getStatusCode shouldBe 200
res.getHeader("Cache-Control") shouldBe "no-cache, private"
val etag: String = res.getHeader("ETag")
etag should not be null
val res2 = http.prepareGet(s"/caching/etag/andy").setHeaders(Map("If-None-Match" -> Seq(etag))).execute().get
res2.getStatusCode shouldBe 200
val res3 = http.prepareGet(s"/caching/etag/sam").setHeaders(Map("If-None-Match" -> Seq(etag))).execute().get
res3.getStatusCode shouldBe 304
}
}
describe("Routes") {
it("should return 404 for not found route") {
val res = http.prepareGet("/querystringmap?1=a&1=b&2=c&3=%2623").execute().get()
res.getResponseBody shouldBe "1->ab|2->c|3->&23"
}
it("should give query string map") {
val res = http.prepareGet("/missing").execute().get
res.getStatusCode shouldBe 404
}
it("should give cookie from header") {
val res = http.prepareGet("/cookieFromRequest").setHeader("Cookie", "cookie=hello").execute().get
res.getResponseBody shouldBe "hello"
}
it("should return unicode") {
val res = http.prepareGet("/unicode").execute().get
res.getResponseBody shouldBe "Почему это не рабосаетdafafdafdadfadfadf"
}
it("should return headers only for HEAD") {
val res = http.prepareHead("/missing").execute().get
res.getStatusCode shouldBe 404
val resp = http.prepareHead("/head").execute().get
resp.getStatusCode shouldBe 200
resp.getHeader("header") shouldBe "value"
// resp.getHeader("Content-Length") shouldBe "0"
val resp2 = http.prepareHead("/future/morethanone/string").execute().get
resp2.getStatusCode shouldBe 200
resp2.getHeader("Content-Length") shouldBe "6"
}
it("should return 405 for invalid method") {
val wrong = http.preparePost("/himynameis/Sam").execute().get
wrong.getStatusCode shouldBe 405
wrong.getHeader("Allow") shouldBe "GET, HEAD"
val missing = http.preparePost("/missing").execute().get
missing.getStatusCode shouldBe 404
val post = http.preparePost("/post").setBody("body").execute().get
post.getResponseBody shouldBe "body"
}
it("HEAD should return 200, 302, 404 and 500 error codes") {
val error = http.prepareHead("/future/morethanone/Error").execute().get
error.getStatusCode shouldBe 500
val notFound = http.prepareHead("/future/morethanone/NotFound").execute().get
notFound.getStatusCode shouldBe 404
val redirect = http.prepareHead("/future/morethanone/redirect").execute().get
redirect.getStatusCode shouldBe 302
val string = http.prepareHead("/future/morethanone/string").execute().get
string.getStatusCode shouldBe 200
val headers = http.prepareHead("/future/morethanone/headers").execute().get
headers.getHeader("hi") shouldBe "there"
val cs = http.prepareHead("/future/morethanone/cookies").execute().get
val cookie = cs.getCookies.asScala.collectFirst {
case c if c.name == "cookie" => c.value
}
cookie shouldBe Some("tasty")
}
}
it("should return 500 for timeout") {
val res = http.prepareGet("/future/timeout").execute().get
res.getStatusCode shouldBe 500
res.getResponseBody should include("java.util.concurrent.TimeoutException")
}
it("should parse path params") {
val res = http.prepareGet("/himynameis/Sam").execute().get
res.getResponseBody shouldBe "hi Sam"
}
it("should not URL decode parsed path params") {
val res = http.prepareGet("/himynameis/Sam%2FOwen").execute().get
res.getResponseBody shouldBe "hi Sam%2FOwen"
}
it("should parse regex params") {
val res = http.prepareGet("/regex/year/2000").execute().get
res.getResponseBody shouldBe "hell0 the year 2000"
}
it("should not URL decode parsed regex params") {
val res = http.prepareGet("/regex/date/01%2F01%2F2000").execute().get
res.getResponseBody shouldBe "hell0 the date 01%2F01%2F2000"
}
it("should be able to get and post from same uri") {
val get = http.prepareGet("/getandpost").execute().get
get.getResponseBody shouldBe "get"
val post = http.preparePost("/getandpost").execute().get
post.getResponseBody shouldBe "post"
}
it("should be able to set cookies") {
val res = http.prepareGet("/future/morethanone/cookies").execute().get
val head = res.getCookies.asScala.head
head.value shouldBe "tasty"
head.isHttpOnly shouldBe true
}
it("pathInfo should not include servlet path") {
val res = http.prepareGet("/future/morethanone/pathInfo").execute().get
res.getResponseBody shouldBe "/morethanone/pathInfo"
}
it("should be able to retrieve request uri") {
val res = http.prepareGet("/uri?foo=bar#qunx").execute().get
res.getResponseBody should endWith("/uri?foo=bar")
}
it("should be able to gte user principle") {
val res = http.prepareGet("/himyusernameis").execute().get
res.getResponseBody shouldBe "hi Sam"
}
it("should be able to use GzipHandler") {
def shouldBeGzipped(path: String): Unit = {
val res = http.prepareGet(s"$path").setHeaders(Map("Accept-Encoding" -> Seq("gzip"))).execute().get
res.getHeader("Content-Encoding") shouldBe "gzip"
}
shouldBeGzipped("/file")
shouldBeGzipped("/future/morethanone/file")
shouldBeGzipped("/caching/etag/andy")
}
}