From 0f4fdf0a303238e13d826b3c335ebe8ecfa52ffb Mon Sep 17 00:00:00 2001 From: Alessio Coser Date: Wed, 20 May 2020 17:15:09 +0200 Subject: [PATCH] updated to latest daikon-core --- build.gradle | 2 +- .../kotlin/daikon/lambda/LambdaRequest.kt | 4 ++ .../kotlin/daikon/lambda/LambdaResponse.kt | 10 +++ src/test/kotlin/daikon/lambda/RequestTest.kt | 65 ++++++++++++++++++- src/test/kotlin/daikon/lambda/ResponseTest.kt | 34 ++++++++++ 5 files changed, 113 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index fe09146..abee40e 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ dependencies { compile 'com.amazonaws:aws-lambda-java-core:1.2.0' compile 'com.google.code.gson:gson:2.8.6' - compile 'com.github.DaikonWeb:daikon-core:1.3.0' + compile 'com.github.DaikonWeb:daikon-core:1.3.4' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2' testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2' diff --git a/src/main/kotlin/daikon/lambda/LambdaRequest.kt b/src/main/kotlin/daikon/lambda/LambdaRequest.kt index 4e4d228..2b48cb6 100644 --- a/src/main/kotlin/daikon/lambda/LambdaRequest.kt +++ b/src/main/kotlin/daikon/lambda/LambdaRequest.kt @@ -43,6 +43,8 @@ data class LambdaRequest( return headers.containsKey(name) } + override fun hasParam(name: String) = try { param(name); true } catch (t: Throwable) { false } + override fun body(): String { if(body == null) { return "" @@ -72,6 +74,8 @@ data class LambdaRequest( return attributes[key]!! as T } + override fun hasAttribute(key: String) = try { attribute(key); true } catch (t: Throwable) { false } + override fun method(): Method { return Method.valueOf(httpMethod) } diff --git a/src/main/kotlin/daikon/lambda/LambdaResponse.kt b/src/main/kotlin/daikon/lambda/LambdaResponse.kt index 9cd6c32..cf32265 100644 --- a/src/main/kotlin/daikon/lambda/LambdaResponse.kt +++ b/src/main/kotlin/daikon/lambda/LambdaResponse.kt @@ -12,10 +12,18 @@ class LambdaResponse : Response { writer.write(text) } + override fun write(byteArray: ByteArray) { + TODO("Not yet implemented") + } + override fun status(code: Int) { statusCode = code } + override fun type(): String { + return headers["Content-Type"] ?: "" + } + override fun type(contentType: String) { header("Content-Type", contentType) } @@ -31,6 +39,8 @@ class LambdaResponse : Response { header("Location", path) } + override fun status() = statusCode + fun asMap() = mapOf( "statusCode" to statusCode, "headers" to headers, diff --git a/src/test/kotlin/daikon/lambda/RequestTest.kt b/src/test/kotlin/daikon/lambda/RequestTest.kt index aad7269..0220316 100644 --- a/src/test/kotlin/daikon/lambda/RequestTest.kt +++ b/src/test/kotlin/daikon/lambda/RequestTest.kt @@ -200,7 +200,7 @@ class RequestTest { runHandler(input, output) { before("/") { req, _ -> req.attribute("foo_key", "foo_value") } - get("/") { req, res -> res.write(req.attribute("foo_key")) } + get("/") { req, res -> res.write(req.attribute("foo_key")) } } verify { output.json(mapOf( @@ -243,4 +243,67 @@ class RequestTest { "body" to "POST" )) } } + + @Test + fun `request has param`() { + val input = apiGatewayEvent(method = "POST", path = "/", queryParams = mapOf("name" to "Bob")) + + runHandler(input, output) { + post("/") { req, res -> res.write("${req.hasParam("name")}") } + } + + verify { output.json(mapOf( + "statusCode" to OK_200, + "headers" to emptyMap(), + "body" to "true" + )) } + } + + @Test + fun `request hasn't param`() { + val input = apiGatewayEvent(method = "POST", path = "/") + + runHandler(input, output) { + post("/") { req, res -> res.write("${req.hasParam("name")}") } + } + + verify { output.json(mapOf( + "statusCode" to OK_200, + "headers" to emptyMap(), + "body" to "false" + )) } + } + + @Test + fun `has attribute`() { + val input = apiGatewayEvent(method = "GET", path = "/") + + runHandler(input, output) { + before("/") { req, _ -> req.attribute("foo_key", "foo_value") } + get("/") { req, res -> + res.write("${req.hasAttribute("foo_key")}") + } + } + + verify { output.json(mapOf( + "statusCode" to OK_200, + "headers" to emptyMap(), + "body" to "true" + )) } + } + + @Test + fun `hasn't attribute`() { + val input = apiGatewayEvent(method = "GET", path = "/") + + runHandler(input, output) { + get("/") { req, res -> res.write("${req.hasAttribute("any")}") } + } + + verify { output.json(mapOf( + "statusCode" to OK_200, + "headers" to emptyMap(), + "body" to "false" + )) } + } } diff --git a/src/test/kotlin/daikon/lambda/ResponseTest.kt b/src/test/kotlin/daikon/lambda/ResponseTest.kt index bfefcdb..d29b28d 100644 --- a/src/test/kotlin/daikon/lambda/ResponseTest.kt +++ b/src/test/kotlin/daikon/lambda/ResponseTest.kt @@ -1,5 +1,7 @@ package daikon.lambda +import daikon.core.HttpStatus +import daikon.core.HttpStatus.CONTINUE_100 import daikon.core.HttpStatus.CREATED_201 import daikon.core.HttpStatus.MOVED_TEMPORARILY_302 import daikon.core.HttpStatus.OK_200 @@ -87,4 +89,36 @@ class ResponseTest { "body" to "" )) } } + + @Test + fun `read response content type`() { + val input = apiGatewayEvent(method = "GET", path = "/") + + runHandler(input, output) { + any("/") { _, res -> res.type("application/json") } + after("/") { _, res -> res.write(res.type()) } + } + + verify { output.json(mapOf( + "statusCode" to OK_200, + "headers" to mapOf("Content-Type" to "application/json"), + "body" to "application/json" + )) } + } + + @Test + fun `read response status`() { + val input = apiGatewayEvent(method = "GET", path = "/") + + runHandler(input, output) { + any("/") { _, res -> res.status(CONTINUE_100) } + after("/") { _, res -> res.write("${res.status()}") } + } + + verify { output.json(mapOf( + "statusCode" to CONTINUE_100, + "headers" to emptyMap(), + "body" to "100" + )) } + } } \ No newline at end of file