Skip to content

Commit f9bb4b3

Browse files
committed
MockHttpServletResponse's getHeaderNames declares Collection instead of Set for Servlet 3.0 compatibility
Issue: SPR-9885
1 parent 01f3102 commit f9bb4b3

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

org.springframework.test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -24,11 +24,11 @@
2424
import java.io.UnsupportedEncodingException;
2525
import java.io.Writer;
2626
import java.util.ArrayList;
27+
import java.util.Collection;
2728
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.Locale;
3031
import java.util.Map;
31-
import java.util.Set;
3232
import javax.servlet.ServletOutputStream;
3333
import javax.servlet.http.Cookie;
3434
import javax.servlet.http.HttpServletResponse;
@@ -51,9 +51,10 @@ public class MockHttpServletResponse implements HttpServletResponse {
5151
private static final String CHARSET_PREFIX = "charset=";
5252

5353
private static final String CONTENT_TYPE_HEADER = "Content-Type";
54-
54+
5555
private static final String CONTENT_LENGTH_HEADER = "Content-Length";
5656

57+
5758
//---------------------------------------------------------------------
5859
// ServletResponse properties
5960
//---------------------------------------------------------------------
@@ -63,7 +64,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
6364
private boolean writerAccessAllowed = true;
6465

6566
private String characterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
66-
67+
6768
private boolean charset = false;
6869

6970
private final ByteArrayOutputStream content = new ByteArrayOutputStream();
@@ -108,7 +109,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
108109

109110
/**
110111
* Set whether {@link #getOutputStream()} access is allowed.
111-
* <p>Default is <code>true</code>.
112+
* <p>Default is {@code true}.
112113
*/
113114
public void setOutputStreamAccessAllowed(boolean outputStreamAccessAllowed) {
114115
this.outputStreamAccessAllowed = outputStreamAccessAllowed;
@@ -123,7 +124,7 @@ public boolean isOutputStreamAccessAllowed() {
123124

124125
/**
125126
* Set whether {@link #getWriter()} access is allowed.
126-
* <p>Default is <code>true</code>.
127+
* <p>Default is {@code true}.
127128
*/
128129
public void setWriterAccessAllowed(boolean writerAccessAllowed) {
129130
this.writerAccessAllowed = writerAccessAllowed;
@@ -141,17 +142,17 @@ public void setCharacterEncoding(String characterEncoding) {
141142
this.charset = true;
142143
updateContentTypeHeader();
143144
}
144-
145+
145146
private void updateContentTypeHeader() {
146147
if (this.contentType != null) {
147148
StringBuilder sb = new StringBuilder(this.contentType);
148-
if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) {
149+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
149150
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
150151
}
151152
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
152153
}
153154
}
154-
155+
155156
public String getCharacterEncoding() {
156157
return this.characterEncoding;
157158
}
@@ -297,20 +298,20 @@ public boolean containsHeader(String name) {
297298
/**
298299
* Return the names of all specified headers as a Set of Strings.
299300
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
300-
* @return the <code>Set</code> of header name <code>Strings</code>, or an empty <code>Set</code> if none
301+
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
301302
*/
302-
public Set<String> getHeaderNames() {
303+
public Collection<String> getHeaderNames() {
303304
return this.headers.keySet();
304305
}
305306

306307
/**
307308
* Return the primary value for the given header as a String, if any.
308309
* Will return the first value in case of multiple values.
309-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
310+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
310311
* As of Spring 3.1, it returns a stringified value for Servlet 3.0 compatibility.
311312
* Consider using {@link #getHeaderValue(String)} for raw Object access.
312313
* @param name the name of the header
313-
* @return the associated header value, or <code>null<code> if none
314+
* @return the associated header value, or {@code null} if none
314315
*/
315316
public String getHeader(String name) {
316317
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
@@ -319,7 +320,7 @@ public String getHeader(String name) {
319320

320321
/**
321322
* Return all values for the given header as a List of Strings.
322-
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
323+
* <p>As of Servlet 3.0, this method is also defined in HttpServletResponse.
323324
* As of Spring 3.1, it returns a List of stringified values for Servlet 3.0 compatibility.
324325
* Consider using {@link #getHeaderValues(String)} for raw Object access.
325326
* @param name the name of the header
@@ -339,7 +340,7 @@ public List<String> getHeaders(String name) {
339340
* Return the primary value for the given header, if any.
340341
* <p>Will return the first value in case of multiple values.
341342
* @param name the name of the header
342-
* @return the associated header value, or <code>null<code> if none
343+
* @return the associated header value, or {@code null} if none
343344
*/
344345
public Object getHeaderValue(String name) {
345346
HeaderValueHolder header = HeaderValueHolder.getByName(this.headers, name);
@@ -374,7 +375,7 @@ public String encodeURL(String url) {
374375
* returning the given URL String as-is.
375376
* <p>Can be overridden in subclasses, appending a session id or the like
376377
* in a redirect-specific fashion. For general URL encoding rules,
377-
* override the common {@link #encodeURL} method instead, appyling
378+
* override the common {@link #encodeURL} method instead, applying
378379
* to redirect URLs as well as to general URLs.
379380
*/
380381
public String encodeRedirectURL(String url) {
@@ -456,7 +457,7 @@ private void addHeaderValue(String name, Object value) {
456457
}
457458
doAddHeaderValue(name, value, false);
458459
}
459-
460+
460461
private boolean setSpecialHeader(String name, Object value) {
461462
if (CONTENT_TYPE_HEADER.equalsIgnoreCase(name)) {
462463
setContentType((String) value);

org.springframework.test/src/test/java/org/springframework/mock/web/MockHttpServletResponseTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -18,7 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.util.Arrays;
21-
import java.util.Set;
21+
import java.util.Collection;
2222

2323
import junit.framework.TestCase;
2424

@@ -117,7 +117,7 @@ public void testHttpHeaderNameCasingIsPreserved() throws Exception {
117117

118118
MockHttpServletResponse response = new MockHttpServletResponse();
119119
response.addHeader(headerName, "value1");
120-
Set<String> responseHeaders = response.getHeaderNames();
120+
Collection<String> responseHeaders = response.getHeaderNames();
121121
assertNotNull(responseHeaders);
122122
assertEquals(1, responseHeaders.size());
123123
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());

0 commit comments

Comments
 (0)