Skip to content

Commit

Permalink
[#116] - Manually set content-type header (#117)
Browse files Browse the repository at this point in the history
Disable automatic content type response handler and manually set content type from each handler

Fixes issue #116

Signed-off-by: Usman Saleem <[email protected]>
  • Loading branch information
usmansaleem authored Jul 7, 2020
1 parent 466c047 commit 65c7b95
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public void signDataWithKeyLoadedFromUnencryptedFile() {
.contentType(ContentType.JSON)
.pathParam("publicKey", keyPair.getPublicKey().toString())
.body(new JsonObject().put("signingRoot", SIGNING_ROOT.toHexString()).toString())
.when()
.post(SIGN_ENDPOINT)
.then()
.assertThat()
.statusCode(200)
.contentType(ContentType.TEXT)
.body(equalToIgnoringCase(expectedSignature.toString()));
}

Expand All @@ -97,11 +96,10 @@ public void signDataWithKeyLoadedFromKeyStoreFile(KdfFunction kdfFunction) {
.contentType(ContentType.JSON)
.pathParam("publicKey", keyPair.getPublicKey().toString())
.body(new JsonObject().put("signingRoot", SIGNING_ROOT.toHexString()).toString())
.when()
.post(SIGN_ENDPOINT)
.then()
.assertThat()
.statusCode(200)
.contentType(ContentType.TEXT)
.body(equalToIgnoringCase(expectedSignature.toString()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.nio.file.Path;

import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import org.apache.tuweni.bytes.Bytes;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -88,15 +89,35 @@ public void allLoadedKeysAreReturnedPublicKeyResponse() {
"", containsInAnyOrder(key1.getPublicKey().toString(), key2.getPublicKey().toString()));
}

@Test
public void allLoadedKeysAreReturnedPublicKeyResponseWithEmptyAccept() {
final BLSKeyPair key1 = createKey(PRIVATE_KEY_1);
final BLSKeyPair key2 = createKey(PRIVATE_KEY_2);

final SignerConfigurationBuilder builder = new SignerConfigurationBuilder();
builder.withKeyStoreDirectory(testDirectory);
startSigner(builder.build());

// without openapi filter
given()
.baseUri(signer.getUrl())
.accept("")
.get(SIGNER_PUBLIC_KEYS_PATH)
.then()
.statusCode(200)
.contentType(ContentType.JSON)
.body(
"", containsInAnyOrder(key1.getPublicKey().toString(), key2.getPublicKey().toString()));
}

private ValidatableResponse whenGetSignerPublicKeysPathThenAssertThat() {
return given()
.baseUri(signer.getUrl())
.filter(getOpenApiValidationFilter())
.when()
.get(SIGNER_PUBLIC_KEYS_PATH)
.then()
.assertThat()
.statusCode(200);
.statusCode(200)
.contentType(ContentType.JSON);
}

private BLSKeyPair createKey(final String privateKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import tech.pegasys.eth2signer.dsl.signer.SignerConfigurationBuilder;

import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;

public class UpcheckAcceptanceTest extends AcceptanceTestBase {
Expand All @@ -28,11 +29,10 @@ void upcheckOnCorrectPortRespondsWithOK() {
given()
.baseUri(signer.getUrl())
.filter(getOpenApiValidationFilter())
.when()
.get("/upcheck")
.then()
.assertThat()
.statusCode(200)
.contentType(ContentType.TEXT)
.body(equalToIgnoringCase("OK"));
}
}
3 changes: 3 additions & 0 deletions core/src/main/java/tech/pegasys/eth2signer/core/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ private OpenAPI3RouterFactory createOpenApiRouterFactory(
throws InterruptedException, ExecutionException {
final LogErrorHandler errorHandler = new LogErrorHandler();
final OpenAPI3RouterFactory openAPI3RouterFactory = getOpenAPI3RouterFactory(vertx);
openAPI3RouterFactory
.getOptions()
.setMountResponseContentTypeHandler(false); // manually set content-type

openAPI3RouterFactory.addHandlerByOperationId(UPCHECK_OPERATION_ID, new UpcheckHandler());
openAPI3RouterFactory.addFailureHandlerByOperationId(UPCHECK_OPERATION_ID, errorHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package tech.pegasys.eth2signer.core.http.handlers;

import static com.google.common.net.MediaType.JSON_UTF_8;
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import tech.pegasys.eth2signer.core.signing.ArtifactSignerProvider;

import io.vertx.core.Handler;
Expand All @@ -29,6 +32,6 @@ public GetPublicKeysHandler(final ArtifactSignerProvider signerProvider) {
public void handle(final RoutingContext context) {
final JsonArray jsonArray = new JsonArray();
signerProvider.availableIdentifiers().forEach(jsonArray::add);
context.response().end(jsonArray.encode());
context.response().putHeader(CONTENT_TYPE, JSON_UTF_8.toString()).end(jsonArray.encode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package tech.pegasys.eth2signer.core.http.handlers;

import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import tech.pegasys.artemis.bls.BLSSignature;
import tech.pegasys.eth2signer.core.http.models.SigningRequestBody;
import tech.pegasys.eth2signer.core.signing.ArtifactSigner;
Expand Down Expand Up @@ -49,7 +52,10 @@ public void handle(RoutingContext routingContext) {

final Bytes dataToSign = getDataToSign(params);
final BLSSignature signature = signer.get().sign(dataToSign);
routingContext.response().end(signature.toString());
routingContext
.response()
.putHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString())
.end(signature.toString());
}

private Bytes getDataToSign(final RequestParameters params) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package tech.pegasys.eth2signer.core.http.handlers;

import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;

import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;

public class UpcheckHandler implements Handler<RoutingContext> {

@Override
public void handle(RoutingContext routingContext) {
routingContext.response().end("OK");
routingContext.response().putHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString()).end("OK");
}
}

0 comments on commit 65c7b95

Please sign in to comment.