|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -13,15 +13,16 @@ |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.web.util; |
|
|
|
package org.springframework.web.util; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Assert; |
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.mock.web.test.MockHttpServletRequest; |
|
|
|
import org.springframework.mock.web.test.MockHttpServletRequest; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
import org.springframework.util.FileCopyUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Brian Clozel |
|
|
|
* @author Brian Clozel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@ -31,12 +32,8 @@ public class ContentCachingRequestWrapperTests { |
|
|
|
|
|
|
|
|
|
|
|
protected static final String CHARSET = "UTF-8"; |
|
|
|
protected static final String CHARSET = "UTF-8"; |
|
|
|
|
|
|
|
|
|
|
|
private MockHttpServletRequest request; |
|
|
|
private final MockHttpServletRequest request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
|
|
|
|
public void setup() throws Exception { |
|
|
|
|
|
|
|
this.request = new MockHttpServletRequest(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void cachedContent() throws Exception { |
|
|
|
public void cachedContent() throws Exception { |
|
|
|
@ -46,7 +43,41 @@ public class ContentCachingRequestWrapperTests { |
|
|
|
|
|
|
|
|
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
Assert.assertArrayEquals(response, wrapper.getContentAsByteArray()); |
|
|
|
assertArrayEquals(response, wrapper.getContentAsByteArray()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void cachedContentWithLimit() throws Exception { |
|
|
|
|
|
|
|
this.request.setMethod("GET"); |
|
|
|
|
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
|
|
|
|
this.request.setContent("Hello World".getBytes(CHARSET)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3); |
|
|
|
|
|
|
|
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
|
|
|
|
assertArrayEquals("Hello World".getBytes(CHARSET), response); |
|
|
|
|
|
|
|
assertArrayEquals("Hel".getBytes(CHARSET), wrapper.getContentAsByteArray()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void cachedContentWithOverflow() throws Exception { |
|
|
|
|
|
|
|
this.request.setMethod("GET"); |
|
|
|
|
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
|
|
|
|
this.request.setContent("Hello World".getBytes(CHARSET)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3) { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected void handleContentOverflow(int contentCacheLimit) { |
|
|
|
|
|
|
|
throw new IllegalStateException(String.valueOf(contentCacheLimit)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
|
|
|
|
fail("Should have thrown IllegalStateException"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (IllegalStateException ex) { |
|
|
|
|
|
|
|
assertEquals("3", ex.getMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@ -55,29 +86,28 @@ public class ContentCachingRequestWrapperTests { |
|
|
|
this.request.setContentType(FORM_CONTENT_TYPE); |
|
|
|
this.request.setContentType(FORM_CONTENT_TYPE); |
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
this.request.setParameter("first", "value"); |
|
|
|
this.request.setParameter("first", "value"); |
|
|
|
this.request.setParameter("second", new String[] {"foo", "bar"}); |
|
|
|
this.request.setParameter("second", "foo", "bar"); |
|
|
|
|
|
|
|
|
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
// getting request parameters will consume the request body
|
|
|
|
// getting request parameters will consume the request body
|
|
|
|
Assert.assertFalse(wrapper.getParameterMap().isEmpty()); |
|
|
|
assertFalse(wrapper.getParameterMap().isEmpty()); |
|
|
|
Assert.assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray())); |
|
|
|
assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray())); |
|
|
|
// SPR-12810 : inputstream body should be consumed
|
|
|
|
// SPR-12810 : inputstream body should be consumed
|
|
|
|
Assert.assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))); |
|
|
|
assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SPR-12810
|
|
|
|
@Test // SPR-12810
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void inputStreamFormPostRequest() throws Exception { |
|
|
|
public void inputStreamFormPostRequest() throws Exception { |
|
|
|
this.request.setMethod("POST"); |
|
|
|
this.request.setMethod("POST"); |
|
|
|
this.request.setContentType(FORM_CONTENT_TYPE); |
|
|
|
this.request.setContentType(FORM_CONTENT_TYPE); |
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
this.request.setCharacterEncoding(CHARSET); |
|
|
|
this.request.setParameter("first", "value"); |
|
|
|
this.request.setParameter("first", "value"); |
|
|
|
this.request.setParameter("second", new String[] {"foo", "bar"}); |
|
|
|
this.request.setParameter("second", "foo", "bar"); |
|
|
|
|
|
|
|
|
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); |
|
|
|
|
|
|
|
|
|
|
|
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); |
|
|
|
Assert.assertArrayEquals(response, wrapper.getContentAsByteArray()); |
|
|
|
assertArrayEquals(response, wrapper.getContentAsByteArray()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|