Browse Source

ResponseEntity's headers(HttpHeaders) accepts null value

Issue: SPR-12792
pull/755/head
Juergen Hoeller 11 years ago
parent
commit
73e8021e59
  1. 4
      spring-web/src/main/java/org/springframework/http/ResponseEntity.java
  2. 30
      spring-web/src/test/java/org/springframework/http/ResponseEntityTests.java

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

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

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Arjen Poutsma
* @author Marcel Overdijk * @author Marcel Overdijk
* @author Kazuki Shimizu
*/ */
public class ResponseEntityTests { public class ResponseEntityTests {
@ -161,4 +162,31 @@ public class ResponseEntityTests {
assertNull(responseEntity.getBody()); 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());
}
} }

Loading…
Cancel
Save