Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[grid] flatten combined routes to improve routing #13856

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions java/src/org/openqa/selenium/remote/http/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -292,7 +292,7 @@ private HttpRequest transform(HttpRequest request) {
// Don't forget to register our prefix
Object rawPrefixes = request.getAttribute(ROUTE_PREFIX_KEY);
if (!(rawPrefixes instanceof List)) {
rawPrefixes = new LinkedList<>();
rawPrefixes = Collections.emptyList();
}
List<String> prefixes =
Stream.concat(((List<?>) rawPrefixes).stream(), Stream.of(prefix))
Expand Down Expand Up @@ -321,7 +321,21 @@ private static class CombinedRoute extends Route {
private CombinedRoute(Stream<Routable> routes) {
// We want later routes to have a greater chance of being called so that we can override
// routes as necessary.
List<Routable> routables = routes.collect(Collectors.toList());
List<Routable> routables =
routes
.flatMap(
route -> {
// flatten a nested CombinedRoute
if (route instanceof CombinedRoute) {
List<Routable> nestedRoutes =
new ArrayList<>(((CombinedRoute) route).allRoutes);
// reverse to have the identical behaviour like not flattened
Collections.reverse(nestedRoutes);
return nestedRoutes.stream();
}
return Stream.of(route);
})
.collect(Collectors.toList());
Collections.reverse(routables);
allRoutes = List.copyOf(routables);
Require.stateCondition(!allRoutes.isEmpty(), "At least one route must be specified.");
Expand Down
7 changes: 5 additions & 2 deletions java/test/org/openqa/selenium/remote/http/RouteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ void laterRoutesOverrideEarlierRoutesToFacilitateOverridingRoutes() {
HttpHandler handler =
Route.combine(
Route.get("/hello").to(() -> req -> new HttpResponse().setContent(utf8String("world"))),
Route.get("/hello")
.to(() -> req -> new HttpResponse().setContent(utf8String("buddy"))));
Route.combine(
Route.get("/hello")
.to(() -> req -> new HttpResponse().setContent(utf8String("world"))),
Route.get("/hello")
.to(() -> req -> new HttpResponse().setContent(utf8String("buddy")))));

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