From 88405be8a5953e6541a3821fdeace125492f3764 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Sat, 22 Aug 2015 15:29:19 +0200 Subject: [PATCH] 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 --- .../org/springframework/http/ResponseEntity.java | 8 ++++++++ .../http/ResponseEntityTests.java | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java index 6d279597736..b3e766d4e3c 100644 --- a/spring-web/src/main/java/org/springframework/http/ResponseEntity.java +++ b/spring-web/src/main/java/org/springframework/http/ResponseEntity.java @@ -421,6 +421,14 @@ public class ResponseEntity extends HttpEntity { @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; } diff --git a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java index abd2b556359..dca351a41cc 100644 --- a/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java +++ b/spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java @@ -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 responseEntity = ResponseEntity.ok(). allow(HttpMethod.GET). - eTag(eTag). lastModified(12345L). location(location). contentLength(contentLength). @@ -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 { assertNull(responseEntity.getBody()); } + @Test + public void Etagheader() throws URISyntaxException { + + ResponseEntity 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();