Skip to content

Commit

Permalink
[java] Allow OPTIONS to be sent to the graphql endpoint
Browse files Browse the repository at this point in the history
The ApolloClient we are using sends an OTIONS request before the
POST and we should allow it.
  • Loading branch information
AutomatedTester committed Jul 13, 2020
1 parent 444af06 commit 4d4eed0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public enum HttpMethod {
DELETE,
GET,
POST,
OPTIONS,
}
9 changes: 9 additions & 0 deletions java/client/src/org/openqa/selenium/remote/http/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.openqa.selenium.remote.http.Contents.utf8String;
import static org.openqa.selenium.remote.http.HttpMethod.DELETE;
import static org.openqa.selenium.remote.http.HttpMethod.GET;
import static org.openqa.selenium.remote.http.HttpMethod.OPTIONS;
import static org.openqa.selenium.remote.http.HttpMethod.POST;
import static org.openqa.selenium.remote.http.UrlPath.ROUTE_PREFIX_KEY;

Expand Down Expand Up @@ -111,6 +112,14 @@ public static TemplatizedRouteConfig post(String template) {
urlTemplate);
}

public static TemplatizedRouteConfig options(String template) {
UrlTemplate urlTemplate = new UrlTemplate(Require.nonNull("URL Template", template));

return new TemplatizedRouteConfig(
new MatchesHttpMethod(OPTIONS).and(new MatchesTemplate(urlTemplate)),
urlTemplate);
}

public static NestedRouteConfig prefix(String prefix) {
Require.nonNull("Prefix", prefix);
Require.stateCondition(!prefix.isEmpty(), "Prefix to use must not be of 0 length");
Expand Down
1 change: 1 addition & 0 deletions java/server/src/org/openqa/selenium/grid/commands/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ protected void execute(Config config) {
HttpHandler httpHandler = combine(
router.with(networkOptions.getSpecComplianceChecks()),
Route.prefix("/wd/hub").to(combine(router.with(networkOptions.getSpecComplianceChecks()))),
Route.options("/graphql").to(() -> graphqlHandler),
Route.post("/graphql").to(() -> graphqlHandler),
Route.get("/readyz").to(() -> readinessCheck));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ protected void execute(Config config) {
HttpHandler httpHandler = combine(
router,
Route.prefix("/wd/hub").to(combine(router)),
Route.options("/graphql").to(() -> graphqlHandler),
Route.post("/graphql").to(() -> graphqlHandler),
Route.get("/readyz").to(() -> readinessCheck));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.http.Contents;
import org.openqa.selenium.remote.http.HttpHandler;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.http.HttpRequest;
import org.openqa.selenium.remote.http.HttpResponse;

Expand All @@ -45,6 +46,7 @@
import java.util.concurrent.ExecutionException;

import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.openqa.selenium.json.Json.JSON_UTF_8;
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.remote.http.Contents.utf8String;
Expand Down Expand Up @@ -86,6 +88,9 @@ public GraphqlHandler(Distributor distributor, URI publicUri) {

@Override
public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
if (req.getMethod() == HttpMethod.OPTIONS) {
return new HttpResponse().setStatus(HTTP_OK);
}
Map<String, Object> inputs = JSON.toType(Contents.string(req), MAP_TYPE);

if (!(inputs.get("query") instanceof String)) {
Expand Down

0 comments on commit 4d4eed0

Please sign in to comment.