From 52ed4226bd29d9d026b9348fa0811a057c3b0bf2 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 12 Oct 2018 17:57:07 +0200 Subject: [PATCH] Tests for MockHttpServletRequestTests.setContent reset in 5.0.x Issue: SPR-17373 --- .../mock/web/MockHttpServletRequestTests.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java index f3cc3a7a55..4c5c103085 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockHttpServletRequestTests.java +++ b/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.springframework.http.HttpHeaders; +import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils; import static org.junit.Assert.*; @@ -73,8 +74,23 @@ public class MockHttpServletRequestTests { byte[] bytes = "body".getBytes(Charset.defaultCharset()); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); - assertNotNull(request.getInputStream()); 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 @@ -82,7 +98,6 @@ public class MockHttpServletRequestTests { byte[] bytes = "request body".getBytes(); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); - assertNotNull(request.getContentAsByteArray()); assertEquals(bytes, request.getContentAsByteArray()); } @@ -100,14 +115,12 @@ public class MockHttpServletRequestTests { request.setCharacterEncoding("UTF-16"); request.setContent(bytes); assertEquals(bytes.length, request.getContentLength()); - assertNotNull(request.getContentAsString()); assertEquals(palindrome, request.getContentAsString()); } @Test public void noContent() throws IOException { assertEquals(-1, request.getContentLength()); - assertNotNull(request.getInputStream()); assertEquals(-1, request.getInputStream().read()); assertNull(request.getContentAsByteArray()); } @@ -180,7 +193,6 @@ public class MockHttpServletRequestTests { String headerName = "Header1"; request.addHeader(headerName, "value1"); Enumeration requestHeaders = request.getHeaderNames(); - assertNotNull(requestHeaders); assertEquals("HTTP header casing not being preserved", headerName, requestHeaders.nextElement()); } @@ -511,10 +523,7 @@ public class MockHttpServletRequestTests { request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); } - private void assertEqualEnumerations(Enumeration enum1, Enumeration enum2) { - assertNotNull(enum1); - assertNotNull(enum2); int count = 0; while (enum1.hasMoreElements()) { assertTrue("enumerations must be equal in length", enum2.hasMoreElements());