use of javax.servlet.http.HttpServletResponseWrapper in project jodd by oblac.
the class CharArrayResponseWrapperTest method testBytes.
@Test
public void testBytes() throws IOException {
HttpServletResponseWrapper rw = mock(HttpServletResponseWrapper.class);
ServletOutputStream os = mock(ServletOutputStream.class);
when(rw.getOutputStream()).thenReturn(os);
CharArrayResponseWrapper wrappedResponse = new CharArrayResponseWrapper(rw);
try {
wrappedResponse.getOutputStream();
Assert.fail();
} catch (IOException ignore) {
}
//sos.write(new byte[]{123, 123});
//char[] chars = wrappedResponse.toCharArray();
//assertNull(chars);
}
use of javax.servlet.http.HttpServletResponseWrapper in project roboguice by roboguice.
the class ServletTest method testRequestAndResponseBindings_matchesPassedParameters.
public void testRequestAndResponseBindings_matchesPassedParameters() throws Exception {
final int[] filterInvoked = new int[1];
final boolean[] servletInvoked = new boolean[1];
createInjector(new ServletModule() {
@Override
protected void configureServlets() {
final HttpServletRequest[] previousReq = new HttpServletRequest[1];
final HttpServletResponse[] previousResp = new HttpServletResponse[1];
final Provider<ServletRequest> servletReqProvider = getProvider(ServletRequest.class);
final Provider<HttpServletRequest> reqProvider = getProvider(HttpServletRequest.class);
final Provider<ServletResponse> servletRespProvider = getProvider(ServletResponse.class);
final Provider<HttpServletResponse> respProvider = getProvider(HttpServletResponse.class);
Filter filter = new Filter() {
public void init(FilterConfig filterConfig) {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
filterInvoked[0]++;
assertSame(req, servletReqProvider.get());
assertSame(req, reqProvider.get());
if (previousReq[0] != null) {
assertEquals(req, previousReq[0]);
}
assertSame(resp, servletRespProvider.get());
assertSame(resp, respProvider.get());
if (previousResp[0] != null) {
assertEquals(resp, previousResp[0]);
}
chain.doFilter(previousReq[0] = new HttpServletRequestWrapper((HttpServletRequest) req), previousResp[0] = new HttpServletResponseWrapper((HttpServletResponse) resp));
assertSame(req, reqProvider.get());
assertSame(resp, respProvider.get());
}
public void destroy() {
}
};
filter("/*").through(filter);
// filter twice to test wrapping in filters
filter("/*").through(filter);
serve("/*").with(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
servletInvoked[0] = true;
assertSame(req, servletReqProvider.get());
assertSame(req, reqProvider.get());
assertSame(resp, servletRespProvider.get());
assertSame(resp, respProvider.get());
}
});
}
});
GuiceFilter filter = new GuiceFilter();
filter.doFilter(newFakeHttpServletRequest(), newFakeHttpServletResponse(), new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
throw new IllegalStateException("Shouldn't get here");
}
});
assertEquals(2, filterInvoked[0]);
assertTrue(servletInvoked[0]);
}
use of javax.servlet.http.HttpServletResponseWrapper in project roboguice by roboguice.
the class ServletTest method testRequestAndResponseBindings_wrappingFilter.
public void testRequestAndResponseBindings_wrappingFilter() throws Exception {
final HttpServletRequest request = newFakeHttpServletRequest();
final ImmutableMap<String, String[]> wrappedParamMap = ImmutableMap.of("wrap", new String[] { "a", "b" });
final HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public Map getParameterMap() {
return wrappedParamMap;
}
@Override
public Object getAttribute(String attr) {
// Ensure that attributes are stored on the original request object.
throw new UnsupportedOperationException();
}
};
final HttpServletResponse response = newFakeHttpServletResponse();
final HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);
final boolean[] filterInvoked = new boolean[1];
final Injector injector = createInjector(new ServletModule() {
@Override
protected void configureServlets() {
filter("/*").through(new Filter() {
@Inject
Provider<ServletRequest> servletReqProvider;
@Inject
Provider<HttpServletRequest> reqProvider;
@Inject
Provider<ServletResponse> servletRespProvider;
@Inject
Provider<HttpServletResponse> respProvider;
public void init(FilterConfig filterConfig) {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
filterInvoked[0] = true;
assertSame(req, servletReqProvider.get());
assertSame(req, reqProvider.get());
assertSame(resp, servletRespProvider.get());
assertSame(resp, respProvider.get());
chain.doFilter(requestWrapper, responseWrapper);
assertSame(req, reqProvider.get());
assertSame(resp, respProvider.get());
}
public void destroy() {
}
});
}
});
GuiceFilter filter = new GuiceFilter();
final boolean[] chainInvoked = new boolean[1];
FilterChain filterChain = new FilterChain() {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
chainInvoked[0] = true;
assertSame(requestWrapper, servletRequest);
assertSame(requestWrapper, injector.getInstance(ServletRequest.class));
assertSame(requestWrapper, injector.getInstance(HTTP_REQ_KEY));
assertSame(responseWrapper, servletResponse);
assertSame(responseWrapper, injector.getInstance(ServletResponse.class));
assertSame(responseWrapper, injector.getInstance(HTTP_RESP_KEY));
assertSame(servletRequest.getParameterMap(), injector.getInstance(REQ_PARAMS_KEY));
InRequest inRequest = injector.getInstance(InRequest.class);
assertSame(inRequest, injector.getInstance(InRequest.class));
}
};
filter.doFilter(request, response, filterChain);
assertTrue(chainInvoked[0]);
assertTrue(filterInvoked[0]);
}
use of javax.servlet.http.HttpServletResponseWrapper in project spring-framework by spring-projects.
the class ServletWebRequestTests method decoratedNativeRequest.
@Test
public void decoratedNativeRequest() {
HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest);
HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse);
ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse);
assertSame(decoratedRequest, request.getNativeRequest());
assertSame(decoratedRequest, request.getNativeRequest(ServletRequest.class));
assertSame(decoratedRequest, request.getNativeRequest(HttpServletRequest.class));
assertSame(servletRequest, request.getNativeRequest(MockHttpServletRequest.class));
assertNull(request.getNativeRequest(MultipartRequest.class));
assertSame(decoratedResponse, request.getNativeResponse());
assertSame(decoratedResponse, request.getNativeResponse(ServletResponse.class));
assertSame(decoratedResponse, request.getNativeResponse(HttpServletResponse.class));
assertSame(servletResponse, request.getNativeResponse(MockHttpServletResponse.class));
assertNull(request.getNativeResponse(MultipartRequest.class));
}
use of javax.servlet.http.HttpServletResponseWrapper in project spring-security by spring-projects.
the class HttpSessionSecurityContextRepositoryTests method traverseWrappedRequests.
// SEC-2578
@Test
public void traverseWrappedRequests() {
HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response);
SecurityContext context = repo.loadContext(holder);
assertThat(request.getSession(false)).isNull();
// Simulate authentication during the request
context.setAuthentication(testToken);
repo.saveContext(context, new HttpServletRequestWrapper(holder.getRequest()), new HttpServletResponseWrapper(holder.getResponse()));
assertThat(request.getSession(false)).isNotNull();
assertThat(request.getSession().getAttribute(SPRING_SECURITY_CONTEXT_KEY)).isEqualTo(context);
}
Aggregations