
net.sourceforge.stripes.mock.MockHttpServletResponse Maven / Gradle / Ivy
/* Copyright 2005-2006 Tim Fennell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sourceforge.stripes.mock;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
/**
* Mock implementation of an HttpServletResponse. Captures any output is written along with
* any headers, status information etc. and makes it available through various getter methods.
*
* Of major note is the fact that none of the setStatus(), sendError() or sendRedirect() methods
* have any real effect on the request processing lifecycle. Information is recorded so it can be
* verified what was invoked, but that is all.
*
* @author Tim Fennell
* @since Stripes 1.1.1
*/
public class MockHttpServletResponse implements HttpServletResponse {
private MockServletOutputStream out = new MockServletOutputStream();
private PrintWriter writer = new PrintWriter(out, true);
private Locale locale = Locale.getDefault();
private Map> headers = new HashMap>();
private List cookies = new ArrayList();
private int status = 200;
private String errorMessage;
private String characterEncoding = "UTF-8";
private int contentLength;
private String contentType = "text/html";
private String redirectUrl;
/** Adds a cookie to the set of cookies in the response. */
public void addCookie(Cookie cookie) {
// Remove existing cookies with the same name as the new one
ListIterator iterator = cookies.listIterator();
while (iterator.hasNext()) {
if (iterator.next().getName().equals(cookie.getName()))
iterator.remove();
}
this.cookies.add(cookie);
}
/** Gets the set of cookies stored in the response. */
public Cookie[] getCookies() { return this.cookies.toArray(new Cookie[this.cookies.size()]); }
/** Returns true if the specified header was placed in the response. */
public boolean containsHeader(String name) { return this.headers.containsKey(name); }
/** Returns the URL unchanged. */
public String encodeURL(String url) { return url; }
/** Returns the URL unchanged. */
public String encodeRedirectURL(String url) { return url; }
/** Returns the URL unchanged. */
public String encodeUrl(String url) { return url; }
/** Returns the URL unchanged. */
public String encodeRedirectUrl(String url) { return url; }
/** Sets the status code and saves the message so it can be retrieved later. */
public void sendError(int status, String errorMessage) throws IOException {
this.status = status;
this.errorMessage = errorMessage;
}
/** Sets that status code to the error code provided. */
public void sendError(int status) throws IOException { this.status = status; }
/** Simply stores the URL that was supplied, so that it can be examined later with getRedirectUrl. */
public void sendRedirect(String url) throws IOException { this.redirectUrl = url; }
/**
* If a call was made to sendRedirect() this method will return the URL that was supplied.
* Otherwise it will return null.
*/
public String getRedirectUrl() { return this.redirectUrl; }
/** Stores the value in a Long and saves it as a header. */
public void setDateHeader(String name, long value) {
this.headers.remove(name);
addDateHeader(name, value);
}
/** Adds the specified value for the named header (does not remove/replace existing values). */
public void addDateHeader(String name, long value) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy