use of javax.servlet.ServletResponse in project roboguice by roboguice.
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() {
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 roboguice by roboguice.
the class ServletTest method testNewRequestObject.
public void testNewRequestObject() 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() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
invoked[0] = true;
assertNotNull(injector.getInstance(InRequest.class));
assertNull(injector.getInstance(IN_REQUEST_NULL_KEY));
}
};
filter.doFilter(request, null, filterChain);
assertTrue(invoked[0]);
}
use of javax.servlet.ServletResponse in project roboguice by roboguice.
the class ServletTest method testHttpSessionIsSerializable.
public void testHttpSessionIsSerializable() throws Exception {
final Injector injector = createInjector();
final HttpServletRequest request = newFakeHttpServletRequest();
final HttpSession session = request.getSession();
GuiceFilter filter = new GuiceFilter();
final boolean[] invoked = new boolean[1];
FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
invoked[0] = true;
assertNotNull(injector.getInstance(InSession.class));
assertNull(injector.getInstance(IN_SESSION_NULL_KEY));
}
};
filter.doFilter(request, null, filterChain);
assertTrue(invoked[0]);
HttpSession deserializedSession = reserialize(session);
String inSessionKey = IN_SESSION_KEY.toString();
String inSessionNullKey = IN_SESSION_NULL_KEY.toString();
assertTrue(deserializedSession.getAttribute(inSessionKey) instanceof InSession);
assertEquals(NullObject.INSTANCE, deserializedSession.getAttribute(inSessionNullKey));
}
use of javax.servlet.ServletResponse in project spring-boot by spring-projects.
the class ErrorPageFilterTests method exceptionError.
@Test
public void exceptionError() throws Exception {
this.filter.addErrorPages(new ErrorPage(RuntimeException.class, "/500"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
super.doFilter(request, response);
throw new RuntimeException("BAD");
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(500);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
Map<String, Object> requestAttributes = getAttributesForDispatch("/500");
assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isEqualTo(RuntimeException.class);
assertThat(requestAttributes.get(RequestDispatcher.ERROR_EXCEPTION)).isInstanceOf(RuntimeException.class);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION_TYPE)).isNull();
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)).isNull();
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isEqualTo("/500");
}
use of javax.servlet.ServletResponse in project spring-boot by spring-projects.
the class ErrorPageFilterTests method statusError.
@Test
public void statusError() throws Exception {
this.filter.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
this.chain = new MockFilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
((HttpServletResponse) response).sendError(400, "BAD");
super.doFilter(request, response);
}
};
this.filter.doFilter(this.request, this.response, this.chain);
assertThat(((HttpServletResponseWrapper) this.chain.getResponse()).getStatus()).isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)).isEqualTo(400);
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_MESSAGE)).isEqualTo("BAD");
assertThat(this.request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)).isEqualTo("/test/path");
assertThat(this.response.isCommitted()).isTrue();
assertThat(this.response.getForwardedUrl()).isEqualTo("/400");
}
Aggregations