Skip to content

Commit cde4431

Browse files
committed
Add containsKey to ServletResponseHttpHeaders
Issue: SPR-13668
1 parent d500d52 commit cde4431

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

spring-web/src/main/java/org/springframework/http/server/ServletServerHttpResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ private class ServletResponseHttpHeaders extends HttpHeaders {
132132

133133
private static final long serialVersionUID = 3410708522401046302L;
134134

135+
@Override
136+
public boolean containsKey(Object key) {
137+
return (super.containsKey(key) || (get(key) != null));
138+
}
139+
135140
@Override
136141
public String getFirst(String headerName) {
137142
String value = servletResponse.getHeader(headerName);

spring-web/src/test/java/org/springframework/http/server/ServletServerHttpResponseTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717
package org.springframework.http.server;
1818

1919
import java.nio.charset.Charset;
20-
import java.util.Arrays;
20+
import java.util.Collections;
2121
import java.util.List;
2222

2323
import org.junit.Before;
@@ -29,23 +29,28 @@
2929
import org.springframework.mock.web.test.MockHttpServletResponse;
3030
import org.springframework.util.FileCopyUtils;
3131

32-
import static org.junit.Assert.*;
32+
import static org.junit.Assert.assertArrayEquals;
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
3335

3436
/**
3537
* @author Arjen Poutsma
38+
* @author Rossen Stoyanchev
3639
*/
3740
public class ServletServerHttpResponseTests {
3841

3942
private ServletServerHttpResponse response;
4043

4144
private MockHttpServletResponse mockResponse;
4245

46+
4347
@Before
4448
public void create() throws Exception {
4549
mockResponse = new MockHttpServletResponse();
4650
response = new ServletServerHttpResponse(mockResponse);
4751
}
4852

53+
4954
@Test
5055
public void setStatusCode() throws Exception {
5156
response.setStatusCode(HttpStatus.NOT_FOUND);
@@ -73,7 +78,7 @@ public void getHeaders() throws Exception {
7378
}
7479

7580
@Test
76-
public void getHeadersFromHttpServletResponse() {
81+
public void preExistingHeadersFromHttpServletResponse() {
7782

7883
String headerName = "Access-Control-Allow-Origin";
7984
String headerValue = "localhost:8080";
@@ -82,7 +87,8 @@ public void getHeadersFromHttpServletResponse() {
8287
this.response = new ServletServerHttpResponse(this.mockResponse);
8388

8489
assertEquals(headerValue, this.response.getHeaders().getFirst(headerName));
85-
assertEquals(Arrays.asList(headerValue), this.response.getHeaders().get(headerName));
90+
assertEquals(Collections.singletonList(headerValue), this.response.getHeaders().get(headerName));
91+
assertTrue(this.response.getHeaders().containsKey(headerName));
8692
}
8793

8894
@Test

0 commit comments

Comments
 (0)