Skip to content

Commit

Permalink
HttpHandler is no longer a Function
Browse files Browse the repository at this point in the history
This is to allow us to declare something as being both a
Function<A,B> and an HttpHandler. Type erasure in Java
means that you can't implement Function twice on the same
class. Which is daft, but it is what it is.
  • Loading branch information
shs96c committed Jul 3, 2019
1 parent 5263a2d commit 9754373
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ default Filter andThen(Filter next) {
}

default HttpHandler andFinally(HttpHandler end) {
return request -> Filter.this.apply(end).apply(request);
return request -> Filter.this.apply(end).execute(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package org.openqa.selenium.remote.http;

import java.util.function.Function;

@FunctionalInterface
public interface HttpHandler extends Function<HttpRequest, HttpResponse> {
public interface HttpHandler {

HttpResponse execute(HttpRequest req);

default HttpHandler with(Filter filter) {
return filter.andFinally(this);
Expand Down
16 changes: 8 additions & 8 deletions java/client/src/org/openqa/selenium/remote/http/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public HttpHandler fallbackTo(Supplier<HttpHandler> handler) {
Objects.requireNonNull(handler, "Handler to use must be set.");
return req -> {
if (test(req)) {
return Route.this.apply(req);
return Route.this.execute(req);
}
return Objects.requireNonNull(handler.get(), "Handler to use must be set.").apply(req);
return Objects.requireNonNull(handler.get(), "Handler to use must be set.").execute(req);
};
}

Expand Down Expand Up @@ -136,7 +136,7 @@ public boolean test(HttpRequest request) {
}

@Override
public HttpResponse apply(HttpRequest request) {
public HttpResponse execute(HttpRequest request) {
UrlTemplate.Match match = template.match(request.getUri());
HttpHandler handler = handlerFunction.apply(
match == null ? ImmutableMap.of() : match.getParameters());
Expand All @@ -147,7 +147,7 @@ public HttpResponse apply(HttpRequest request) {
.setContent(utf8String("Unable to find handler for " + request));
}

return handler.apply(request);
return handler.execute(request);
}
}

Expand Down Expand Up @@ -209,8 +209,8 @@ public boolean test(HttpRequest request) {
}

@Override
public HttpResponse apply(HttpRequest request) {
return route.apply(transform(request));
public HttpResponse execute(HttpRequest request) {
return route.execute(transform(request));
}

private HttpRequest transform(HttpRequest request) {
Expand Down Expand Up @@ -251,15 +251,15 @@ public boolean test(HttpRequest request) {
}

@Override
public HttpResponse apply(HttpRequest request) {
public HttpResponse execute(HttpRequest request) {
return allRoutes.stream()
.filter(route -> route.test(request))
.findFirst()
.map(route -> (HttpHandler) route)
.orElse(req -> new HttpResponse()
.setStatus(HTTP_NOT_FOUND)
.setContent(utf8String("No handler found for " + req)))
.apply(request);
.execute(request);
}
}
}
10 changes: 5 additions & 5 deletions java/client/test/org/openqa/selenium/remote/http/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ public void aFilterShouldWrapAnHttpHandler() {

HttpHandler handler = ((Filter) next -> req -> {
filterCalled.set(true);
return next.apply(req);
return next.execute(req);
}).andFinally(req -> {
handlerCalled.set(true);
return new HttpResponse();
});

HttpResponse res = handler.apply(new HttpRequest(GET, "/cheese"));
HttpResponse res = handler.execute(new HttpRequest(GET, "/cheese"));

assertThat(res).isNotNull();
assertThat(handlerCalled.get()).isTrue();
Expand All @@ -51,16 +51,16 @@ public void aFilterShouldWrapAnHttpHandler() {
@Test
public void shouldBePossibleToChainFiltersOneAfterAnother() {
HttpHandler handler = ((Filter) next -> req -> {
HttpResponse res = next.apply(req);
HttpResponse res = next.execute(req);
res.addHeader("cheese", "cheddar");
return res;
}).andThen(next -> req -> {
HttpResponse res = next.apply(req);
HttpResponse res = next.execute(req);
res.setHeader("cheese", "brie");
return res;
}).andFinally(req -> new HttpResponse());

HttpResponse res = handler.apply(new HttpRequest(GET, "/cheese"));
HttpResponse res = handler.execute(new HttpRequest(GET, "/cheese"));

assertThat(res).isNotNull();
// Because the headers are applied to the response _after_ the request has been processed,
Expand Down
20 changes: 10 additions & 10 deletions java/client/test/org/openqa/selenium/remote/http/RouteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void shouldRouteSimplePaths() {
HttpRequest request = new HttpRequest(GET, "/hello");
Assertions.assertThat(route.test(request)).isTrue();

HttpResponse res = route.apply(request);
HttpResponse res = route.execute(request);
assertThat(string(res)).isEqualTo("Hello, World!");
}

Expand All @@ -61,7 +61,7 @@ public void shouldAllowRoutesToBeUrlTemplates() {
HttpRequest request = new HttpRequest(POST, "/greeting/cheese");
Assertions.assertThat(route.test(request)).isTrue();

HttpResponse res = route.apply(request);
HttpResponse res = route.execute(request);
assertThat(string(res)).isEqualTo("Hello, cheese!");
}

Expand All @@ -72,7 +72,7 @@ public void shouldAllowRoutesToBePrefixed() {

HttpRequest request = new HttpRequest(GET, "/cheese/type");
Assertions.assertThat(route.test(request)).isTrue();
HttpResponse res = route.apply(request);
HttpResponse res = route.execute(request);
assertThat(string(res)).isEqualTo("brie");
}

Expand All @@ -85,7 +85,7 @@ public void shouldAllowRoutesToBeNested() {

HttpRequest good = new HttpRequest(GET, "/cheese/favourite/is/stilton");
Assertions.assertThat(route.test(good)).isTrue();
HttpResponse response = route.apply(good);
HttpResponse response = route.execute(good);
assertThat(string(response)).isEqualTo("stilton");

HttpRequest bad = new HttpRequest(GET, "/cheese/favourite/not-here");
Expand All @@ -100,7 +100,7 @@ public void nestedRoutesShouldStripPrefixFromRequest() {

HttpRequest request = new HttpRequest(GET, "/cheese/type");
Assertions.assertThat(route.test(request)).isTrue();
HttpResponse res = route.apply(request);
HttpResponse res = route.execute(request);
assertThat(string(res)).isEqualTo("/type");
}

Expand All @@ -113,12 +113,12 @@ public void itShouldBePossibleToCombineRoutes() {

HttpRequest greet = new HttpRequest(GET, "/hello");
Assertions.assertThat(route.test(greet)).isTrue();
HttpResponse response = route.apply(greet);
HttpResponse response = route.execute(greet);
assertThat(string(response)).isEqualTo("world");

HttpRequest cheese = new HttpRequest(POST, "/cheese");
Assertions.assertThat(route.test(cheese)).isTrue();
response = route.apply(cheese);
response = route.execute(cheese);
assertThat(string(response)).isEqualTo("gouda");
}

Expand All @@ -128,7 +128,7 @@ public void laterRoutesOverrideEarlierRoutesToFacilitateOverridingRoutes() {
Route.get("/hello").to(() -> req -> new HttpResponse().setContent(utf8String("world"))),
Route.get("/hello").to(() -> req -> new HttpResponse().setContent(utf8String("buddy"))));

HttpResponse response = handler.apply(new HttpRequest(GET, "/hello"));
HttpResponse response = handler.execute(new HttpRequest(GET, "/hello"));
assertThat(string(response)).isEqualTo("buddy");
}

Expand All @@ -137,10 +137,10 @@ public void shouldUseFallbackIfAnyDeclared() {
HttpHandler handler = Route.delete("/negativity").to(() -> req -> new HttpResponse())
.fallbackTo(() -> req -> new HttpResponse().setStatus(HTTP_NOT_FOUND));

HttpResponse res = handler.apply(new HttpRequest(DELETE, "/negativity"));
HttpResponse res = handler.execute(new HttpRequest(DELETE, "/negativity"));
assertThat(res.getStatus()).isEqualTo(HTTP_OK);

res = handler.apply(new HttpRequest(GET, "/joy"));
res = handler.execute(new HttpRequest(GET, "/joy"));
assertThat(res.getStatus()).isEqualTo(HTTP_NOT_FOUND);
}
}

0 comments on commit 9754373

Please sign in to comment.