Skip to content
Open
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 @@ -82,9 +82,13 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {

if (shouldUseCompression(request)) {
response.setHeader("Content-Encoding", "gzip");
try (GZIPOutputStream gzipOutputStream =
new GZIPOutputStream(response.sendHeadersAndGetBody(200, 0))) {
writer.write(gzipOutputStream, snapshots);
if (request.getMethod().equals("HEAD")) {
response.sendHeadersAndGetBody(200, -1);
} else {
try (GZIPOutputStream gzipOutputStream =
new GZIPOutputStream(response.sendHeadersAndGetBody(200, 0))) {
writer.write(gzipOutputStream, snapshots);
}
}
} else {
ByteArrayOutputStream responseBuffer =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,29 @@ void testMultipleAcceptEncodingHeaders() throws IOException {

@Test
void testHeadRequest() throws IOException {
TestHttpExchange exchange = new TestHttpExchange("HEAD", null);
TestHttpExchange exchange =
new TestHttpExchange("HEAD", null).withHeader("Accept-Encoding", "identity");
handler.handleRequest(exchange);

assertThat(exchange.getResponseCode()).isEqualTo(200);
assertThat(exchange.getResponseHeaders().get("Content-Length")).isNotNull();
assertThat(exchange.getResponseHeaders()).doesNotContainKey("Content-Encoding");
// For HEAD requests, body should be empty even though Content-Length is set
assertThat(exchange.rawResponseBody.size()).isEqualTo(0);
assertThat(exchange.response.contentLength).isEqualTo(-1);
}

@Test
void testHeadRequestWithGzip() throws IOException {
TestHttpExchange exchange =
new TestHttpExchange("HEAD", null).withHeader("Accept-Encoding", "gzip");
handler.handleRequest(exchange);

assertThat(exchange.getResponseCode()).isEqualTo(200);
assertThat(exchange.getResponseHeaders().get("Content-Encoding")).isEqualTo("gzip");
assertThat(exchange.getResponseHeaders().get("Content-Type")).contains("text/plain");
assertThat(exchange.rawResponseBody.size()).isZero();
assertThat(exchange.response.contentLength).isEqualTo(-1);
}

@Test
Expand Down Expand Up @@ -362,6 +378,7 @@ private static class TestHttpResponse implements PrometheusHttpResponse {
private final Map<String, String> headers = new HashMap<>();
private final ByteArrayOutputStream outputStream;
private int statusCode;
private int contentLength;

TestHttpResponse(ByteArrayOutputStream outputStream) {
this.outputStream = outputStream;
Expand All @@ -376,6 +393,7 @@ public void setHeader(String name, String value) {
public OutputStream sendHeadersAndGetBody(int statusCode, int contentLength)
throws IOException {
this.statusCode = statusCode;
this.contentLength = contentLength;
return outputStream;
}
}
Expand Down