Browse Source

Quote ETags set with ResponseEntity builder API

Prior to this change, trying to set an unquoted ETag with
`ResponseEntity`'s API would throw an `IllegalArgumentException`.

This commit automatically quotes ETag values set using ResponseEntity.

Issue: SPR-13378
pull/864/merge
Brian Clozel 11 years ago
parent
commit
88405be8a5
  1. 8
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  2. 16
      spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

8
spring-web/src/main/java/org/springframework/http/ResponseEntity.java

@ -421,6 +421,14 @@ public class ResponseEntity<T> extends HttpEntity<T> { @@ -421,6 +421,14 @@ public class ResponseEntity<T> extends HttpEntity<T> {
@Override
public BodyBuilder eTag(String eTag) {
if (eTag != null) {
if(!eTag.startsWith("\"") && !eTag.startsWith("W/\"")) {
eTag = "\"" + eTag;
}
if(!eTag.endsWith("\"")) {
eTag = eTag + "\"";
}
}
this.headers.setETag(eTag);
return this;
}

16
spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

@ -134,14 +134,12 @@ public class ResponseEntityTests { @@ -134,14 +134,12 @@ public class ResponseEntityTests {
@Test
public void headers() throws URISyntaxException {
String eTag = "\"foo\"";
URI location = new URI("location");
long contentLength = 67890;
MediaType contentType = MediaType.TEXT_PLAIN;
ResponseEntity<Void> responseEntity = ResponseEntity.ok().
allow(HttpMethod.GET).
eTag(eTag).
lastModified(12345L).
location(location).
contentLength(contentLength).
@ -153,7 +151,6 @@ public class ResponseEntityTests { @@ -153,7 +151,6 @@ public class ResponseEntityTests {
HttpHeaders responseHeaders = responseEntity.getHeaders();
assertEquals("GET", responseHeaders.getFirst("Allow"));
assertEquals(eTag, responseHeaders.getFirst("ETag"));
assertEquals("Thu, 01 Jan 1970 00:00:12 GMT",
responseHeaders.getFirst("Last-Modified"));
assertEquals(location.toASCIIString(),
@ -164,6 +161,19 @@ public class ResponseEntityTests { @@ -164,6 +161,19 @@ public class ResponseEntityTests {
assertNull(responseEntity.getBody());
}
@Test
public void Etagheader() throws URISyntaxException {
ResponseEntity<Void> responseEntity = ResponseEntity.ok().eTag("\"foo\"").build();
assertEquals("\"foo\"", responseEntity.getHeaders().getETag());
responseEntity = ResponseEntity.ok().eTag("foo").build();
assertEquals("\"foo\"", responseEntity.getHeaders().getETag());
responseEntity = ResponseEntity.ok().eTag("W/\"foo\"").build();
assertEquals("W/\"foo\"", responseEntity.getHeaders().getETag());
}
@Test
public void headersCopy() {
HttpHeaders customHeaders = new HttpHeaders();

Loading…
Cancel
Save