From dbd9d4b082905e9bb9d3e6dd7ae18be0f37c5154 Mon Sep 17 00:00:00 2001 From: Ivan Sanchez Date: Sun, 24 Sep 2017 13:38:48 +0100 Subject: [PATCH] Experiment with minimal HttpHandler creation in java --- .../src/main/kotlin/org/http4k/core/Status.kt | 1 + .../src/main/kotlin/org/http4k/core/Uri.kt | 1 + .../java/Http4kJavaCompatibilityTest.java | 44 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 src/test/java/org/http4k/java/Http4kJavaCompatibilityTest.java diff --git a/http4k-core/src/main/kotlin/org/http4k/core/Status.kt b/http4k-core/src/main/kotlin/org/http4k/core/Status.kt index cf8b4e0872..c93e5e0f97 100644 --- a/http4k-core/src/main/kotlin/org/http4k/core/Status.kt +++ b/http4k-core/src/main/kotlin/org/http4k/core/Status.kt @@ -7,6 +7,7 @@ data class Status(val code: Int, val description: String) { val SWITCHING_PROTOCOLS = status(101, "Switching Protocols") private val SUCCESSFUL = 200..299 + @JvmField val OK = status(200, "OK") val CREATED = status(201, "Created") val ACCEPTED = status(202, "Accepted") diff --git a/http4k-core/src/main/kotlin/org/http4k/core/Uri.kt b/http4k-core/src/main/kotlin/org/http4k/core/Uri.kt index c4fed08e23..fd1419c8d8 100644 --- a/http4k-core/src/main/kotlin/org/http4k/core/Uri.kt +++ b/http4k-core/src/main/kotlin/org/http4k/core/Uri.kt @@ -7,6 +7,7 @@ data class Uri(val scheme: String, val userInfo: String, val host: String, val p companion object { private val AUTHORITY = Regex("(?:([^@]+)@)?([^:]+)(?::([\\d]+))?") private val RFC3986 = Regex("^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\\?([^#]*))?(?:#(.*))?") + @JvmStatic fun of(value: String): Uri { val result = RFC3986.matchEntire(value) ?: throw RuntimeException("Invalid Uri: $value") val (scheme, authority, path, query, fragment) = result.destructured diff --git a/src/test/java/org/http4k/java/Http4kJavaCompatibilityTest.java b/src/test/java/org/http4k/java/Http4kJavaCompatibilityTest.java new file mode 100644 index 0000000000..339c4a91b0 --- /dev/null +++ b/src/test/java/org/http4k/java/Http4kJavaCompatibilityTest.java @@ -0,0 +1,44 @@ +package org.http4k.java; + +import com.google.common.base.Function; +import org.http4k.core.*; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.http4k.core.Method.GET; +import static org.http4k.core.Status.OK; +import static org.http4k.java.RequestFactory.request; +import static org.http4k.java.ResponseFactory.response; +import static org.junit.Assert.assertThat; + +public class Http4kJavaCompatibilityTest { + + @Test + public void handler_function() { + HttpHandler handler = request -> response(OK).body("test"); + + Response response = handler.handle(request(GET, Uri.of("/test"))); + + assertThat(response.getStatus(), equalTo(OK)); + assertThat(response.bodyString(), equalTo("test")); + } + +} + +interface HttpHandler extends Function { + default Response handle(Request request) { + return apply(request); + } +} + +class RequestFactory { + static Request request(Method method, Uri uri) { + return Request.Companion.invoke(method, uri); + } +} + +class ResponseFactory { + static Response response(Status status) { + return Response.Companion.invoke(status); + } +} \ No newline at end of file