Skip to content

Commit

Permalink
Experiment with minimal HttpHandler creation in java
Browse files Browse the repository at this point in the history
  • Loading branch information
s4nchez committed Sep 24, 2017
1 parent 613f369 commit dbd9d4b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions http4k-core/src/main/kotlin/org/http4k/core/Status.kt
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions http4k-core/src/main/kotlin/org/http4k/core/Uri.kt
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions 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<Request, Response> {
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);
}
}

0 comments on commit dbd9d4b

Please sign in to comment.