InputStreamHttpServletRequestWrapper extends HttpServletRequestWrapper { @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body); return new ServletInputStream() { @Override public boolean isFinished() { return byteArrayInputStream.available() == 0; } @Override public boolean isReady() { return true; } @Override public void setReadListener(ReadListener readListener) { throw new UnsupportedOperationException("Not implemented"); } @Override public int read() throws IOException { return byteArrayInputStream.read(); } }; } InputStreamHttpServletRequestWrapper(HttpServletRequest request); @Override ServletInputStream getInputStream();  }
@Test public void testGetInputStreamContent() throws Exception { assertThat(IOUtils.toString(inputStreamHttpServletRequestWrapper.getInputStream()), is("test")); }
@Test(expected = NullPointerException.class) public void testGetInputStreamNullBody() throws IOException { when(httpServletRequest.getInputStream()).thenReturn(servletInputStream); stub(method(IOUtils.class, "toByteArray", InputStream.class)).toReturn(null); inputStreamHttpServletRequestWrapper = new InputStreamHttpServletRequestWrapper(httpServletRequest); inputStreamHttpServletRequestWrapper.getInputStream(); }
@Test(expected = IOException.class) public void testInputStreamIOException() throws IOException { when(httpServletRequest.getInputStream()).thenReturn(servletInputStream); stub(method(IOUtils.class, "toByteArray", InputStream.class)).toThrow(new IOException("some io exception")); inputStreamHttpServletRequestWrapper = new InputStreamHttpServletRequestWrapper(httpServletRequest); }
@Test public void testInputStreamIOExceptionDesc() throws Exception { when(httpServletRequest.getInputStream()).thenReturn(servletInputStream); stub(method(IOUtils.class, "toByteArray", InputStream.class)).toThrow(new IOException("some io exception")); try { inputStreamHttpServletRequestWrapper = new InputStreamHttpServletRequestWrapper(httpServletRequest); } catch (IOException e) { assertThat(e.getMessage(), is("some io exception")); } }
@Test public void testGetInputStreamIsReady() throws IOException { ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); assertThat(is.isReady(), is(true)); }
@Test(expected = RuntimeException.class) public void testGetInputStreamSetReadListener() throws IOException { ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); is.setReadListener(readListener); }
@Test public void testGetInputStreamSetReadListenerDetail() throws Exception { ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); try { is.setReadListener(readListener); } catch (RuntimeException e) { assertThat(e.getMessage(), is("Not implemented")); } }
@Test public void testGetInputStreamIsFinishedTrue() throws Exception { PowerMockito.whenNew(ByteArrayInputStream.class).withArguments(any()).thenReturn(byteArrayInputStream); when(byteArrayInputStream.available()).thenReturn(0); ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); assertThat(is.isFinished(), is(true)); }
@Test public void testGetInputStreamIsFinishedFalse() throws Exception { PowerMockito.whenNew(ByteArrayInputStream.class).withArguments(any()).thenReturn(byteArrayInputStream); when(byteArrayInputStream.available()).thenReturn(-1); ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); assertThat(is.isFinished(), is(false)); }
@Test public void testGetInputStreamRead() throws Exception { PowerMockito.whenNew(ByteArrayInputStream.class).withArguments(any()).thenReturn(byteArrayInputStream); when(byteArrayInputStream.read()).thenReturn(5); ServletInputStream is = inputStreamHttpServletRequestWrapper.getInputStream(); assertThat(is.read(), is(5)); }