|
| 1 | +package io.github.howardjohn.lambda |
| 2 | + |
| 3 | +import java.io.{ByteArrayInputStream, ByteArrayOutputStream} |
| 4 | + |
| 5 | +import io.circe.generic.auto._ |
| 6 | +import io.circe.parser.decode |
| 7 | +import io.circe.syntax._ |
| 8 | +import io.github.howardjohn.lambda.ProxyEncoding.{ProxyRequest, ProxyResponse} |
| 9 | +import org.scalatest.Assertions._ |
| 10 | +import org.scalatest.{FeatureSpec, GivenWhenThen} |
| 11 | + |
| 12 | +/** |
| 13 | + * Defines the behavior a LambdaHandler must implement. They should be tested against a set of the following routes: |
| 14 | + * GET /hello => "Hello World!" |
| 15 | + * GET /hello?times=3 => "Hello World! Hello World! Hello World!" |
| 16 | + * GET /long => takes a second to complete |
| 17 | + * POST /post with body => responds with the body |
| 18 | + * GET /exception => throws a RouteException |
| 19 | + * GET /error => responds with a 500 error code |
| 20 | + * GET /header with header InputHeader => responds with a new header, |
| 21 | + * OutputHeader: outputHeaderValue and the value of InputHeader as the body |
| 22 | + */ |
| 23 | +trait LambdaHandlerBehavior { this: FeatureSpec with GivenWhenThen => |
| 24 | + import LambdaHandlerBehavior._ |
| 25 | + |
| 26 | + def behavior(handler: LambdaHandler) { |
| 27 | + scenario("A simple get request is made") { |
| 28 | + Given("a GET request to /hello") |
| 29 | + val response = runRequest("/hello")(handler) |
| 30 | + |
| 31 | + Then("the status code should be 200") |
| 32 | + assert(response.statusCode === 200) |
| 33 | + |
| 34 | + And("the body should be Hello World!") |
| 35 | + assert(response.body === "Hello World!") |
| 36 | + } |
| 37 | + |
| 38 | + scenario("A bad http method is used on a route") { |
| 39 | + Given("a POST request to /hello") |
| 40 | + val response = runRequest("/hello", httpMethod = "POST")(handler) |
| 41 | + |
| 42 | + Then("the status code should be 404") |
| 43 | + assert(response.statusCode === 404) |
| 44 | + } |
| 45 | + |
| 46 | + scenario("Including query parameters in a call") { |
| 47 | + Given("a GET request to /hello?times=3") |
| 48 | + val response = runRequest("/hello", query = Some(Map("times" -> "3")))(handler) |
| 49 | + |
| 50 | + Then("the status code should be 200") |
| 51 | + assert(response.statusCode === 200) |
| 52 | + |
| 53 | + And("the body should be Hello World! Hello World! Hello World!") |
| 54 | + assert(response.body === "Hello World! Hello World! Hello World!") |
| 55 | + } |
| 56 | + |
| 57 | + scenario("A request takes a long time to respond") { |
| 58 | + Given("a GET request to /long") |
| 59 | + val response = runRequest("/long")(handler) |
| 60 | + |
| 61 | + Then("the status code should be 200") |
| 62 | + assert(response.statusCode === 200) |
| 63 | + } |
| 64 | + |
| 65 | + scenario("A POST call is made with a body") { |
| 66 | + Given("a POST request to /post") |
| 67 | + val response = runRequest("/post", body = Some("body"), httpMethod = "POST")(handler) |
| 68 | + |
| 69 | + Then("the status code should be 200") |
| 70 | + assert(response.statusCode === 200) |
| 71 | + |
| 72 | + And("the body should be body") |
| 73 | + assert(response.body === "body") |
| 74 | + } |
| 75 | + |
| 76 | + scenario("A request causes an exception") { |
| 77 | + Given("a GET request to /exception") |
| 78 | + Then("an exception should be thrown") |
| 79 | + intercept[RouteException](runRequest("/exception")(handler)) |
| 80 | + } |
| 81 | + |
| 82 | + scenario("A request returns an error response") { |
| 83 | + Given("a GET request to /error") |
| 84 | + val response = runRequest("/error")(handler) |
| 85 | + |
| 86 | + Then("the status code should be 500") |
| 87 | + assert(response.statusCode === 500) |
| 88 | + } |
| 89 | + |
| 90 | + scenario("Request and responding with headers") { |
| 91 | + Given("a GET request to /header") |
| 92 | + val response = runRequest("/header", headers = Some(Map(inputHeader -> inputHeaderValue)))(handler) |
| 93 | + |
| 94 | + Then("the status code should be 200") |
| 95 | + assert(response.statusCode === 200) |
| 96 | + |
| 97 | + And("the body should be inputHeaderValue") |
| 98 | + assert(response.body === inputHeaderValue) |
| 99 | + |
| 100 | + And("the headers should include outputHeader") |
| 101 | + assert(response.headers.get(outputHeader) === Some(outputHeaderValue)) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +object LambdaHandlerBehavior { |
| 107 | + case class RouteException() extends RuntimeException("There was an exception in the route") |
| 108 | + val outputHeader = "OutputHeader" |
| 109 | + val outputHeaderValue = "outputHeaderValue" |
| 110 | + val inputHeader = "InputHeader" |
| 111 | + val inputHeaderValue = "inputHeaderValue" |
| 112 | + |
| 113 | + private def runRequest( |
| 114 | + path: String, |
| 115 | + httpMethod: String = "GET", |
| 116 | + headers: Option[Map[String, String]] = None, |
| 117 | + body: Option[String] = None, |
| 118 | + query: Option[Map[String, String]] = None |
| 119 | + )(handler: LambdaHandler): ProxyResponse = { |
| 120 | + val request = ProxyRequest( |
| 121 | + httpMethod, |
| 122 | + path, |
| 123 | + headers, |
| 124 | + body, |
| 125 | + query |
| 126 | + ).asJson.noSpaces |
| 127 | + val input = new ByteArrayInputStream(request.getBytes("UTF-8")) |
| 128 | + val output = new ByteArrayOutputStream() |
| 129 | + handler.handle(input, output) |
| 130 | + decode[ProxyResponse](new String(output.toByteArray)) match { |
| 131 | + case Left(err) => fail(err) |
| 132 | + case Right(resp) => resp |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments