Skip to content

Commit 5a773b7

Browse files
jhoellerunknown
authored andcommitted
MockHttpServletResponse's getHeaderNames declares Collection instead of Set for Servlet 3.0 compatibility
Issue: SPR-9885
1 parent c1a4f5c commit 5a773b7

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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;
@@ -56,6 +56,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
5656

5757
private static final String LOCATION_HEADER = "Location";
5858

59+
5960
//---------------------------------------------------------------------
6061
// ServletResponse properties
6162
//---------------------------------------------------------------------
@@ -145,7 +146,7 @@ public void setCharacterEncoding(String characterEncoding) {
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);
@@ -299,7 +300,7 @@ public boolean containsHeader(String name) {
299300
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
300301
* @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

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -16,18 +16,17 @@
1616

1717
package org.springframework.mock.web;
1818

19-
import static org.junit.Assert.*;
20-
2119
import java.io.IOException;
2220
import java.util.Arrays;
23-
import java.util.Set;
24-
21+
import java.util.Collection;
2522
import javax.servlet.http.HttpServletResponse;
2623

2724
import org.junit.Test;
2825

2926
import org.springframework.web.util.WebUtils;
3027

28+
import static org.junit.Assert.*;
29+
3130
/**
3231
* Unit tests for {@link MockHttpServletResponse}.
3332
*
@@ -127,7 +126,7 @@ public void contentLengthHeader() {
127126
public void httpHeaderNameCasingIsPreserved() throws Exception {
128127
final String headerName = "Header1";
129128
response.addHeader(headerName, "value1");
130-
Set<String> responseHeaders = response.getHeaderNames();
129+
Collection<String> responseHeaders = response.getHeaderNames();
131130
assertNotNull(responseHeaders);
132131
assertEquals(1, responseHeaders.size());
133132
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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;
@@ -38,11 +38,9 @@
3838
import org.springframework.web.util.WebUtils;
3939

4040
/**
41-
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse}
42-
* interface. Supports the Servlet 3.0 API level
41+
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
4342
*
44-
* <p>Used for testing the web framework; also useful for testing
45-
* application controllers.
43+
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0.
4644
*
4745
* @author Juergen Hoeller
4846
* @author Rod Johnson
@@ -58,6 +56,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
5856

5957
private static final String LOCATION_HEADER = "Location";
6058

59+
6160
//---------------------------------------------------------------------
6261
// ServletResponse properties
6362
//---------------------------------------------------------------------
@@ -148,7 +147,7 @@ public void setCharacterEncoding(String characterEncoding) {
148147
private void updateContentTypeHeader() {
149148
if (this.contentType != null) {
150149
StringBuilder sb = new StringBuilder(this.contentType);
151-
if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) {
150+
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
152151
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
153152
}
154153
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
@@ -319,7 +318,7 @@ public boolean containsHeader(String name) {
319318
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
320319
*/
321320
@Override
322-
public Set<String> getHeaderNames() {
321+
public Collection<String> getHeaderNames() {
323322
return this.headers.keySet();
324323
}
325324

0 commit comments

Comments
 (0)