@Test
  public void testDoFilterGzipResponseCompression() throws Exception {
    MockHttpServletRequest request =
        new MockHttpServletRequest(HttpMethod.POST.name(), "http://localhost:8080/gzip");
    request.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip");

    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain =
        new MockFilterChain(
            servlet,
            new GzipServletFilter(),
            new OncePerRequestFilter() {
              @Override
              protected void doFilterInternal(
                  HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                  throws ServletException, IOException {
                response.getOutputStream().write("Should be compressed".getBytes());
              }
            });
    filterChain.doFilter(request, response);

    ByteArrayOutputStream unzippedStream = new ByteArrayOutputStream();
    StreamUtils.copy(
        new GZIPInputStream(new ByteArrayInputStream(response.getContentAsByteArray())),
        unzippedStream);
    String unzipped = new String(unzippedStream.toByteArray());

    Assert.assertEquals(unzipped, "Should be compressed");
  }
 /** SPR-10414 */
 @Test
 @SuppressWarnings("deprecation")
 public void modifyStatusMessageAfterSendError() throws IOException {
   response.sendError(HttpServletResponse.SC_NOT_FOUND);
   response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Server Error");
   assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND);
 }
 @Test
 public void addDateHeader() {
   response.addDateHeader("Last-Modified", 1437472800000L);
   response.addDateHeader("Last-Modified", 1437472801000L);
   assertEquals("Tue, 21 Jul 2015 10:00:00 GMT", response.getHeaders("Last-Modified").get(0));
   assertEquals("Tue, 21 Jul 2015 10:00:01 GMT", response.getHeaders("Last-Modified").get(1));
 }
 @Test
 public void getDateHeader() {
   long time = 1437472800000L;
   response.setDateHeader("Last-Modified", time);
   assertEquals("Tue, 21 Jul 2015 10:00:00 GMT", response.getHeader("Last-Modified"));
   assertEquals(time, response.getDateHeader("Last-Modified"));
 }
 @Test
 public void setContentTypeUTF8() {
   String contentType = "test/plain;charset=UTF-8";
   response.setContentType(contentType);
   assertEquals("UTF-8", response.getCharacterEncoding());
   assertEquals(contentType, response.getContentType());
   assertEquals(contentType, response.getHeader("Content-Type"));
 }
  @Test
  public void redirect() throws Exception {
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.sendRedirect("/login");

    assertEquals(response.getStatus(), HttpServletResponse.SC_FOUND);
    assertEquals(response.getRedirect(), "/login");
  }
 @Test
 public void setCharacterEncodingThenContentType() {
   response.setCharacterEncoding("UTF-8");
   response.setContentType("test/plain");
   assertEquals("test/plain", response.getContentType());
   assertEquals("test/plain;charset=UTF-8", response.getHeader("Content-Type"));
   assertEquals("UTF-8", response.getCharacterEncoding());
 }
 @Test
 public void setContentType() {
   String contentType = "test/plain";
   response.setContentType(contentType);
   assertEquals(contentType, response.getContentType());
   assertEquals(contentType, response.getHeader("Content-Type"));
   assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
 }
 @Test
 public void sendRedirect() throws IOException {
   String redirectUrl = "/redirect";
   response.sendRedirect(redirectUrl);
   assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
   assertEquals(redirectUrl, response.getHeader("Location"));
   assertEquals(redirectUrl, response.getRedirectedUrl());
   assertTrue(response.isCommitted());
 }
 @Test
 public void httpHeaderNameCasingIsPreserved() throws Exception {
   final String headerName = "Header1";
   response.addHeader(headerName, "value1");
   Collection<String> responseHeaders = response.getHeaderNames();
   assertNotNull(responseHeaders);
   assertEquals(1, responseHeaders.size());
   assertEquals(
       "HTTP header casing not being preserved", headerName, responseHeaders.iterator().next());
 }
  /**
   * Retrieve the string with the serialized (JSON/XML) Object if have one as response.
   *
   * @return String with the object serialized
   */
  public String serializedResult() throws Exception {

    if ("application/xml".equals(response.getContentType())) {
      return response.getContentAsString();
    }

    if ("application/json".equals(response.getContentType())) {
      return response.getContentAsString();
    }

    return null;
  }
