-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProxyNoBodyTest.scala
More file actions
75 lines (62 loc) · 3.02 KB
/
ProxyNoBodyTest.scala
File metadata and controls
75 lines (62 loc) · 3.02 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
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 ProxyNoBodyTest extends TestSuite with ProxyEncoder with LambdaTestUtils {
val tests = Tests {
'ProxyNoBody - {
'ValidInput - {
val output = new ByteArrayOutputStream()
val handler = new NoBodyTestHandleReturnSuccess
val apiGatewayReq = makeNoBodyReq
val apiGatewayReqString = apiGatewayReq.asJson.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)
}
'HandlerGeneratedError - {
val output = new ByteArrayOutputStream()
val handler = new NoBodyTestHandleReturnError
val apiGatewayReq = makeNoBodyReq
val apiGatewayReqString = apiGatewayReq.asJson.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 OutputDataTest(data: String)
object OutputDataTest {
implicit val decoder: Decoder[OutputDataTest] = Decoder.forProduct1("data")(OutputDataTest.apply)
implicit val encoder: Encoder[OutputDataTest] = Encoder.forProduct1("data")(x => x.data)
}
abstract class NoBodyTestHandle extends ProxyNoBody[Errors, OutputDataTest] {
override def invalidInput(circeError: Error): APIGatewayProxyResponse[Errors] =
APIGatewayProxyResponse(400, None, Some(InputError("bad data!")))
}
class NoBodyTestHandleReturnError extends NoBodyTestHandle {
override def handler(request: APIGatewayProxyRequestNoBody, c: Context): Response[Errors, OutputDataTest] =
Left(APIGatewayProxyResponse(500, None, Some(InputError("boom!"))))
}
class NoBodyTestHandleReturnSuccess extends NoBodyTestHandle {
override def handler(request: APIGatewayProxyRequestNoBody, c: Context): Response[Errors, OutputDataTest] =
Right(APIGatewayProxyResponse(200, None, Some(OutputDataTest("we have liftoff!"))))
}