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);
}
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);
}
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]);
}
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);
}
}
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]);
}
Aggregations