ResponseEntity's headers(HttpHeaders) accepts null value

Issue: SPR-12792
(cherry picked from commit 73e8021)
This commit is contained in:
Juergen Hoeller 2015-03-10 15:11:58 +01:00
parent 6b9e89118f
commit cb6459c271
2 changed files with 32 additions and 2 deletions

View File

@ -381,7 +381,9 @@ public class ResponseEntity<T> extends HttpEntity<T> {
@Override
public BodyBuilder headers(HttpHeaders headers) {
if (headers != null) {
this.headers.putAll(headers);
}
return this;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Marcel Overdijk
* @author Kazuki Shimizu
*/
public class ResponseEntityTests {
@ -161,4 +162,31 @@ public class ResponseEntityTests {
assertNull(responseEntity.getBody());
}
@Test
public void headersCopy(){
HttpHeaders customHeaders = new HttpHeaders();
customHeaders.set("X-CustomHeader", "vale");
ResponseEntity<Void> responseEntity = ResponseEntity.ok().headers(customHeaders).build();
HttpHeaders responseHeaders = responseEntity.getHeaders();
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(1, responseHeaders.size());
assertEquals(1, responseHeaders.get("X-CustomHeader").size());
assertEquals("vale", responseHeaders.getFirst("X-CustomHeader"));
}
@Test // SPR-12792
public void headersCopyWithEmptyAndNull(){
ResponseEntity<Void> responseEntityWithEmptyHeaders =
ResponseEntity.ok().headers(new HttpHeaders()).build();
ResponseEntity<Void> responseEntityWithNullHeaders =
ResponseEntity.ok().headers(null).build();
assertEquals(HttpStatus.OK, responseEntityWithEmptyHeaders.getStatusCode());
assertTrue(responseEntityWithEmptyHeaders.getHeaders().isEmpty());
assertEquals(responseEntityWithEmptyHeaders.toString(), responseEntityWithNullHeaders.toString());
}
}