Skip to content

Commit 88405be

Browse files
committed
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
1 parent 2df3646 commit 88405be

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,14 @@ public BodyBuilder contentType(MediaType contentType) {
421421

422422
@Override
423423
public BodyBuilder eTag(String eTag) {
424+
if (eTag != null) {
425+
if(!eTag.startsWith("\"") && !eTag.startsWith("W/\"")) {
426+
eTag = "\"" + eTag;
427+
}
428+
if(!eTag.endsWith("\"")) {
429+
eTag = eTag + "\"";
430+
}
431+
}
424432
this.headers.setETag(eTag);
425433
return this;
426434
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,12 @@ public void unprocessableEntity() throws URISyntaxException {
134134

135135
@Test
136136
public void headers() throws URISyntaxException {
137-
String eTag = "\"foo\"";
138137
URI location = new URI("location");
139138
long contentLength = 67890;
140139
MediaType contentType = MediaType.TEXT_PLAIN;
141140

142141
ResponseEntity<Void> responseEntity = ResponseEntity.ok().
143142
allow(HttpMethod.GET).
144-
eTag(eTag).
145143
lastModified(12345L).
146144
location(location).
147145
contentLength(contentLength).
@@ -153,7 +151,6 @@ public void headers() throws URISyntaxException {
153151
HttpHeaders responseHeaders = responseEntity.getHeaders();
154152

155153
assertEquals("GET", responseHeaders.getFirst("Allow"));
156-
assertEquals(eTag, responseHeaders.getFirst("ETag"));
157154
assertEquals("Thu, 01 Jan 1970 00:00:12 GMT",
158155
responseHeaders.getFirst("Last-Modified"));
159156
assertEquals(location.toASCIIString(),
@@ -164,6 +161,19 @@ public void headers() throws URISyntaxException {
164161
assertNull(responseEntity.getBody());
165162
}
166163

164+
@Test
165+
public void Etagheader() throws URISyntaxException {
166+
167+
ResponseEntity<Void> responseEntity = ResponseEntity.ok().eTag("\"foo\"").build();
168+
assertEquals("\"foo\"", responseEntity.getHeaders().getETag());
169+
170+
responseEntity = ResponseEntity.ok().eTag("foo").build();
171+
assertEquals("\"foo\"", responseEntity.getHeaders().getETag());
172+
173+
responseEntity = ResponseEntity.ok().eTag("W/\"foo\"").build();
174+
assertEquals("W/\"foo\"", responseEntity.getHeaders().getETag());
175+
}
176+
167177
@Test
168178
public void headersCopy() {
169179
HttpHeaders customHeaders = new HttpHeaders();

0 commit comments

Comments
 (0)