-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProxyWithBodyTest.scala
More file actions
102 lines (85 loc) · 4.46 KB
/
ProxyWithBodyTest.scala
File metadata and controls
102 lines (85 loc) · 4.46 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
package com.lifeway.aws.lambda
import com.lifeway.aws.lambda.Errors
import java.io.ByteArrayOutputStream
import com.amazonaws.services.lambda.runtime.Context
import com.lifeway.aws.lambda.Proxy.Response
import io.circe._
import io.circe.syntax._
import io.circe.parser._
import utest._
object ProxyWithBodyTest extends TestSuite with ProxyEncoder with LambdaTestUtils {
val tests = Tests {
'ProxyWithBody - {
'ValidInput - {
val output = new ByteArrayOutputStream()
val handler = new WithBodyTestHandleReturnSuccess
val apiGatewayReq = makeRequest[InputDataTest](InputDataTest("input data!"))
val apiGatewayReqString =
apiGatewayReq.asJson(APIGatewayProxyRequest.encode[InputDataTest](InputDataTest.encoder)).toString()
handler.handler(streamFromString(apiGatewayReqString), output, makeContext())
val outputJson = parse(output.toString).right.get
val expectedJson = parse("""{
| "statusCode": 200,
| "headers": null,
| "body": "{\"data\":\"we have liftoff!\"}",
| "isBase64Encoded": false
|}""".stripMargin).right.get
assert(outputJson == expectedJson)
}
'InvalidInput - {
val output = new ByteArrayOutputStream()
val handler = new WithBodyTestHandleReturnSuccess
val apiGatewayReq = makeRequest[BadInputData](BadInputData("input data!"))
val apiGatewayReqString =
apiGatewayReq.asJson(APIGatewayProxyRequest.encode[BadInputData](BadInputData.encoder)).toString()
handler.handler(streamFromString(apiGatewayReqString), output, makeContext())
val outputJson = parse(output.toString).right.get
val expectedJson = parse("""{
| "statusCode": 400,
| "headers": null,
| "body": "{\"errorCode\":null,\"message\":\"bad data!\"}",
| "isBase64Encoded": false
|}""".stripMargin).right.get
assert(outputJson == expectedJson)
}
'HandlerGeneratedError - {
val output = new ByteArrayOutputStream()
val handler = new WithBodyTestHandleReturnError
val apiGatewayReq = makeRequest[InputDataTest](InputDataTest("input data!"))
val apiGatewayReqString =
apiGatewayReq.asJson(APIGatewayProxyRequest.encode[InputDataTest](InputDataTest.encoder)).toString()
handler.handler(streamFromString(apiGatewayReqString), output, makeContext())
val outputJson = parse(output.toString).right.get
val expectedJson = parse("""{
| "statusCode": 500,
| "headers": null,
| "body": "{\"errorCode\":null,\"message\":\"boom!\"}",
| "isBase64Encoded": false
|}""".stripMargin).right.get
assert(outputJson == expectedJson)
}
}
}
}
case class InputDataTest(data: String)
object InputDataTest {
implicit val decoder: Decoder[InputDataTest] = Decoder.forProduct1("data")(InputDataTest.apply)
implicit val encoder: Encoder[InputDataTest] = Encoder.forProduct1("data")(x => x.data)
}
case class BadInputData(anotherThing: String)
object BadInputData {
implicit val decoder: Decoder[BadInputData] = Decoder.forProduct1("anotherThing")(BadInputData.apply)
implicit val encoder: Encoder[BadInputData] = Encoder.forProduct1("anotherThing")(x => x.anotherThing)
}
abstract class WithBodyTestHandle extends ProxyWithBody[InputDataTest, Errors, InputDataTest] {
override def invalidInput(circeError: Error): APIGatewayProxyResponse[Errors] =
APIGatewayProxyResponse(400, None, Some(InputError("bad data!")))
}
class WithBodyTestHandleReturnError extends WithBodyTestHandle {
override def handler(request: APIGatewayProxyRequest[InputDataTest], c: Context): Response[Errors, InputDataTest] =
Left(APIGatewayProxyResponse(500, None, Some(InputError("boom!"))))
}
class WithBodyTestHandleReturnSuccess extends WithBodyTestHandle {
override def handler(request: APIGatewayProxyRequest[InputDataTest], c: Context): Response[Errors, InputDataTest] =
Right(APIGatewayProxyResponse(200, None, Some(InputDataTest("we have liftoff!"))))
}