Skip to content

Commit

Permalink
updated to latest daikon-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed May 20, 2020
1 parent b33bed4 commit 0f4fdf0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/daikon/lambda/LambdaRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down Expand Up @@ -72,6 +74,8 @@ data class LambdaRequest(
return attributes[key]!! as T
}

override fun hasAttribute(key: String) = try { attribute<Any>(key); true } catch (t: Throwable) { false }

override fun method(): Method {
return Method.valueOf(httpMethod)
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/daikon/lambda/LambdaResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -31,6 +39,8 @@ class LambdaResponse : Response {
header("Location", path)
}

override fun status() = statusCode

fun asMap() = mapOf(
"statusCode" to statusCode,
"headers" to headers,
Expand Down
65 changes: 64 additions & 1 deletion src/test/kotlin/daikon/lambda/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>("foo_key")) }
}

verify { output.json(mapOf(
Expand Down Expand Up @@ -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<String, String>(),
"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<String, String>(),
"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<String, String>(),
"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<String, String>(),
"body" to "false"
)) }
}
}
34 changes: 34 additions & 0 deletions src/test/kotlin/daikon/lambda/ResponseTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<String, String>(),
"body" to "100"
)) }
}
}

0 comments on commit 0f4fdf0

Please sign in to comment.