Skip to content

Commit 2a454e0

Browse files
committed
test: Cover the remaining content-coding branches
Fills the gaps JaCoCo flagged: repeated codings in one Accept-Encoding header, weight parameters mixed with others, a valueless parameter, 304 responses, an unparsable hand-set Content-Length, and streams whose handler supplied its own Content-Type or Content-Encoding. Branch coverage on AcceptEncodingHeader goes 70% to 93% and on ResponseRenderer 83% to 87%, keeping the new code clear of the Sonar new-code gate.
1 parent 5cbb498 commit 2a454e0

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/test/java/com/retailsvc/http/internal/AcceptEncodingHeaderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,26 @@ void malformedQValueIsTreatedAsAccepted() {
9191
void emptyTokensAreIgnored() {
9292
assertThat(AcceptEncodingHeader.acceptsGzip("deflate,,gzip")).isTrue();
9393
}
94+
95+
@Test
96+
void repeatedGzipTokensTakeThePositiveWeight() {
97+
assertThat(AcceptEncodingHeader.acceptsGzip("gzip;q=0, gzip")).isTrue();
98+
assertThat(AcceptEncodingHeader.acceptsGzip("gzip, x-gzip;q=0")).isTrue();
99+
}
100+
101+
@Test
102+
void repeatedWildcardsTakeThePositiveWeight() {
103+
assertThat(AcceptEncodingHeader.acceptsGzip("*;q=0, *")).isTrue();
104+
}
105+
106+
@Test
107+
void parametersOtherThanWeightAreIgnored() {
108+
assertThat(AcceptEncodingHeader.acceptsGzip("gzip;level=9")).isTrue();
109+
assertThat(AcceptEncodingHeader.acceptsGzip("gzip;level=9;q=0")).isFalse();
110+
}
111+
112+
@Test
113+
void valuelessParameterIsIgnored() {
114+
assertThat(AcceptEncodingHeader.acceptsGzip("gzip;q")).isTrue();
115+
}
94116
}

src/test/java/com/retailsvc/http/internal/ResponseRendererTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.retailsvc.http.internal;
22

3+
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
34
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
45
import static java.net.HttpURLConnection.HTTP_OK;
56
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -346,6 +347,56 @@ void keepsContentLengthOnNullBodyBelowThreshold() throws IOException {
346347
assertThat(responseHeaders.getFirst("Content-Length")).isEqualTo("12");
347348
}
348349

350+
@Test
351+
void neverCompressesNotModifiedResponses() throws IOException {
352+
acceptsGzip();
353+
354+
renderer.render(exchange, Response.bytes(HTTP_NOT_MODIFIED, largeText(), JSON));
355+
356+
assertThat(responseHeaders.getFirst(CONTENT_ENCODING)).isNull();
357+
}
358+
359+
@Test
360+
void keepsContentLengthOnNullBodyWhenUnparsable() throws IOException {
361+
acceptsGzip();
362+
363+
renderer.render(
364+
exchange,
365+
Response.status(HTTP_OK)
366+
.withContentType(TEXT)
367+
.withHeader("Content-Length", "not-a-number"));
368+
369+
assertThat(responseHeaders.getFirst("Content-Length")).isEqualTo("not-a-number");
370+
}
371+
372+
@Test
373+
void keepsHandlerSuppliedContentTypeOnStreams() throws IOException {
374+
acceptsGzip();
375+
byte[] payload = largeText();
376+
377+
renderer.render(
378+
exchange,
379+
Response.stream(HTTP_OK, TEXT, out -> out.write(payload))
380+
.withHeader("Content-Type", "text/csv"));
381+
382+
assertThat(responseHeaders.get("Content-Type")).containsExactly("text/csv");
383+
assertThat(gunzip(sink.toByteArray())).isEqualTo(payload);
384+
}
385+
386+
@Test
387+
void skipsCompressionOnStreamWhenHandlerAlreadySetContentEncoding() throws IOException {
388+
acceptsGzip();
389+
byte[] payload = largeText();
390+
391+
renderer.render(
392+
exchange,
393+
Response.stream(HTTP_OK, TEXT, out -> out.write(payload))
394+
.withHeader(CONTENT_ENCODING, "br"));
395+
396+
assertThat(responseHeaders.get(CONTENT_ENCODING)).containsExactly("br");
397+
assertThat(sink.toByteArray()).isEqualTo(payload);
398+
}
399+
349400
private void acceptsGzip() {
350401
requestHeaders.add("Accept-Encoding", "gzip, deflate, br");
351402
}

0 commit comments

Comments
 (0)