Search in sources :

Example 26 with ServletResponse

use of javax.servlet.ServletResponse in project spring-framework by spring-projects.

the class OpenEntityManagerInViewTests method testOpenEntityManagerInViewFilterAsyncScenario.

@Test
public void testOpenEntityManagerInViewFilterAsyncScenario() throws Exception {
    given(manager.isOpen()).willReturn(true);
    final EntityManagerFactory factory2 = mock(EntityManagerFactory.class);
    final EntityManager manager2 = mock(EntityManager.class);
    given(factory2.createEntityManager()).willReturn(manager2);
    given(manager2.isOpen()).willReturn(true);
    MockServletContext sc = new MockServletContext();
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(sc);
    wac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", factory);
    wac.getDefaultListableBeanFactory().registerSingleton("myEntityManagerFactory", factory2);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
    filterConfig2.addInitParameter("entityManagerFactoryBeanName", "myEntityManagerFactory");
    final OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
    filter.init(filterConfig);
    final OpenEntityManagerInViewFilter filter2 = new OpenEntityManagerInViewFilter();
    filter2.init(filterConfig2);
    final AtomicInteger count = new AtomicInteger(0);
    final FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            assertTrue(TransactionSynchronizationManager.hasResource(factory));
            servletRequest.setAttribute("invoked", Boolean.TRUE);
            count.incrementAndGet();
        }
    };
    final AtomicInteger count2 = new AtomicInteger(0);
    final FilterChain filterChain2 = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertTrue(TransactionSynchronizationManager.hasResource(factory2));
            filter.doFilter(servletRequest, servletResponse, filterChain);
            count2.incrementAndGet();
        }
    };
    FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
    AsyncWebRequest asyncWebRequest = mock(AsyncWebRequest.class);
    given(asyncWebRequest.isAsyncStarted()).willReturn(true);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
    asyncManager.setTaskExecutor(new SyncTaskExecutor());
    asyncManager.setAsyncWebRequest(asyncWebRequest);
    asyncManager.startCallableProcessing(new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "anything";
        }
    });
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    filter2.doFilter(this.request, this.response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    assertEquals(1, count.get());
    assertEquals(1, count2.get());
    assertNotNull(request.getAttribute("invoked"));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).addTimeoutHandler(any(Runnable.class));
    verify(asyncWebRequest, times(2)).addCompletionHandler(any(Runnable.class));
    verify(asyncWebRequest).startAsync();
    // Async dispatch after concurrent handling produces result ...
    reset(asyncWebRequest);
    given(asyncWebRequest.isAsyncStarted()).willReturn(false);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    filter.doFilter(this.request, this.response, filterChain3);
    assertFalse(TransactionSynchronizationManager.hasResource(factory));
    assertFalse(TransactionSynchronizationManager.hasResource(factory2));
    assertEquals(2, count.get());
    assertEquals(2, count2.get());
    verify(this.manager).close();
    verify(manager2).close();
    wac.close();
}
Also used : WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) MockFilterConfig(org.springframework.mock.web.test.MockFilterConfig) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntityManagerFactory(javax.persistence.EntityManagerFactory) PassThroughFilterChain(org.springframework.mock.web.test.PassThroughFilterChain) Test(org.junit.Test)

Example 27 with ServletResponse

use of javax.servlet.ServletResponse in project spring-framework by spring-projects.

the class ConditionalDelegatingFilterProxyTests method assertFilterNotInvoked.

private void assertFilterNotInvoked(String requestUri, String pattern) throws Exception {
    request.setRequestURI(request.getContextPath() + requestUri);
    filter = new PatternMappingFilterProxy(delegate, pattern);
    filter.doFilter(request, response, filterChain);
    assertThat(delegate.request, equalTo((ServletRequest) null));
    assertThat(delegate.response, equalTo((ServletResponse) null));
    assertThat(delegate.chain, equalTo((FilterChain) null));
    assertThat(filterChain.getRequest(), equalTo((ServletRequest) request));
    assertThat(filterChain.getResponse(), equalTo((ServletResponse) response));
    filterChain = new MockFilterChain();
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain)

Example 28 with ServletResponse

use of javax.servlet.ServletResponse in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterHas500ResponseStatusWhenExceptionIsThrown.

@Test
@SuppressWarnings("unchecked")
public void filterHas500ResponseStatusWhenExceptionIsThrown() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    try {
        this.filter.doFilterInternal(request, response, new FilterChain() {

            @Override
            public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
                throw new RuntimeException();
            }
        });
        fail("Exception was swallowed");
    } catch (RuntimeException ex) {
        Map<String, Object> headers = (Map<String, Object>) this.repository.findAll().iterator().next().getInfo().get("headers");
        Map<String, Object> responseHeaders = (Map<String, Object>) headers.get("response");
        assertThat((String) responseHeaders.get("status")).isEqualTo("500");
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 29 with ServletResponse

use of javax.servlet.ServletResponse in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.addHeader("Authorization", "my-auth-header");
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(((Map<Object, Object>) headers.get("request"))).hasSize(0);
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 30 with ServletResponse

use of javax.servlet.ServletResponse in project spring-boot by spring-projects.

the class WebRequestTraceFilterTests method filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude.

@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude() throws ServletException, IOException {
    this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS));
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.addHeader("Content-Type", "application/json");
    this.filter.doFilterInternal(request, response, new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        }
    });
    Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
    Map<String, Object> headers = (Map<String, Object>) info.get("headers");
    assertThat(headers.get("response") == null).isTrue();
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) IOException(java.io.IOException) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ServletResponse (javax.servlet.ServletResponse)155 ServletRequest (javax.servlet.ServletRequest)129 HttpServletResponse (javax.servlet.http.HttpServletResponse)104 HttpServletRequest (javax.servlet.http.HttpServletRequest)90 FilterChain (javax.servlet.FilterChain)81 Test (org.junit.Test)76 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)26 Injector (com.google.inject.Injector)25 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 FilterConfig (javax.servlet.FilterConfig)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14