Skip to content

Commit

Permalink
check null before use this.content
Browse files Browse the repository at this point in the history
Resolves #1192
Resolves #1193
  • Loading branch information
npeters authored and olegz committed Oct 24, 2024
1 parent 3930cad commit 5f767f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class ServerlessHttpServletRequest implements HttpServletRequest {

private static final BufferedReader EMPTY_BUFFERED_READER = new BufferedReader(new StringReader(""));

private static final InputStream EMPTY_INPUT_STREAM = new ByteArrayInputStream(new byte[0]);
/**
* Date formats as specified in the HTTP RFC.
*
Expand Down Expand Up @@ -283,7 +284,15 @@ public String getContentType() {

@Override
public ServletInputStream getInputStream() {
InputStream stream = new ByteArrayInputStream(this.content);

InputStream stream;
if (this.content == null) {
stream = EMPTY_INPUT_STREAM;
}
else {
stream = new ByteArrayInputStream(this.content);
}

return new ServletInputStream() {

boolean finished = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ public void validatePostWithBody() throws Exception {
assertThat(pet.getName()).isNotEmpty();
}

@Test
public void validatePostWithoutBody() throws Exception {
ServerlessHttpServletRequest request = new ServerlessHttpServletRequest(null, "POST", "/pets/");
request.setContentType("application/json");
ServerlessHttpServletResponse response = new ServerlessHttpServletResponse();
try {
mvc.service(request, response);
}
catch (jakarta.servlet.ServletException e) {
assertThat(e.getCause()).isNotInstanceOf(NullPointerException.class);
}

assertThat(response.getStatus()).isEqualTo(400); // application fail because the pet is empty ;)
}

@Test
public void validatePostAsyncWithBody() throws Exception {
// System.setProperty("spring.main.banner-mode", "off");
Expand Down

0 comments on commit 5f767f6

Please sign in to comment.