Skip to content

Commit 16720d7

Browse files
raoulkmduesterhoeft
authored andcommitted
Header data class (#27)
1 parent 1a60bcb commit 16720d7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

router/src/main/kotlin/io/moia/router/APIGatewayProxyEventExtensions.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import com.google.common.net.MediaType
66
import java.net.URI
77
import java.util.Base64
88

9+
/** Data class that represents an HTTP header */
10+
data class Header(val name: String, val value: String)
11+
912
fun APIGatewayProxyRequestEvent.acceptHeader() = getHeaderCaseInsensitive("accept")
1013
fun APIGatewayProxyRequestEvent.acceptedMediaTypes() = acceptHeader()
1114
?.split(",")
@@ -54,9 +57,15 @@ fun APIGatewayProxyRequestEvent.location(path: String): URI {
5457
fun APIGatewayProxyRequestEvent.withHeader(name: String, value: String) =
5558
this.also { if (headers == null) headers = mutableMapOf() }.also { headers[name] = value }
5659

60+
fun APIGatewayProxyRequestEvent.withHeader(header: Header) =
61+
this.withHeader(header.name, header.value)
62+
5763
fun APIGatewayProxyResponseEvent.withHeader(name: String, value: String) =
5864
this.also { if (headers == null) headers = mutableMapOf() }.also { headers[name] = value }
5965

66+
fun APIGatewayProxyResponseEvent.withHeader(header: Header) =
67+
this.withHeader(header.name, header.value)
68+
6069
fun APIGatewayProxyResponseEvent.withLocationHeader(request: APIGatewayProxyRequestEvent, path: String) =
6170
this.also { if (headers == null) headers = mutableMapOf() }.also { headers["location"] = request.location(path).toString() }
6271

router/src/test/kotlin/io/moia/router/APIGatewayProxyEventExtensionsTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.moia.router
22

3+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
34
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
45
import org.assertj.core.api.BDDAssertions.then
56
import org.junit.jupiter.api.Test
@@ -56,4 +57,20 @@ class APIGatewayProxyEventExtensionsTest {
5657

5758
then(location.toString()).isEqualTo("http://example.com/some/path")
5859
}
60+
61+
@Test
62+
fun `header class should work as expected with APIGatewayProxyRequestEvent`() {
63+
64+
val request = APIGatewayProxyRequestEvent().withHeader(Header("foo", "bar"))
65+
66+
then(request.headers["foo"]).isEqualTo("bar")
67+
}
68+
69+
@Test
70+
fun `header class should work as expected with APIGatewayProxyResponseEvent`() {
71+
72+
val request = APIGatewayProxyResponseEvent().withHeader(Header("foo", "bar"))
73+
74+
then(request.headers["foo"]).isEqualTo("bar")
75+
}
5976
}

0 commit comments

Comments
 (0)