/
ResponseUtils.scala
62 lines (46 loc) · 1.66 KB
/
ResponseUtils.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
package com.twitter.finatra.utils
import com.twitter.finagle.httpx.Response
import com.twitter.finagle.httpx.Status
import com.twitter.finagle.httpx.Status._
object ResponseUtils {
def is5xxResponse(response: Response) = {
errorClass(response) == 5
}
def is4xxOr5xxResponse(response: Response) = {
val errClass = errorClass(response)
errClass == 4 || errClass == 5
}
def expectOkResponse(response: Response): Unit = {
expectResponseStatus(response)(Ok)
}
def expectOkResponse(response: Response, withBody: String = null): Unit = {
expectResponseStatus(response)(Ok, withBody)
}
def expectForbiddenResponse(response: Response): Unit = {
expectResponseStatus(response)(Forbidden)
}
def expectForbiddenResponse(response: Response, withBody: String = null): Unit = {
expectResponseStatus(response)(Forbidden, withBody)
}
def expectNotFoundResponse(response: Response): Unit = {
expectResponseStatus(response)(NotFound)
}
def expectNotFoundResponse(response: Response, withBody: String = null): Unit = {
expectResponseStatus(response)(NotFound, withBody)
}
/* Private */
private def errorClass(response: Response): Int = {
response.statusCode / 100
}
private def expectResponseStatus(
response: Response)(
expectedStatus: Status = null,
withBody: String = null): Unit = {
assert(
expectedStatus == null || response.status == expectedStatus,
"Expected " + expectedStatus + " but received " + response.status)
assert(
withBody == null || response.contentString == withBody,
"Expected body " + withBody + " but received \"" + response.contentString + "\"")
}
}