Example usage for org.springframework.mock.web MockHttpServletResponse setWriterAccessAllowed

List of usage examples for org.springframework.mock.web MockHttpServletResponse setWriterAccessAllowed

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletResponse setWriterAccessAllowed.

Prototype

public void setWriterAccessAllowed(boolean writerAccessAllowed) 

Source Link

Document

Set whether #getWriter() access is allowed.

Usage

From source file:net.node42.filter.SimpleTimeoutLoadLimitingFilterTest.java

@Test
public void testTimeoutResponseReturned() throws Exception {
    final SimpleTimeoutLoadLimitingFilter target = new SimpleTimeoutLoadLimitingFilter();
    final MockFilterConfig filterConfig = new MockFilterConfig(new MockServletContext());
    // Nothing gets through, and it times out immediately...
    filterConfig.addInitParameter("request_count_concurrent_max", "0");
    filterConfig.addInitParameter("request_timeout", "-1000");
    target.init(filterConfig);/* w  ww.  j  a va2 s.co m*/

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setWriterAccessAllowed(true);
    final FilterChain filterChain = mock(FilterChain.class);

    runLoadLimitingFilter(target, request, response, filterChain);
    await().atMost(2, TimeUnit.SECONDS).untilAtomic(target.ACTIVE_REQUESTS, equalTo(0));
    assertThat(response.getContentAsString()).isNotNull().isNotEmpty()
            .isEqualTo(SimpleTimeoutLoadLimitingFilter.CONTENT_TIMEOUT_DEFAULT);
}

From source file:net.node42.filter.SimpleTimeoutLoadLimitingFilterTest.java

@Test
public void testConcurrentCountIncrementation() throws Exception {

    final SimpleTimeoutLoadLimitingFilter target = new SimpleTimeoutLoadLimitingFilter();
    final MockFilterConfig filterConfig = new MockFilterConfig(new MockServletContext());
    target.init(filterConfig);/*from  w ww  . ja v a2s.  com*/

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setWriterAccessAllowed(true);
    final FilterChain filterChain = mock(FilterChain.class);

    doAnswer(new Answer() {
        @Override
        public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(7000L);
            return null;
        }
    }).when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class));

    // Reusing the request/response object for multiple calls. Safe, but they are not viable to inspect for state.
    await().atMost(2, TimeUnit.SECONDS).untilAtomic(target.ACTIVE_REQUESTS, equalTo(0));
    runLoadLimitingFilter(target, request, response, filterChain);
    await().atMost(2, TimeUnit.SECONDS).untilAtomic(target.ACTIVE_REQUESTS, equalTo(1));
    runLoadLimitingFilter(target, request, response, filterChain);
    await().atMost(2, TimeUnit.SECONDS).untilAtomic(target.ACTIVE_REQUESTS, equalTo(2));

    target.destroy();
}

From source file:net.node42.filter.SimpleTimeoutLoadLimitingFilterTest.java

@Test
public void testDestroy() throws Exception {
    final SimpleTimeoutLoadLimitingFilter target = new SimpleTimeoutLoadLimitingFilter();
    final MockFilterConfig filterConfig = new MockFilterConfig(new MockServletContext());
    filterConfig.addInitParameter("request_count_concurrent_max", "0");
    filterConfig.addInitParameter("request_count_check_interval", "200");
    filterConfig.addInitParameter("request_timeout", "10000");

    target.init(filterConfig);//w  ww  .  j  a v  a  2s  . co  m
    assertThat(target.maxConcurrentRequests).isEqualTo(0);
    assertThat(target.requestCountCheckInterval).isEqualTo(200L);
    assertThat(target.requestTimeoutMillis).isEqualTo(10000);

    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    response.setWriterAccessAllowed(true);
    final FilterChain filterChain = mock(FilterChain.class);

    runLoadLimitingFilter(target, request, response, filterChain);
    assertThat(response.getContentLength()).isEqualTo(0); // Verify we haven't written a timeout message

    target.destroy();
    assertThat(target.DESTROY.get()).isEqualTo(true);
    await().atMost(5, TimeUnit.SECONDS).until(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            return !response.getContentAsString().isEmpty();
        }
    });
    assertThat(response.getContentAsString()).isNotEmpty()
            .isEqualTo(SimpleTimeoutLoadLimitingFilter.CONTENT_TIMEOUT_DEFAULT);
}