Skip to content

Commit

Permalink
MockHttpServletResponse's getHeaderNames declares Collection instead …
Browse files Browse the repository at this point in the history
…of Set for Servlet 3.0 compatibility

Issue: SPR-9885
  • Loading branch information
jhoeller authored and unknown committed Jan 22, 2013
1 parent c1a4f5c commit 5a773b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -24,11 +24,11 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -56,6 +56,7 @@ public class MockHttpServletResponse implements HttpServletResponse {

private static final String LOCATION_HEADER = "Location";


//---------------------------------------------------------------------
// ServletResponse properties
//---------------------------------------------------------------------
Expand Down Expand Up @@ -145,7 +146,7 @@ public void setCharacterEncoding(String characterEncoding) {
private void updateContentTypeHeader() {
if (this.contentType != null) {
StringBuilder sb = new StringBuilder(this.contentType);
if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) {
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
}
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
Expand Down Expand Up @@ -299,7 +300,7 @@ public boolean containsHeader(String name) {
* <p>As of Servlet 3.0, this method is also defined HttpServletResponse.
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
*/
public Set<String> getHeaderNames() {
public Collection<String> getHeaderNames() {
return this.headers.keySet();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -16,18 +16,17 @@

package org.springframework.mock.web;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Set;

import java.util.Collection;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;

import org.springframework.web.util.WebUtils;

import static org.junit.Assert.*;

/**
* Unit tests for {@link MockHttpServletResponse}.
*
Expand Down Expand Up @@ -127,7 +126,7 @@ public void contentLengthHeader() {
public void httpHeaderNameCasingIsPreserved() throws Exception {
final String headerName = "Header1";
response.addHeader(headerName, "value1");
Set<String> responseHeaders = response.getHeaderNames();
Collection<String> responseHeaders = response.getHeaderNames();
assertNotNull(responseHeaders);
assertEquals(1, responseHeaders.size());
assertEquals("HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -24,11 +24,11 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -38,11 +38,9 @@
import org.springframework.web.util.WebUtils;

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

private static final String LOCATION_HEADER = "Location";


//---------------------------------------------------------------------
// ServletResponse properties
//---------------------------------------------------------------------
Expand Down Expand Up @@ -148,7 +147,7 @@ public void setCharacterEncoding(String characterEncoding) {
private void updateContentTypeHeader() {
if (this.contentType != null) {
StringBuilder sb = new StringBuilder(this.contentType);
if (this.contentType.toLowerCase().indexOf(CHARSET_PREFIX) == -1 && this.charset) {
if (!this.contentType.toLowerCase().contains(CHARSET_PREFIX) && this.charset) {
sb.append(";").append(CHARSET_PREFIX).append(this.characterEncoding);
}
doAddHeaderValue(CONTENT_TYPE_HEADER, sb.toString(), true);
Expand Down Expand Up @@ -319,7 +318,7 @@ public boolean containsHeader(String name) {
* @return the {@code Set} of header name {@code Strings}, or an empty {@code Set} if none
*/
@Override
public Set<String> getHeaderNames() {
public Collection<String> getHeaderNames() {
return this.headers.keySet();
}

Expand Down

0 comments on commit 5a773b7

Please sign in to comment.