Search in sources :

Example 1 with RequestFilter

use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.

the class JDiscFilterForServletTest method bindings.

private Module bindings(BindingRepository<RequestFilter> requestFilters, BindingRepository<ResponseFilter> responseFilters) {
    return Modules.combine(new AbstractModule() {

        @Override
        protected void configure() {
            bind(FilterBindings.class).toInstance(new FilterBindings(requestFilters, responseFilters));
            bind(FilterInvoker.class).toInstance(new FilterInvoker() {

                @Override
                public HttpServletRequest invokeRequestFilterChain(RequestFilter requestFilter, URI uri, HttpServletRequest httpRequest, ResponseHandler responseHandler) {
                    TestRequestFilter filter = (TestRequestFilter) requestFilter;
                    filter.runAsSecurityFilter(httpRequest, responseHandler);
                    return httpRequest;
                }

                @Override
                public void invokeResponseFilterChain(ResponseFilter responseFilter, URI uri, HttpServletRequest request, HttpServletResponse response) {
                    TestResponseFilter filter = (TestResponseFilter) responseFilter;
                    filter.runAsSecurityFilter(request, response);
                }
            });
        }
    }, guiceModule());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) FilterInvoker(com.yahoo.jdisc.http.server.jetty.FilterInvoker) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterBindings(com.yahoo.jdisc.http.server.FilterBindings) URI(java.net.URI) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) AbstractModule(com.google.inject.AbstractModule)

Example 2 with RequestFilter

use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatResponseFilterIsRunOnBoundPath.

@Test
public void requireThatResponseFilterIsRunOnBoundPath() throws Exception {
    final ResponseFilter filter = mock(ResponseFilterMockBase.class);
    final BindingRepository<RequestFilter> requestFilters = null;
    final BindingRepository<ResponseFilter> responseFilters = new BindingRepository<>();
    responseFilters.bind("http://*/filtered/*", filter);
    final MyRequestHandler requestHandler = new MyRequestHandler();
    final TestDriver testDriver = newDriver(requestHandler, requestFilters, responseFilters);
    testDriver.client().get("/filtered/status.html");
    assertThat(requestHandler.awaitInvocation(), is(true));
    verify(filter, times(1)).filter(any(Response.class), any(Request.class));
    assertThat(testDriver.close(), is(true));
}
Also used : HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) BindingRepository(com.yahoo.jdisc.application.BindingRepository) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) HttpRequest(com.yahoo.jdisc.http.HttpRequest) Request(com.yahoo.jdisc.Request) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 3 with RequestFilter

use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatRequestFilterCanTerminateChain.

@Test
public void requireThatRequestFilterCanTerminateChain() throws Exception {
    final RequestFilter requestFilter1 = new RespondForbiddenFilter();
    final RequestFilter requestFilter2 = mock(RequestFilter.class);
    final RequestFilter requestFilterChain = RequestFilterChain.newInstance(requestFilter1, requestFilter2);
    final HttpRequest request = null;
    final ResponseHandler responseHandler = mock(ResponseHandler.class);
    when(responseHandler.handleResponse(any(Response.class))).thenReturn(mock(ContentChannel.class));
    requestFilterChain.filter(request, responseHandler);
    verify(requestFilter2, never()).filter(any(HttpRequest.class), any(ResponseHandler.class));
    final ArgumentCaptor<Response> responseCaptor = ArgumentCaptor.forClass(Response.class);
    verify(responseHandler).handleResponse(responseCaptor.capture());
    assertThat(responseCaptor.getValue().getStatus(), is(Response.Status.FORBIDDEN));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 4 with RequestFilter

use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatRequestFilterIsRunOnBoundPath.

@Test
public void requireThatRequestFilterIsRunOnBoundPath() throws Exception {
    final RequestFilter filter = mock(RequestFilterMockBase.class);
    final BindingRepository<RequestFilter> requestFilters = new BindingRepository<>();
    requestFilters.bind("http://*/filtered/*", filter);
    final BindingRepository<ResponseFilter> responseFilters = null;
    final MyRequestHandler requestHandler = new MyRequestHandler();
    final TestDriver testDriver = newDriver(requestHandler, requestFilters, responseFilters);
    testDriver.client().get("/filtered/status.html");
    assertThat(requestHandler.awaitInvocation(), is(true));
    verify(filter, times(1)).filter(any(HttpRequest.class), any(ResponseHandler.class));
    assertThat(testDriver.close(), is(true));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) BindingRepository(com.yahoo.jdisc.application.BindingRepository) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Example 5 with RequestFilter

use of com.yahoo.jdisc.http.filter.RequestFilter in project vespa by vespa-engine.

the class FilterTestCase method requireThatRequestFilterChainCallsFilterWithOriginalResponseHandler.

@Test
public void requireThatRequestFilterChainCallsFilterWithOriginalResponseHandler() throws Exception {
    final RequestFilter requestFilter = mock(RequestFilter.class);
    final RequestFilter requestFilterChain = RequestFilterChain.newInstance(requestFilter);
    final HttpRequest request = null;
    final ResponseHandler responseHandler = mock(ResponseHandler.class);
    requestFilterChain.filter(request, responseHandler);
    // Check that the filter is called with the same response handler argument as the chain was,
    // in a manner that allows the handler object to be wrapped.
    final ArgumentCaptor<ResponseHandler> responseHandlerCaptor = ArgumentCaptor.forClass(ResponseHandler.class);
    verify(requestFilter).filter(any(HttpRequest.class), responseHandlerCaptor.capture());
    verify(responseHandler, never()).handleResponse(any(Response.class));
    responseHandlerCaptor.getValue().handleResponse(mock(Response.class));
    verify(responseHandler, times(1)).handleResponse(any(Response.class));
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) HttpResponse(com.yahoo.jdisc.http.HttpResponse) Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) Test(org.testng.annotations.Test)

Aggregations

RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)16 Test (org.testng.annotations.Test)11 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)10 HttpRequest (com.yahoo.jdisc.http.HttpRequest)10 ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)10 BindingRepository (com.yahoo.jdisc.application.BindingRepository)6 Response (com.yahoo.jdisc.Response)5 HttpResponse (com.yahoo.jdisc.http.HttpResponse)5 Request (com.yahoo.jdisc.Request)3 ComponentId (com.yahoo.component.ComponentId)2 ComponentRegistry (com.yahoo.component.provider.ComponentRegistry)2 ChainsConfig (com.yahoo.container.core.ChainsConfig)2 FilterChainRepository (com.yahoo.container.http.filter.FilterChainRepository)2 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)2 ServerConfig (com.yahoo.jdisc.http.ServerConfig)2 SecurityRequestFilter (com.yahoo.jdisc.http.filter.SecurityRequestFilter)2 SecurityResponseFilter (com.yahoo.jdisc.http.filter.SecurityResponseFilter)2 FilterBindings (com.yahoo.jdisc.http.server.FilterBindings)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Test (org.junit.Test)2