endpoint root path will redirect to /graphql (#283)

When users hit GraphQL url http://url:port/ they will be redirected to http://url:port/graphql.

Signed-off-by: Anthony Buckle <anthonybuckle@gmail.com>
pull/287/head
anthonybuckle 5 years ago committed by Danno Ferrin
parent 2ce5ac9bb7
commit c9af435b79
  1. 35
      ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/graphql/GraphQLHttpService.java
  2. 5
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/GraphQLHttpServiceCorsTest.java
  3. 22
      ethereum/api/src/test/java/org/hyperledger/besu/ethereum/api/graphql/GraphQLHttpServiceTest.java

@ -68,6 +68,7 @@ public class GraphQLHttpService {
private static final InetSocketAddress EMPTY_SOCKET_ADDRESS = new InetSocketAddress("0.0.0.0", 0);
private static final String APPLICATION_JSON = "application/json";
private static final String GRAPH_QL_ROUTE = "/graphql";
private static final MediaType MEDIA_TYPE_JUST_JSON = MediaType.JSON_UTF_8.withoutParameters();
private static final String EMPTY_RESPONSE = "";
@ -140,9 +141,9 @@ public class GraphQLHttpService {
BodyHandler.create()
.setUploadsDirectory(dataDir.resolve("uploads").toString())
.setDeleteUploadedFilesOnEnd(true));
router.route("/").method(GET).handler(this::handleEmptyRequest);
router.route("/").method(GET).method(POST).handler(this::handleEmptyRequestAndRedirect);
router
.route("/graphql")
.route(GRAPH_QL_ROUTE)
.method(GET)
.method(POST)
.produces(APPLICATION_JSON)
@ -243,9 +244,12 @@ public class GraphQLHttpService {
return NetworkUtility.urlForSocketAddress("http", socketAddress());
}
// Facilitate remote health-checks in AWS, inter alia.
private void handleEmptyRequest(final RoutingContext routingContext) {
routingContext.response().setStatusCode(201).end();
// Empty Get/Post requests to / will be redirected to /graphql using 308 Permanent Redirect
private void handleEmptyRequestAndRedirect(final RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();
response.setStatusCode(HttpResponseStatus.PERMANENT_REDIRECT.code());
response.putHeader("Location", "/graphql");
response.end();
}
private void handleGraphQLRequest(final RoutingContext routingContext) {
@ -257,7 +261,12 @@ public class GraphQLHttpService {
switch (request.method()) {
case GET:
query = request.getParam("query");
final String queryString = request.getParam("query");
if (queryString == null) {
query = "";
} else {
query = queryString;
}
operationName = request.getParam("operationName");
final String variableString = request.getParam("variables");
if (variableString != null) {
@ -272,7 +281,12 @@ public class GraphQLHttpService {
final String requestBody = routingContext.getBodyAsString().trim();
final GraphQLJsonRequest jsonRequest =
Json.decodeValue(requestBody, GraphQLJsonRequest.class);
query = jsonRequest.getQuery();
final String jsonQuery = jsonRequest.getQuery();
if (jsonQuery == null) {
query = "";
} else {
query = jsonQuery;
}
operationName = jsonRequest.getOperationName();
Map<String, Object> jsonVariables = jsonRequest.getVariables();
if (jsonVariables != null) {
@ -282,7 +296,12 @@ public class GraphQLHttpService {
}
} else {
// treat all else as application/graphql
query = routingContext.getBodyAsString().trim();
final String requestQuery = routingContext.getBodyAsString().trim();
if (requestQuery == null) {
query = "";
} else {
query = requestQuery;
}
operationName = null;
variables = Collections.emptyMap();
}

@ -140,7 +140,10 @@ public class GraphQLHttpServiceCorsTest {
public void requestWithNoOriginShouldSucceedWhenCorsIsSet() throws Exception {
graphQLHttpService = createGraphQLHttpServiceWithAllowedDomains("http://foo.io");
final Request request = new Request.Builder().url(graphQLHttpService.url()).build();
final Request request =
new Request.Builder()
.url(graphQLHttpService.url() + "/graphql?query={protocolVersion}")
.build();
try (final Response response = client.newCall(request).execute()) {
Assertions.assertThat(response.isSuccessful()).isTrue();

@ -36,8 +36,10 @@ import java.util.Optional;
import java.util.Set;
import graphql.GraphQL;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -153,10 +155,26 @@ public class GraphQLHttpServiceTest {
}
@Test
public void handleEmptyRequest() throws Exception {
public void handleEmptyRequestAndRedirect() throws Exception {
try (final Response resp =
client.newCall(new Request.Builder().get().url(service.url()).build()).execute()) {
Assertions.assertThat(resp.code()).isEqualTo(201);
Assertions.assertThat(resp.code()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
}
final RequestBody body = RequestBody.create(null, "");
try (final Response resp =
client.newCall(new Request.Builder().post(body).url(service.url()).build()).execute()) {
Assertions.assertThat(resp.code()).isEqualTo(HttpResponseStatus.PERMANENT_REDIRECT.code());
String location = resp.header("Location");
Assertions.assertThat(location).isNotEmpty().isNotNull();
HttpUrl redirectUrl = resp.request().url().resolve(location);
Assertions.assertThat(redirectUrl).isNotNull();
Request.Builder redirectBuilder = resp.request().newBuilder();
redirectBuilder.post(resp.request().body());
resp.body().close();
try (final Response redirectResp =
client.newCall(redirectBuilder.url(redirectUrl).build()).execute()) {
Assertions.assertThat(redirectResp.code()).isEqualTo(HttpResponseStatus.BAD_REQUEST.code());
}
}
}

Loading…
Cancel
Save