Skip to content

Commit

Permalink
fix: setting the response Content-Type
Browse files Browse the repository at this point in the history
This commit closes #365.
  • Loading branch information
halber committed Jan 18, 2024
1 parent be07896 commit 1b04954
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/io/neonbee/endpoint/raw/RawEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ queryParameterMap, multiMapToMap(request.headers()), routingContext.body().buffe
.addHeader("X-HTTP-Method", request.method().name());

DataContextImpl context = new DataContextImpl(routingContext);
requestData(routingContext.vertx(), new DataRequest(qualifiedName, query),
new DataContextImpl(routingContext)).onComplete(asyncResult -> {
requestData(routingContext.vertx(), new DataRequest(qualifiedName, query), context)
.onComplete(asyncResult -> {
if (asyncResult.failed()) {
Throwable cause = asyncResult.cause();
if (cause instanceof DataException dataException) {
Expand Down Expand Up @@ -175,7 +175,7 @@ queryParameterMap, multiMapToMap(request.headers()), routingContext.body().buffe
return;
}

HttpServerResponse response = routingContext.response()//
HttpServerResponse response = routingContext.response()
.putHeader("Content-Type",
Optional.ofNullable(context.responseData().get("Content-Type"))
.map(String.class::cast).orElse("application/json"));
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/io/neonbee/endpoint/raw/RawEndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import io.neonbee.NeonBeeDeployable;
import io.neonbee.data.DataAdapter;
import io.neonbee.data.DataContext;
import io.neonbee.data.DataException;
Expand All @@ -30,6 +31,7 @@
import io.vertx.core.Verticle;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.client.HttpResponse;
Expand Down Expand Up @@ -132,6 +134,33 @@ void testPassQueryToDataQuery(VertxTestContext testContext) {
.onComplete(testContext.succeedingThenComplete());
}

@Test
@DisplayName("RawDataEndpointHandler must set the Content-Type header correct")
void test(VertxTestContext testContext) {
deployVerticle(new RawResponseVerticle())
.compose(s -> sendRequest("Test", "", ""))
.onComplete(testContext.succeeding(resp -> testContext.verify(() -> {
assertThat(resp.getHeader("Content-Type")).isEqualTo("image/gif");
assertThat(resp.bodyAsJsonObject()).isEqualTo(JsonObject.of("foo", "bar"));
testContext.completeNow();
})));
}

@NeonBeeDeployable(namespace = NEONBEE_NAMESPACE, autoDeploy = false)
public static class RawResponseVerticle extends DataVerticle<JsonObject> {

@Override
public Future<JsonObject> retrieveData(DataQuery query, DataContext context) {
context.responseData().put("Content-Type", "image/gif");
return Future.succeededFuture(JsonObject.of("foo", "bar"));
}

@Override
public String getName() {
return "Test";
}
}

private Future<HttpResponse<Buffer>> sendRequest(String verticleName, String path, String query) {
String uriPath = String.format("/raw/%s/%s/%s?%s", NEONBEE_NAMESPACE, verticleName, path, query);
return createRequest(HttpMethod.GET, uriPath).send();
Expand Down

0 comments on commit 1b04954

Please sign in to comment.