示例#12
0
 public void render(FacesContext context) throws FacesException {
   if (null != context) {
     ExternalContext extContext = context.getExternalContext();
     if (null != extContext) {
       Object response = extContext.getResponse();
       if (response instanceof MockHttpServletResponse) {
         ((MockHttpServletResponse) response).setStatus(HttpServletResponse.SC_OK);
       }
     }
   }
 }
 @Test
 public void servletOutputStreamCommittedWhenBufferSizeExceeded() throws IOException {
   assertFalse(response.isCommitted());
   response.getOutputStream().write('X');
   assertFalse(response.isCommitted());
   int size = response.getBufferSize();
   response.getOutputStream().write(new byte[size]);
   assertTrue(response.isCommitted());
   assertEquals(size + 1, response.getContentAsByteArray().length);
 }
  @Test
  public void contentTypeHeaderWithMoreComplexCharsetSyntax() {
    String contentType = "test/plain;charset=\"utf-8\";foo=\"charset=bar\";foocharset=bar;foo=bar";
    response.setHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals("UTF-8", response.getCharacterEncoding());

    response = new MockHttpServletResponse();
    response.addHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals("UTF-8", response.getCharacterEncoding());
  }
  @Test
  public void contentTypeHeaderUTF8() {
    String contentType = "test/plain;charset=UTF-8";
    response.setHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals("UTF-8", response.getCharacterEncoding());

    response = new MockHttpServletResponse();
    response.addHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals("UTF-8", response.getCharacterEncoding());
  }
  @Test
  public void contentTypeHeader() {
    String contentType = "test/plain";
    response.addHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());

    response = new MockHttpServletResponse();
    response.setHeader("Content-Type", contentType);
    assertEquals(contentType, response.getContentType());
    assertEquals(contentType, response.getHeader("Content-Type"));
    assertEquals(WebUtils.DEFAULT_CHARACTER_ENCODING, response.getCharacterEncoding());
  }
 @Test
 public void servletWriterCommittedWhenBufferSizeExceeded() throws IOException {
   assertFalse(response.isCommitted());
   response.getWriter().write("X");
   assertFalse(response.isCommitted());
   int size = response.getBufferSize();
   char[] data = new char[size];
   Arrays.fill(data, 'p');
   response.getWriter().write(data);
   assertTrue(response.isCommitted());
   assertEquals(size + 1, response.getContentAsByteArray().length);
 }
  @Test
  public void testDoFilterNoCompression() throws Exception {
    MockHttpServletRequest request =
        new MockHttpServletRequest(HttpMethod.POST.name(), "http://localhost:8080/gzip");

    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain =
        new MockFilterChain(
            servlet,
            new GzipServletFilter(),
            new OncePerRequestFilter() {
              @Override
              protected void doFilterInternal(
                  HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                  throws ServletException, IOException {
                response.getOutputStream().write("Should be compressed".getBytes());
              }
            });
    filterChain.doFilter(request, response);

    String unzipped = new String(response.getContentAsByteArray());
    Assert.assertEquals(unzipped, "Should be compressed");
  }
 @Test
 public void servletOutputStreamCommittedOnOutputStreamFlush() throws IOException {
   assertFalse(response.isCommitted());
   response.getOutputStream().write('X');
   assertFalse(response.isCommitted());
   response.getOutputStream().flush();
   assertTrue(response.isCommitted());
   assertEquals(1, response.getContentAsByteArray().length);
 }
 @Test
 public void servletWriterCommittedOnWriterFlush() throws IOException {
   assertFalse(response.isCommitted());
   response.getWriter().write("X");
   assertFalse(response.isCommitted());
   response.getWriter().flush();
   assertTrue(response.isCommitted());
   assertEquals(1, response.getContentAsByteArray().length);
 }
 @Test
 public void setDateHeader() {
   response.setDateHeader("Last-Modified", 1437472800000L);
   assertEquals("Tue, 21 Jul 2015 10:00:00 GMT", response.getHeader("Last-Modified"));
 }
 @Test
 public void contentLengthHeader() {
   response.addHeader("Content-Length", "66");
   assertEquals(66, response.getContentLength());
   assertEquals("66", response.getHeader("Content-Length"));
 }
 @Test
 public void servletWriterAutoFlushedForCharArray() throws IOException {
   response.getWriter().write("XY".toCharArray());
   assertEquals("XY", response.getContentAsString());
 }
 @Test
 public void locationHeaderUpdatesGetRedirectedUrl() {
   String redirectUrl = "/redirect";
   response.setHeader("Location", redirectUrl);
   assertEquals(redirectUrl, response.getRedirectedUrl());
 }
 @Test(expected = IllegalArgumentException.class)
 public void getInvalidDateHeader() {
   response.setHeader("Last-Modified", "invalid");
   assertEquals("invalid", response.getHeader("Last-Modified"));
   response.getDateHeader("Last-Modified");
 }
 /** SPR-10414 */
 @Test
 public void modifyStatusAfterSendError() throws IOException {
   response.sendError(HttpServletResponse.SC_NOT_FOUND);
   response.setStatus(HttpServletResponse.SC_OK);
   assertEquals(response.getStatus(), HttpServletResponse.SC_NOT_FOUND);
 }