diff --git a/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java b/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java index 0de0ab476a1a..936f76a839b5 100644 --- a/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java +++ b/spring-web/src/test/java/org/springframework/web/filter/ShallowEtagHeaderFilterTests.java @@ -17,21 +17,22 @@ package org.springframework.web.filter; import java.io.InputStream; -import java.nio.charset.StandardCharsets; import jakarta.servlet.FilterChain; import jakarta.servlet.http.HttpServletResponse; import org.junit.jupiter.api.Test; -import org.springframework.http.MediaType; import org.springframework.util.FileCopyUtils; import org.springframework.web.testfixture.servlet.MockHttpServletRequest; import org.springframework.web.testfixture.servlet.MockHttpServletResponse; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; /** * Tests for {@link ShallowEtagHeaderFilter}. + * * @author Arjen Poutsma * @author Brian Clozel * @author Juergen Hoeller @@ -62,14 +63,14 @@ void isEligibleForEtag() { @Test void filterNoMatch() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); - filterResponse.setContentType(MediaType.TEXT_PLAIN_VALUE); + filterResponse.setContentType(TEXT_PLAIN_VALUE); FileCopyUtils.copy(responseBody, filterResponse.getOutputStream()); }; filter.doFilter(request, response, filterChain); @@ -77,21 +78,21 @@ void filterNoMatch() throws Exception { assertThat(response.getStatus()).as("Invalid status").isEqualTo(200); assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\""); assertThat(response.getContentLength()).as("Invalid Content-Length header").isGreaterThan(0); - assertThat(response.getContentType()).as("Invalid Content-Type header").isEqualTo(MediaType.TEXT_PLAIN_VALUE); + assertThat(response.getContentType()).as("Invalid Content-Type header").isEqualTo(TEXT_PLAIN_VALUE); assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody); } @Test void filterNoMatchWeakETag() throws Exception { this.filter.setWriteWeakETag(true); - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); - filterResponse.setContentType(MediaType.TEXT_PLAIN_VALUE); + filterResponse.setContentType(TEXT_PLAIN_VALUE); FileCopyUtils.copy(responseBody, filterResponse.getOutputStream()); }; filter.doFilter(request, response, filterChain); @@ -99,22 +100,22 @@ void filterNoMatchWeakETag() throws Exception { assertThat(response.getStatus()).as("Invalid status").isEqualTo(200); assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("W/\"0b10a8db164e0754105b7a99be72e3fe5\""); assertThat(response.getContentLength()).as("Invalid Content-Length header").isGreaterThan(0); - assertThat(response.getContentType()).as("Invalid Content-Type header").isEqualTo(MediaType.TEXT_PLAIN_VALUE); + assertThat(response.getContentType()).as("Invalid Content-Type header").isEqualTo(TEXT_PLAIN_VALUE); assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(responseBody); } @Test void filterMatch() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\""; request.addHeader("If-None-Match", etag); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); - byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); filterResponse.setContentLength(responseBody.length); - filterResponse.setContentType(MediaType.TEXT_PLAIN_VALUE); + filterResponse.setContentType(TEXT_PLAIN_VALUE); FileCopyUtils.copy(responseBody, filterResponse.getOutputStream()); }; filter.doFilter(request, response, filterChain); @@ -123,20 +124,19 @@ void filterMatch() throws Exception { assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\""); assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse(); assertThat(response.containsHeader("Content-Type")).as("Response has Content-Type header").isFalse(); - byte[] expecteds = new byte[0]; - assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds); + assertThat(response.getContentAsByteArray()).as("Invalid content").isEmpty(); } @Test void filterMatchWeakEtag() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\""; request.addHeader("If-None-Match", "W/" + etag); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); - byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FileCopyUtils.copy(responseBody, filterResponse.getOutputStream()); filterResponse.setContentLength(responseBody.length); }; @@ -145,13 +145,12 @@ void filterMatchWeakEtag() throws Exception { assertThat(response.getStatus()).as("Invalid status").isEqualTo(304); assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\""); assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse(); - byte[] expecteds = new byte[0]; - assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds); + assertThat(response.getContentAsByteArray()).as("Invalid content").isEmpty(); } @Test void filterWriter() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); String etag = "\"0b10a8db164e0754105b7a99be72e3fe5\""; request.addHeader("If-None-Match", etag); MockHttpServletResponse response = new MockHttpServletResponse(); @@ -167,16 +166,15 @@ void filterWriter() throws Exception { assertThat(response.getStatus()).as("Invalid status").isEqualTo(304); assertThat(response.getHeader("ETag")).as("Invalid ETag").isEqualTo("\"0b10a8db164e0754105b7a99be72e3fe5\""); assertThat(response.containsHeader("Content-Length")).as("Response has Content-Length header").isFalse(); - byte[] expecteds = new byte[0]; - assertThat(response.getContentAsByteArray()).as("Invalid content").isEqualTo(expecteds); + assertThat(response.getContentAsByteArray()).as("Invalid content").isEmpty(); } @Test // SPR-12960 public void filterWriterWithDisabledCaching() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); @@ -193,10 +191,10 @@ public void filterWriterWithDisabledCaching() throws Exception { @Test void filterSendError() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); response.setContentLength(100); @@ -213,10 +211,10 @@ void filterSendError() throws Exception { @Test void filterSendErrorMessage() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); response.setContentLength(100); @@ -234,10 +232,10 @@ void filterSendErrorMessage() throws Exception { @Test void filterSendRedirect() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); response.setContentLength(100); @@ -255,10 +253,10 @@ void filterSendRedirect() throws Exception { @Test // SPR-13717 public void filterFlushResponse() throws Exception { - final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels"); MockHttpServletResponse response = new MockHttpServletResponse(); - final byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); + byte[] responseBody = "Hello World".getBytes(UTF_8); FilterChain filterChain = (filterRequest, filterResponse) -> { assertThat(filterRequest).as("Invalid request passed").isEqualTo(request); ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK);