Comprehensive Servlet 3.1 support in spring-web and spring-test
Issue: SPR-14467
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -37,6 +37,8 @@ public class DelegatingServletInputStream extends ServletInputStream {
|
||||
|
||||
private final InputStream sourceStream;
|
||||
|
||||
private boolean finished = false;
|
||||
|
||||
|
||||
/**
|
||||
* Create a DelegatingServletInputStream for the given source stream.
|
||||
@@ -57,7 +59,11 @@ public class DelegatingServletInputStream extends ServletInputStream {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return this.sourceStream.read();
|
||||
int data = this.sourceStream.read();
|
||||
if (data == -1) {
|
||||
this.finished = true;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,16 +74,17 @@ public class DelegatingServletInputStream extends ServletInputStream {
|
||||
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
throw new UnsupportedOperationException();
|
||||
return this.finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
throw new UnsupportedOperationException();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -74,11 +74,12 @@ public class DelegatingServletOutputStream extends ServletOutputStream {
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
throw new UnsupportedOperationException();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setWriteListener(WriteListener writeListener) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ import org.springframework.util.StringUtils;
|
||||
* is {@link Locale#ENGLISH}. This value can be changed via {@link #addPreferredLocale}
|
||||
* or {@link #setPreferredLocales}.
|
||||
*
|
||||
* <p>As of Spring Framework 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
|
||||
* <p>As of Spring Framework 5.0, this set of mocks is designed on a Servlet 3.1 baseline.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rod Johnson
|
||||
@@ -329,9 +329,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
* throwing an IllegalStateException if not active anymore.
|
||||
*/
|
||||
protected void checkActive() throws IllegalStateException {
|
||||
if (!this.active) {
|
||||
throw new IllegalStateException("Request is not active anymore");
|
||||
}
|
||||
Assert.state(this.active, "Request is not active anymore");
|
||||
}
|
||||
|
||||
|
||||
@@ -808,9 +806,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||
|
||||
@Override
|
||||
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
|
||||
if (!this.asyncSupported) {
|
||||
throw new IllegalStateException("Async not supported");
|
||||
}
|
||||
Assert.state(this.asyncSupported, "Async not supported");
|
||||
this.asyncStarted = true;
|
||||
this.asyncContext = new MockAsyncContext(request, response);
|
||||
return this.asyncContext;
|
||||
|
||||
@@ -45,9 +45,7 @@ import org.springframework.web.util.WebUtils;
|
||||
/**
|
||||
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
|
||||
*
|
||||
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
|
||||
* Beyond that, {@code MockHttpServletResponse} is also compatible with Servlet
|
||||
* 3.1's {@code setContentLengthLong()} method.
|
||||
* <p>As of Spring Framework 5.0, this set of mocks is designed on a Servlet 3.1 baseline.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rod Johnson
|
||||
@@ -154,7 +152,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
* <p>If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value.
|
||||
*/
|
||||
public boolean isCharset() {
|
||||
return charset;
|
||||
return this.charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -181,17 +179,13 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public ServletOutputStream getOutputStream() {
|
||||
if (!this.outputStreamAccessAllowed) {
|
||||
throw new IllegalStateException("OutputStream access not allowed");
|
||||
}
|
||||
Assert.state(this.outputStreamAccessAllowed, "OutputStream access not allowed");
|
||||
return this.outputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getWriter() throws UnsupportedEncodingException {
|
||||
if (!this.writerAccessAllowed) {
|
||||
throw new IllegalStateException("Writer access not allowed");
|
||||
}
|
||||
Assert.state(this.writerAccessAllowed, "Writer access not allowed");
|
||||
if (this.writer == null) {
|
||||
Writer targetWriter = (this.characterEncoding != null ?
|
||||
new OutputStreamWriter(this.content, this.characterEncoding) : new OutputStreamWriter(this.content));
|
||||
@@ -275,9 +269,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public void resetBuffer() {
|
||||
if (isCommitted()) {
|
||||
throw new IllegalStateException("Cannot reset buffer - response is already committed");
|
||||
}
|
||||
Assert.state(!isCommitted(), "Cannot reset buffer - response is already committed");
|
||||
this.content.reset();
|
||||
}
|
||||
|
||||
@@ -456,9 +448,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public void sendError(int status, String errorMessage) throws IOException {
|
||||
if (isCommitted()) {
|
||||
throw new IllegalStateException("Cannot set error status - response is already committed");
|
||||
}
|
||||
Assert.state(!isCommitted(), "Cannot set error status - response is already committed");
|
||||
this.status = status;
|
||||
this.errorMessage = errorMessage;
|
||||
setCommitted(true);
|
||||
@@ -466,18 +456,14 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
@Override
|
||||
public void sendError(int status) throws IOException {
|
||||
if (isCommitted()) {
|
||||
throw new IllegalStateException("Cannot set error status - response is already committed");
|
||||
}
|
||||
Assert.state(!isCommitted(), "Cannot set error status - response is already committed");
|
||||
this.status = status;
|
||||
setCommitted(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendRedirect(String url) throws IOException {
|
||||
if (isCommitted()) {
|
||||
throw new IllegalStateException("Cannot send redirect - response is already committed");
|
||||
}
|
||||
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
|
||||
Assert.notNull(url, "Redirect URL must not be null");
|
||||
setHeader(LOCATION_HEADER, url);
|
||||
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
|
||||
@@ -627,10 +613,8 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||
|
||||
public String getIncludedUrl() {
|
||||
int count = this.includedUrls.size();
|
||||
if (count > 1) {
|
||||
throw new IllegalStateException(
|
||||
"More than 1 URL included - check getIncludedUrls instead: " + this.includedUrls);
|
||||
}
|
||||
Assert.state(count <= 1,
|
||||
() -> "More than 1 URL included - check getIncludedUrls instead: " + this.includedUrls);
|
||||
return (count == 1 ? this.includedUrls.get(0) : null);
|
||||
}
|
||||
|
||||
|
||||
@@ -355,9 +355,8 @@ public class MockServletContext implements ServletContext {
|
||||
|
||||
@Override
|
||||
public RequestDispatcher getRequestDispatcher(String path) {
|
||||
if (!path.startsWith("/")) {
|
||||
throw new IllegalArgumentException("RequestDispatcher path at ServletContext level must start with '/'");
|
||||
}
|
||||
Assert.isTrue(path.startsWith("/"),
|
||||
() -> "RequestDispatcher path [" + path + "] at ServletContext level must start with '/'");
|
||||
return new MockRequestDispatcher(path);
|
||||
}
|
||||
|
||||
@@ -545,11 +544,6 @@ public class MockServletContext implements ServletContext {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVirtualServerName() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Set<String> getDeclaredRoles() {
|
||||
return Collections.unmodifiableSet(this.declaredRoles);
|
||||
}
|
||||
@@ -682,4 +676,9 @@ public class MockServletContext implements ServletContext {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVirtualServerName() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user