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

fix(api): set Content-Type header on all responses #587

Merged
merged 4 commits into from
Jul 23, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public void handle(RoutingContext ctx) {
if (!validateRequestAuthorization(ctx.request()).get()) {
throw new HttpStatusException(401);
}
// set Content-Type: text/plain by default. Handler implementations may replace this.
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.PLAINTEXT.mime());
handleAuthenticated(ctx);
} catch (HttpStatusException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
import io.cryostat.MainModule;
import io.cryostat.net.AuthManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -92,6 +94,10 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
filePath,
ar -> {
if (ar.result()) {
ctx.response()
.putHeader(
HttpHeaders.CONTENT_TYPE,
HttpMimeType.OCTET_STREAM.mime());
ctx.response().sendFile(filePath);
} else {
ctx.response().setStatusCode(404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.web.WebServer;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
Expand Down Expand Up @@ -150,6 +152,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.reports.ReportService;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
Expand Down Expand Up @@ -94,6 +96,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
String recordingName = ctx.pathParam("recordingName");
try {
Path report = reportService.get(recordingName).get();
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.HTML.mime());
ctx.response().sendFile(report.toAbsolutePath().toString());
} catch (ExecutionException | CompletionException ee) {
if (ExceptionUtils.getRootCause(ee)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -102,6 +104,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
}
return infos;
});
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(templates));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.recordings.RecordingOptionsBuilderFactory;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -106,6 +108,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
recordingOptionsBuilderFactory.create(connection.getService());
return getRecordingOptions(connection.getService(), builder);
});
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(optionMap));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.recordings.RecordingOptionsBuilderFactory;

import com.google.gson.Gson;
import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.impl.HttpStatusException;
Expand Down Expand Up @@ -145,6 +147,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
connection.getService(), builder);
});

ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().setStatusCode(200);
ctx.response().end(gson.toJson(updatedMap));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.web.WebServer;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -116,6 +118,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
}
return list;
});
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(descriptors));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@
import io.cryostat.net.AuthManager;
import io.cryostat.net.TargetConnectionManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -104,6 +106,7 @@ public void handleAuthenticated(RoutingContext ctx) throws Exception {
list.add(ALL_EVENTS_TEMPLATE);
return list;
});
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(templates));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@

import io.cryostat.net.AuthManager;
import io.cryostat.net.web.http.AbstractAuthenticatedRequestHandler;
import io.cryostat.net.web.http.HttpMimeType;
import io.cryostat.net.web.http.api.ApiVersion;
import io.cryostat.platform.PlatformClient;

import com.google.gson.Gson;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.web.RoutingContext;

Expand Down Expand Up @@ -82,6 +84,7 @@ public boolean isAsync() {

@Override
public void handleAuthenticated(RoutingContext ctx) throws Exception {
ctx.response().putHeader(HttpHeaders.CONTENT_TYPE, HttpMimeType.JSON.mime());
ctx.response().end(gson.toJson(this.platformClient.listDiscoverableServices()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ protected void writeResponse(RoutingContext ctx, IntermediateResponse<T> interme
response.setStatusMessage(intermediateResponse.getStatusMessage());
}
intermediateResponse.getHeaders().forEach(response::putHeader);
response.putHeader(HttpHeaders.CONTENT_TYPE, mimeType().mime());

ApiMeta meta = new ApiMeta(mimeType(), response.getStatusMessage());
ApiResultData<T> data = new ApiResultData<>(intermediateResponse.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.cryostat.net.web.http.api.ApiVersion;

import io.vertx.core.MultiMap;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand Down Expand Up @@ -79,6 +80,23 @@ class AbstractAuthenticatedRequestHandlerTest {
@BeforeEach
void setup() {
this.handler = new AuthenticatedHandler(auth);
Mockito.lenient().when(ctx.response()).thenReturn(resp);
Mockito.lenient()
.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.lenient()
.when(resp.putHeader(Mockito.anyString(), Mockito.anyString()))
.thenReturn(resp);
}

@Test
void shouldPutDefaultContentTypeHeader() {
when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
handler.handle(ctx);
Mockito.verify(resp).putHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ void shouldThrow404IfNoMatchingRecordingFound() throws Exception {

Mockito.when(fs.listDirectoryChildren(Mockito.any())).thenReturn(List.of());

Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(404));
Expand Down Expand Up @@ -168,6 +174,12 @@ void shouldThrowExceptionIfDeletionFails() throws Exception {

Mockito.when(ctx.pathParam("recordingName")).thenReturn(recordingName);

Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException ex =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));
MatcherAssert.assertThat(ex.getStatusCode(), Matchers.equalTo(500));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ void shouldHandleCorrectPath() {
})
@NullAndEmptySource
void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(rawUrl);
Expand All @@ -126,6 +132,12 @@ void shouldThrow501IfDatasourceUrlMalformed(String rawUrl) {

@Test
void shouldThrowExceptionIfRecordingNotFound() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -173,6 +185,10 @@ public Void answer(InvocationOnMock args) throws Throwable {

HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

handler.handle(ctx);

Expand Down Expand Up @@ -221,6 +237,13 @@ public Void answer(InvocationOnMock args) throws Throwable {
.when(httpReq)
.sendMultipartForm(Mockito.any(), Mockito.any());

HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);

HttpStatusException e =
Assertions.assertThrows(HttpStatusException.class, () -> handler.handle(ctx));

Expand All @@ -238,6 +261,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullStatusMessage() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down Expand Up @@ -288,6 +317,12 @@ public Void answer(InvocationOnMock args) throws Throwable {

@Test
void shouldHandleNullResponseBody() throws Exception {
HttpServerResponse resp = Mockito.mock(HttpServerResponse.class);
Mockito.when(ctx.response()).thenReturn(resp);
Mockito.when(
resp.putHeader(
Mockito.any(CharSequence.class), Mockito.any(CharSequence.class)))
.thenReturn(resp);
Mockito.when(auth.validateHttpHeader(Mockito.any()))
.thenReturn(CompletableFuture.completedFuture(true));
Mockito.when(env.getEnv("GRAFANA_DATASOURCE_URL")).thenReturn(DATASOURCE_URL);
Expand Down
Loading