Search in sources :

Example 86 with ServletResponse

use of javax.servlet.ServletResponse in project guice by google.

the class FilterDefinitionTest method testGetFilterIfMatching.

public void testGetFilterIfMatching() throws ServletException {
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
    HttpServletRequest servletRequest = createMock(HttpServletRequest.class);
    ServletContext servletContext = createMock(ServletContext.class);
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    final MockFilter mockFilter = new MockFilter() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        //suppress rest of chain...
        }
    };
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    expect(servletRequest.getContextPath()).andReturn("/a_context_path");
    expect(servletRequest.getRequestURI()).andReturn("/a_context_path/test.html");
    replay(servletRequest, binding, injector);
    filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
    Filter filter = filterDef.getFilterIfMatching(servletRequest);
    assertSame(filter, mockFilter);
    verify(servletRequest, binding, injector);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain) ServletContext(javax.servlet.ServletContext)

Example 87 with ServletResponse

use of javax.servlet.ServletResponse in project guice by google.

the class FilterDefinitionTest method testFilterCreateDispatchDestroySupressChain.

public final void testFilterCreateDispatchDestroySupressChain() throws ServletException, IOException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    final MockFilter mockFilter = new MockFilter() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        //suppress rest of chain...
        }
    };
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    expect(request.getRequestURI()).andReturn("/index.html");
    expect(request.getContextPath()).andReturn("").anyTimes();
    replay(injector, binding, request);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), new HashMap<String, String>(), null);
    //should fire on mockfilter now
    filterDef.init(createMock(ServletContext.class), injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    assertTrue("init did not fire", mockFilter.isInit());
    Filter matchingFilter = filterDef.getFilterIfMatching(request);
    assertSame(mockFilter, matchingFilter);
    final boolean[] proceed = new boolean[1];
    matchingFilter.doFilter(request, null, new FilterChainInvocation(null, null, null) {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            proceed[0] = true;
        }
    });
    assertFalse("filter did not suppress chain", proceed[0]);
    filterDef.destroy(Sets.<Filter>newIdentityHashSet());
    assertTrue("destroy did not fire", mockFilter.isDestroy());
    verify(injector, request);
}
Also used : Binding(com.google.inject.Binding) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext)

Example 88 with ServletResponse

use of javax.servlet.ServletResponse in project guice by google.

the class ServletTest method testExistingRequestObject.

public void testExistingRequestObject() throws CreationException, IOException, ServletException {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    GuiceFilter filter = new GuiceFilter();
    final boolean[] invoked = new boolean[1];
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            InRequest inRequest = injector.getInstance(InRequest.class);
            assertSame(inRequest, injector.getInstance(InRequest.class));
            assertNull(injector.getInstance(IN_REQUEST_NULL_KEY));
            assertNull(injector.getInstance(IN_REQUEST_NULL_KEY));
        }
    };
    filter.doFilter(request, null, filterChain);
    assertTrue(invoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain)

Example 89 with ServletResponse

use of javax.servlet.ServletResponse in project guice by google.

the class ServletTest method testGuiceFilterConstructors.

public void testGuiceFilterConstructors() throws Exception {
    final RuntimeException servletException = new RuntimeException();
    final RuntimeException chainException = new RuntimeException();
    final Injector injector = createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            serve("/*").with(new HttpServlet() {

                @Override
                protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
                    throw servletException;
                }
            });
        }
    });
    final HttpServletRequest request = newFakeHttpServletRequest();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            throw chainException;
        }
    };
    try {
        new GuiceFilter().doFilter(request, null, filterChain);
        fail();
    } catch (RuntimeException e) {
        assertSame(servletException, e);
    }
    try {
        injector.getInstance(GuiceFilter.class).doFilter(request, null, filterChain);
        fail();
    } catch (RuntimeException e) {
        assertSame(servletException, e);
    }
    try {
        injector.getInstance(Key.get(GuiceFilter.class, ScopingOnly.class)).doFilter(request, null, filterChain);
        fail();
    } catch (RuntimeException e) {
        assertSame(chainException, e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) HttpServlet(javax.servlet.http.HttpServlet) FilterChain(javax.servlet.FilterChain) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 90 with ServletResponse

use of javax.servlet.ServletResponse in project guice by google.

the class ServletTest method testRequestAndResponseBindings.

public void testRequestAndResponseBindings() throws Exception {
    final Injector injector = createInjector();
    final HttpServletRequest request = newFakeHttpServletRequest();
    final HttpServletResponse response = newFakeHttpServletResponse();
    final boolean[] invoked = new boolean[1];
    GuiceFilter filter = new GuiceFilter();
    FilterChain filterChain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            invoked[0] = true;
            assertSame(request, servletRequest);
            assertSame(request, injector.getInstance(ServletRequest.class));
            assertSame(request, injector.getInstance(HTTP_REQ_KEY));
            assertSame(response, servletResponse);
            assertSame(response, injector.getInstance(ServletResponse.class));
            assertSame(response, injector.getInstance(HTTP_RESP_KEY));
            assertSame(servletRequest.getParameterMap(), injector.getInstance(REQ_PARAMS_KEY));
        }
    };
    filter.doFilter(request, response, filterChain);
    assertTrue(invoked[0]);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletTestUtils.newFakeHttpServletRequest(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) Injector(com.google.inject.Injector) FilterChain(javax.servlet.FilterChain) ServletTestUtils.newFakeHttpServletResponse(com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse)

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