@Override public BufferedReader getReader() throws IOException { if (this.reader == null) { this.reader = new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding())); } return this.reader; }
@Override public BufferedReader getReader() throws IOException { if (this.reader == null) { this.reader = new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding())); } return this.reader; }
@Test public void cachedContentWithLimit() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals("Hello World".getBytes(CHARSET), response); assertArrayEquals("Hel".getBytes(CHARSET), wrapper.getContentAsByteArray()); }
@Test public void cachedContentWithOverflow() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3) { @Override protected void handleContentOverflow(int contentCacheLimit) { throw new IllegalStateException(String.valueOf(contentCacheLimit)); } }; try { FileCopyUtils.copyToByteArray(wrapper.getInputStream()); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { assertEquals("3", ex.getMessage()); } }
@Test public void cachedContent() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals(response, wrapper.getContentAsByteArray()); }
@Test public void requestParams() throws Exception { this.request.setMethod("POST"); this.request.setContentType(FORM_CONTENT_TYPE); this.request.setCharacterEncoding(CHARSET); this.request.setParameter("first", "value"); this.request.setParameter("second", "foo", "bar"); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); // getting request parameters will consume the request body assertFalse(wrapper.getParameterMap().isEmpty()); assertEquals("first=value&second=foo&second=bar", new String(wrapper.getContentAsByteArray())); // SPR-12810 : inputstream body should be consumed assertEquals("", new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))); }
@Test // SPR-12810 public void inputStreamFormPostRequest() throws Exception { this.request.setMethod("POST"); this.request.setContentType(FORM_CONTENT_TYPE); this.request.setCharacterEncoding(CHARSET); this.request.setParameter("first", "value"); this.request.setParameter("second", "foo", "bar"); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals(response, wrapper.getContentAsByteArray()); }
@Override public BufferedReader getReader() throws IOException { if (this.reader == null) { this.reader = new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding())); } return this.reader; }
@Override public BufferedReader getReader() throws IOException { if (this.reader == null) { this.reader = new BufferedReader(new InputStreamReader(getInputStream(), getCharacterEncoding())); } return this.reader; }