Skip to content

Commit c269d27

Browse files
committed
Improve no content handling in MockHttpServletRequest
Issue: SPR-11764
1 parent 9706e5c commit c269d27

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,6 +104,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
104104

105105
private static final String CHARSET_PREFIX = "charset=";
106106

107+
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
108+
new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0]));
109+
107110

108111
private boolean active = true;
109112

@@ -375,7 +378,7 @@ public ServletInputStream getInputStream() {
375378
return new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
376379
}
377380
else {
378-
return null;
381+
return EMPTY_SERVLET_INPUT_STREAM;
379382
}
380383
}
381384

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.mock.web;
1818

19+
import java.io.IOException;
20+
import java.nio.charset.Charset;
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.Collections;
@@ -26,6 +28,7 @@
2628
import java.util.Map;
2729

2830
import org.junit.Test;
31+
import org.springframework.util.StreamUtils;
2932

3033
import static org.junit.Assert.*;
3134

@@ -42,6 +45,22 @@ public class MockHttpServletRequestTests {
4245
private MockHttpServletRequest request = new MockHttpServletRequest();
4346

4447

48+
@Test
49+
public void content() throws IOException {
50+
byte[] bytes = "body".getBytes(Charset.defaultCharset());
51+
request.setContent(bytes);
52+
assertEquals(bytes.length, request.getContentLength());
53+
assertNotNull(request.getInputStream());
54+
assertEquals("body", StreamUtils.copyToString(request.getInputStream(), Charset.defaultCharset()));
55+
}
56+
57+
@Test
58+
public void noContent() throws IOException {
59+
assertEquals(-1, request.getContentLength());
60+
assertNotNull(request.getInputStream());
61+
assertEquals(-1, request.getInputStream().read());
62+
}
63+
4564
@Test
4665
public void setContentType() {
4766
String contentType = "test/plain";

spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public class MockHttpServletRequest implements HttpServletRequest {
107107

108108
private static final String CHARSET_PREFIX = "charset=";
109109

110+
private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM =
111+
new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0]));
112+
110113

111114
private boolean active = true;
112115

@@ -378,7 +381,7 @@ public ServletInputStream getInputStream() {
378381
return new DelegatingServletInputStream(new ByteArrayInputStream(this.content));
379382
}
380383
else {
381-
return null;
384+
return EMPTY_SERVLET_INPUT_STREAM;
382385
}
383386
}
384387

0 commit comments

Comments
 (0)