Skip to content

Commit 4863997

Browse files
committed
Add tests for warning headers removing.
1 parent 1d1d870 commit 4863997

File tree

3 files changed

+49
-27
lines changed

3 files changed

+49
-27
lines changed

src/main/java/io/github/nstdio/http/ext/CacheEntryMetadata.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.time.Clock;
2323
import java.time.Instant;
2424
import java.time.temporal.ChronoUnit;
25-
import java.util.List;
2625
import java.util.Optional;
2726
import java.util.concurrent.TimeUnit;
2827

@@ -34,7 +33,6 @@
3433
import static io.github.nstdio.http.ext.Headers.parseInstant;
3534
import static io.github.nstdio.http.ext.Headers.toRFC1123;
3635
import static java.util.concurrent.TimeUnit.MILLISECONDS;
37-
import static java.util.stream.Collectors.toUnmodifiableList;
3836

3937
public final class CacheEntryMetadata {
4038
private final HttpRequest request;
@@ -236,15 +234,14 @@ synchronized void update(HttpHeaders responseHeaders, long requestTimeMs, long r
236234
this.requestTimeMs = requestTimeMs;
237235
this.responseTimeMs = responseTimeMs;
238236

239-
List<String> toRemove = response.headers()
240-
.allValues(HEADER_WARNING)
241-
.stream()
242-
.filter(warn -> warn.startsWith("1"))
243-
.collect(toUnmodifiableList());
237+
var headersBuilder = new HttpHeadersBuilder(response.headers());
244238

245-
HttpHeadersBuilder headersBuilder = new HttpHeadersBuilder(response.headers());
239+
for (String warn : response.headers().allValues(HEADER_WARNING)) {
240+
if (warn.startsWith("1")) {
241+
headersBuilder.remove(HEADER_WARNING, warn);
242+
}
243+
}
246244

247-
toRemove.forEach(value -> headersBuilder.remove(HEADER_WARNING, value));
248245
responseHeaders.map().forEach(headersBuilder::set);
249246

250247
response = ImmutableResponseInfo.toBuilder(response).headers(headersBuilder.build()).build();

src/test/kotlin/io/github/nstdio/http/ext/CacheEntryMetadataTest.kt

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
*/
1616
package io.github.nstdio.http.ext
1717

18+
import io.github.nstdio.http.ext.Headers.HEADER_WARNING
19+
import io.github.nstdio.http.ext.Helpers.responseInfo
20+
import io.github.nstdio.http.ext.Helpers.responseInfo0
1821
import io.kotest.matchers.booleans.shouldBeFalse
1922
import io.kotest.matchers.booleans.shouldBeTrue
23+
import io.kotest.matchers.collections.shouldContainOnly
2024
import io.kotest.matchers.longs.shouldBeNegative
25+
import io.kotest.matchers.should
2126
import org.junit.jupiter.api.Assertions.assertFalse
2227
import org.junit.jupiter.api.Disabled
2328
import org.junit.jupiter.api.Nested
@@ -47,7 +52,7 @@ internal class CacheEntryMetadataTest {
4752
"Accept-Language", "en-EN"
4853
)
4954
.build()
50-
val m = CacheEntryMetadata(0, 0, Helpers.responseInfo(headers), r, Clock.systemDefaultZone())
55+
val m = CacheEntryMetadata(0, 0, responseInfo(headers), r, Clock.systemDefaultZone())
5156

5257
//when
5358
val actual = m.varyHeaders()
@@ -59,6 +64,34 @@ internal class CacheEntryMetadataTest {
5964
.hasHeaderWithValues("User-Agent", "Java/11")
6065
}
6166

67+
@Test
68+
fun `Should remove warning header from response`() {
69+
//given
70+
val request = HttpRequest.newBuilder(URI.create("https://example.com")).build()
71+
val responseInfo = responseInfo0(
72+
mapOf(
73+
HEADER_WARNING to listOf(
74+
"112 - \"cache down\" \"Wed, 21 Oct 2015 07:28:00 GMT\"",
75+
"110 anderson/1.3.37 \"Response is stale\"",
76+
"299 - \"Deprecated\""
77+
)
78+
)
79+
)
80+
val metadata = CacheEntryMetadata(0, 0, responseInfo, request, Clock.systemDefaultZone())
81+
val responseHeaders = HttpHeadersBuilder()
82+
.add("X-A", "B")
83+
.build()
84+
85+
//when
86+
metadata.update(responseHeaders, 0, 0)
87+
88+
//then
89+
metadata.response().headers().should {
90+
it.allValues(HEADER_WARNING).shouldContainOnly("299 - \"Deprecated\"")
91+
it.allValues("X-A").shouldContainOnly("B")
92+
}
93+
}
94+
6295
@Test
6396
fun shouldGenerateHeuristicExpirationWarning() {
6497
//given
@@ -68,7 +101,7 @@ internal class CacheEntryMetadataTest {
68101
// Creating Last-Modified just about to exceed 24 hour limit
69102
val lastModified = baseInstant.minus(241, ChronoUnit.HOURS)
70103
val expectedExpirationTime = baseInstant.plus(2, ChronoUnit.DAYS)
71-
val info = Helpers.responseInfo(
104+
val info = responseInfo(
72105
mutableMapOf(
73106
"Last-Modified" to Headers.toRFC1123(lastModified),
74107
"Date" to Headers.toRFC1123(Instant.ofEpochSecond(0))
@@ -110,7 +143,7 @@ internal class CacheEntryMetadataTest {
110143
val baseClock = Clock.systemUTC()
111144
val dateHeader = baseClock.instant()
112145
val clock = FixedRateTickClock.of(baseClock, Duration.ofSeconds(1))
113-
val info = Helpers.responseInfo(
146+
val info = responseInfo(
114147
mutableMapOf(
115148
"Cache-Control" to "max-age=1,must-revalidate",
116149
"Date" to Headers.toRFC1123(dateHeader)
@@ -141,7 +174,7 @@ internal class CacheEntryMetadataTest {
141174
val responseTimeMs: Long = 350
142175
val serverDate = responseTimeMs - 50
143176
val clock = Clock.fixed(Instant.ofEpochMilli(responseTimeMs + 5), ZoneId.systemDefault())
144-
val info = Helpers.responseInfo(
177+
val info = responseInfo(
145178
mutableMapOf(
146179
"Cache-Control" to "max-age=1",
147180
"Date" to Headers.toRFC1123(Instant.ofEpochMilli(serverDate))

src/test/kotlin/io/github/nstdio/http/ext/Helpers.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@ import java.net.http.HttpHeaders
2020
import java.net.http.HttpResponse.ResponseInfo
2121

2222
internal object Helpers {
23-
fun responseInfo(headers: Map<String, String>): ResponseInfo {
24-
return object : ResponseInfo {
25-
override fun statusCode(): Int {
26-
return 200
27-
}
23+
fun responseInfo(headers: Map<String, String>) = responseInfo0(headers.mapValues { listOf(it.value) }.toMutableMap())
2824

29-
override fun headers(): HttpHeaders {
30-
return HttpHeaders.of(headers
31-
.mapValues { listOf(it.value) }
32-
.toMutableMap()) { _, _ -> true }
33-
}
25+
fun responseInfo0(headers: Map<String, List<String>>): ResponseInfo = object : ResponseInfo {
26+
override fun statusCode() = 200
3427

35-
override fun version(): HttpClient.Version {
36-
return HttpClient.Version.HTTP_1_1
37-
}
38-
}
28+
override fun headers() = HttpHeaders.of(headers) { _, _ -> true }
29+
30+
override fun version() = HttpClient.Version.HTTP_1_1
3931
}
4032
}

0 commit comments

Comments
 (0)