Browse Source

Tests for MockHttpServletRequestTests.setContent reset in 5.0.x

Issue: SPR-17373
pull/1998/head
Juergen Hoeller 6 years ago
parent
commit
52ed4226bd
  1. 25
      spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

25
spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java

@ -34,6 +34,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -73,8 +74,23 @@ public class MockHttpServletRequestTests {
byte[] bytes = "body".getBytes(Charset.defaultCharset()); byte[] bytes = "body".getBytes(Charset.defaultCharset());
request.setContent(bytes); request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength()); assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getInputStream());
assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset())); assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset()));
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset()));
}
@Test
public void setContentAndGetReader() throws IOException {
byte[] bytes = "body".getBytes(Charset.defaultCharset());
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertEquals("body", FileCopyUtils.copyToString(request.getReader()));
request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength());
assertEquals("body", FileCopyUtils.copyToString(request.getReader()));
} }
@Test @Test
@ -82,7 +98,6 @@ public class MockHttpServletRequestTests {
byte[] bytes = "request body".getBytes(); byte[] bytes = "request body".getBytes();
request.setContent(bytes); request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength()); assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getContentAsByteArray());
assertEquals(bytes, request.getContentAsByteArray()); assertEquals(bytes, request.getContentAsByteArray());
} }
@ -100,14 +115,12 @@ public class MockHttpServletRequestTests {
request.setCharacterEncoding("UTF-16"); request.setCharacterEncoding("UTF-16");
request.setContent(bytes); request.setContent(bytes);
assertEquals(bytes.length, request.getContentLength()); assertEquals(bytes.length, request.getContentLength());
assertNotNull(request.getContentAsString());
assertEquals(palindrome, request.getContentAsString()); assertEquals(palindrome, request.getContentAsString());
} }
@Test @Test
public void noContent() throws IOException { public void noContent() throws IOException {
assertEquals(-1, request.getContentLength()); assertEquals(-1, request.getContentLength());
assertNotNull(request.getInputStream());
assertEquals(-1, request.getInputStream().read()); assertEquals(-1, request.getInputStream().read());
assertNull(request.getContentAsByteArray()); assertNull(request.getContentAsByteArray());
} }
@ -180,7 +193,6 @@ public class MockHttpServletRequestTests {
String headerName = "Header1"; String headerName = "Header1";
request.addHeader(headerName, "value1"); request.addHeader(headerName, "value1");
Enumeration<String> requestHeaders = request.getHeaderNames(); Enumeration<String> requestHeaders = request.getHeaderNames();
assertNotNull(requestHeaders);
assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement()); assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement());
} }
@ -511,10 +523,7 @@ public class MockHttpServletRequestTests {
request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
} }
private void assertEqualEnumerations(Enumeration<?> enum1, Enumeration<?> enum2) { private void assertEqualEnumerations(Enumeration<?> enum1, Enumeration<?> enum2) {
assertNotNull(enum1);
assertNotNull(enum2);
int count = 0; int count = 0;
while (enum1.hasMoreElements()) { while (enum1.hasMoreElements()) {
assertTrue("enumerations must be equal in length", enum2.hasMoreElements()); assertTrue("enumerations must be equal in length", enum2.hasMoreElements());

Loading…
Cancel
Save