-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleTest.scala
143 lines (114 loc) · 4.05 KB
/
ExampleTest.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
package com.springer.samatra.testing.unit
import java.net.URLEncoder.encode
import java.security.Principal
import java.time.{Clock, LocalDate, ZoneId, ZoneOffset}
import java.util.concurrent.Executors
import com.springer.samatra.routing.Routings.{Controller, HttpResp, Routes}
import com.springer.samatra.routing.StandardResponses._
import com.springer.samatra.testing.unit.ControllerTestHelpers._
import javax.servlet.http.{Cookie, HttpServletRequest, HttpServletResponse}
import org.scalatest.matchers.should.Matchers._
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.funspec.AnyFunSpec
import scala.concurrent.{ExecutionContext, Future}
class ExampleTest extends AnyFunSpec with ScalaFutures {
implicit val ex: ExecutionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(5))
val routes: Routes = new Controller {
import com.springer.samatra.routing.FutureResponses.Implicits.fromFuture
import com.springer.samatra.routing.StandardResponses.Implicits.fromString
put("/put") { req =>
Future {
Redirect(req.queryStringParamValue("to"))
}
}
get("/error") { _ =>
Future {
Halt(500, Some(new RuntimeException("Error message")))
}
}
get("/request-response") { _ =>
(req: HttpServletRequest, resp: HttpServletResponse) => {
resp.setDateHeader("Date", LocalDate.of(2017, 5, 18).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli)
resp.setStatus(200)
resp.getWriter.print("sam")
}
}
get("/futurebug") { _ =>
Future {
"hello"
}
}
get("/user") { req =>
req.underlying.getUserPrincipal.getName
}
get("/hello/:name") { req =>
Future {
WithCookies(AddCookie("cookie", req.cookie("cookie").get)) {
WithHeaders("a" -> "b") {
req.captured("name")
}
}
}
}
}
describe("An example of unit testing controllers") {
implicit val clock: Clock = Clock.tickMinutes(ZoneId.of("GMT"))
it("should put") {
val result = routes.put(
"/put",
body = encode("to=/xml/hi", "UTF-8").getBytes,
headers = Map("Content-Type" -> Seq("application/x-www-form-urlencoded")))
val (statusCode, hs, _, _) = result.run()
statusCode shouldBe 302
hs("Location") shouldBe Seq("/xml/hi")
}
it("should map http resp inside future") {
val result = routes.get("/futurebug")
val (_, _, _, bytes) = result.run()
new String(bytes) shouldBe "hello"
}
it("should test with helper methods") {
val result = routes.get("/request-response")
val (statusCode, headers, _, body) = result.run()
statusCode shouldBe 200
headers("Date") shouldBe Seq("Thu, 18 05 2017 12:00:00 GMT")
new String(body) shouldBe "sam"
}
it("set user principle") {
val principal = new Principal {
override def getName: String = "sam"
}
val (_, _, _, body) = routes.get("/user", userPrincipal = Some(principal)).run()
new String(body) shouldBe "sam"
}
it("should test future string") {
val result: HttpResp = unwrapFutureResp(routes.get("/hello/sam", cookies = Seq(new Cookie("cookie", "expectedValue"))))
result shouldBe
WithCookies(AddCookie("cookie", "expectedValue")) {
WithHeaders("a" -> "b") {
StringResp("sam")
}
}
}
it("returns 404 on no match") {
routes.get("/nomatch") shouldBe Halt(404)
}
it("writes errors to output stream") {
val (statusCode, _, _, body) = routes.get("/error").run()
statusCode shouldBe 500
new String(body) should include("Error message")
}
it("returns 405 on on MethodNotAllowed") {
val result = routes.put("/hello/sam", body = "body".getBytes)
result shouldBe WithHeaders("Allow" -> "GET, HEAD") {
Halt(405)
}
}
it("blows up on no match") {
routes.get("/nomatch") shouldBe Halt(404)
}
it("bug - future of not http response") {
routes.get("/nomatch") shouldBe Halt(404)
}
}
}