use of com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doPutWithRedirectTest.
@Test
public void doPutWithRedirectTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/put-with-redirect-attributes";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.PUT.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
PutWithRedirectAttributesRequestHandler requestHandler = new PutWithRedirectAttributesRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.PUT, requestURI, false), requestHandler);
RequestInterceptor interceptor = mock(RequestInterceptor.class);
when(interceptor.preHandle(any(), any())).thenReturn(true);
componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).sendRedirect("/home");
verify(interceptor, times(1)).preHandle(any(), any());
verify(interceptor, times(1)).postHandle(any(), any());
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor in project ezyhttp by youngmonkeys.
the class ApplicationContextBuilder method registerComponents.
protected void registerComponents(EzyBeanContext beanContext) {
Set controllers = new HashSet<>();
controllers.addAll(beanContext.getSingletons(Controller.class));
controllers.addAll(beanContext.getSingletonsOf(IRequestController.class));
controllerManager.addControllers(controllers);
List exceptionHandlers = beanContext.getSingletons(ExceptionHandler.class);
exceptionHandlerManager.addExceptionHandlers(exceptionHandlers);
List<RequestInterceptor> requestInterceptors = beanContext.getSingletons(Interceptor.class);
requestInterceptors.sort(InterceptorAnnotations.comparator());
interceptorManager.addRequestInterceptors(requestInterceptors);
List bodyConverters = beanContext.getSingletons(BodyConvert.class);
dataConverters.addBodyConverters(bodyConverters);
List stringConverters = beanContext.getSingletons(StringConvert.class);
List uncaughtErrorHandlers = beanContext.getSingletonsOf(UnhandledErrorHandler.class);
List requestResponseWathcers = beanContext.getSingletonsOf(RequestResponseWatcher.class);
dataConverters.setStringConverters(stringConverters);
componentManager.setViewContext(buildViewContext(beanContext));
componentManager.setServerPort(getServerPort(beanContext));
componentManager.setExposeManagementURIs(isExposeManagementURIs(beanContext));
componentManager.setManagementPort(getManagementPort(beanContext));
componentManager.setAsyncDefaultTimeout(getAsyncDefaultTimeout(beanContext));
componentManager.setUnhandledErrorHandler(uncaughtErrorHandlers);
componentManager.addRequestResponseWatchers(requestResponseWathcers);
}
Aggregations