RequestHandlerImpl implements RequestHandler { @Override public void handleRequest(GenericContainerRequest request, GenericContainerResponse response) throws IOException { final ApplicationContext context = ApplicationContext.getCurrent(); try { for (RequestFilter filter : providers.getRequestFilters(context.getPath())) { filter.doFilter(request); } dispatcher.dispatch(request, response); setupInternalResponseHeaders(response.getStatus(), response.getHttpHeaders()); for (ResponseFilter filter : providers.getResponseFilters(context.getPath())) { filter.doFilter(response); } } catch (Exception e) { if (e instanceof WebApplicationException) { handleWebApplicationException((WebApplicationException)e, response); } else if (e instanceof InternalException) { handleInternalException((InternalException)e, response); } else { throw new UnhandledException(e); } } response.writeResponse(); } RequestHandlerImpl(RequestDispatcher dispatcher, ProviderBinder providers); @Override void handleRequest(GenericContainerRequest request, GenericContainerResponse response); @Override ProviderBinder getProviders(); @Override ResourceBinder getResources();  }
@Test public void executesRequestWithRequestDispatcher() throws Exception { requestHandler.handleRequest(request, response); verify(requestDispatcher).dispatch(request, response); }
@Test public void appliesRequestFilters() throws Exception { RequestFilter requestFilter = mock(RequestFilter.class); when(providers.getRequestFilters(requestPath)).thenReturn(newArrayList(requestFilter)); requestHandler.handleRequest(request, response); verify(requestFilter).doFilter(request); }
@Test public void appliesResponseFilters() throws Exception { ResponseFilter responseFilter = mock(ResponseFilter.class); when(providers.getResponseFilters(requestPath)).thenReturn(newArrayList(responseFilter)); requestHandler.handleRequest(request, response); verify(responseFilter).doFilter(response); }
@Test public void wrapsUnexpectedExceptionWithUnhandledException() throws Exception { RuntimeException exception = new RuntimeException(); doThrow(exception).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(exception)); requestHandler.handleRequest(request, response); }
@Test public void sendsResponseFromThrownWebApplicationExceptionWhenResponseHasEntity() throws Exception { WebApplicationException exception = new WebApplicationException(serverError().entity("Some Error").build()); doThrow(exception).when(requestDispatcher).dispatch(request, response); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals("Some Error", argumentCaptor.getValue().getEntity()); }
@Test public void throwsUnhandledExceptionWithCauseOfWebApplicationExceptionWhenJspErrorPageIsAvailableForErrorStatus() throws Exception { ErrorPages errorPages = mock(ErrorPages.class); when(errorPages.hasErrorPage(403)).thenReturn(true); when(environmentContext.get(ErrorPages.class)).thenReturn(errorPages); WebApplicationException webApplicationException = new WebApplicationException(403); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expect(unhandledExceptionWIthStatusMatcher(403)); requestHandler.handleRequest(request, response); }
@Test public void throwsUnhandledExceptionWithCauseOfWebApplicationExceptionWhenJspErrorPageForCauseIsAvailable() throws Exception { ErrorPages errorPages = mock(ErrorPages.class); Exception exception = new Exception(); when(errorPages.hasErrorPage(exception)).thenReturn(true); when(environmentContext.get(ErrorPages.class)).thenReturn(errorPages); WebApplicationException webApplicationException = new WebApplicationException(exception); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(exception)); requestHandler.handleRequest(request, response); }
@Test public void throwsUnhandledExceptionWithWebApplicationExceptionWhenJspErrorPageForWebApplicationExceptionIsAvailable() throws Exception { ErrorPages errorPages = mock(ErrorPages.class); WebApplicationException webApplicationException = new WebApplicationException(); when(errorPages.hasErrorPage(webApplicationException)).thenReturn(true); when(environmentContext.get(ErrorPages.class)).thenReturn(errorPages); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(webApplicationException)); requestHandler.handleRequest(request, response); }
@Test public void usesExceptionMapperToConvertWebApplicationExceptionToResponseWhenResponseFromWebApplicationExceptionDoesNotHaveEntity() throws Exception { WebApplicationException webApplicationException = new WebApplicationException(serverError().build()); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); ExceptionMapper<WebApplicationException> exceptionMapper = mock(ExceptionMapper.class); when(exceptionMapper.toResponse(webApplicationException)).thenReturn(serverError().entity("response from exception mapper").build()); when(providers.getExceptionMapper(WebApplicationException.class)).thenReturn(exceptionMapper); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals("response from exception mapper", argumentCaptor.getValue().getEntity()); }
@Test public void usesMessageFromCauseOfWebApplicationExceptionForResponseEntityWhenResponseFromWebApplicationExceptionDoesNotHaveEntity() throws Exception { Exception exception = new Exception("Error message"); WebApplicationException webApplicationException = new WebApplicationException(exception); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals("Error message", argumentCaptor.getValue().getEntity()); }
@Test public void usesMessageFromWebApplicationExceptionForResponseEntityWhenResponseFromWebApplicationExceptionDoesNotHaveEntity() throws Exception { WebApplicationException webApplicationException = new WebApplicationException("Error message"); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals("Error message", argumentCaptor.getValue().getEntity()); }
@Test public void usesToStringOfCauseOfWebApplicationExceptionForResponseEntityWhenResponseFromWebApplicationExceptionDoesNotHaveEntityAndCauseDoesNotHaveMessage() throws Exception { Exception exception = new Exception(); WebApplicationException webApplicationException = new WebApplicationException(exception); doThrow(webApplicationException).when(requestDispatcher).dispatch(request, response); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals(exception.toString(), argumentCaptor.getValue().getEntity()); }
@Test public void throwsUnhandledExceptionWithCauseOfInternalExceptionWhenJspErrorPageForCauseIsAvailable() throws Exception { ErrorPages errorPages = mock(ErrorPages.class); Exception exception = new Exception(); when(errorPages.hasErrorPage(exception)).thenReturn(true); when(environmentContext.get(ErrorPages.class)).thenReturn(errorPages); InternalException internalException = new InternalException(exception); doThrow(internalException).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(exception)); requestHandler.handleRequest(request, response); }
@Test public void throwsUnhandledExceptionWithInternalExceptionWhenJspErrorPageForInternalExceptionIsAvailable() throws Exception { ErrorPages errorPages = mock(ErrorPages.class); InternalException internalException = new InternalException(new Exception()); when(errorPages.hasErrorPage(internalException)).thenReturn(true); when(environmentContext.get(ErrorPages.class)).thenReturn(errorPages); doThrow(internalException).when(requestDispatcher).dispatch(request, response); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(internalException)); requestHandler.handleRequest(request, response); }
@Test public void usesExceptionMapperToConvertCauseOfInternalExceptionToResponse() throws Exception { Exception exception = new Exception(); InternalException internalException = new InternalException(exception); doThrow(internalException).when(requestDispatcher).dispatch(request, response); ExceptionMapper<Exception> exceptionMapper = mock(ExceptionMapper.class); when(exceptionMapper.toResponse(exception)).thenReturn(serverError().entity("response from exception mapper").build()); when(providers.getExceptionMapper(Exception.class)).thenReturn(exceptionMapper); requestHandler.handleRequest(request, response); ArgumentCaptor<Response> argumentCaptor = ArgumentCaptor.forClass(Response.class); verify(response).setResponse(argumentCaptor.capture()); assertEquals(INTERNAL_SERVER_ERROR, argumentCaptor.getValue().getStatusInfo()); assertEquals("response from exception mapper", argumentCaptor.getValue().getEntity()); }
@Test public void throwsUnhandledExceptionWithCauseOfInternalExceptionWhenExceptionMapperForCauseIsNotAvailable() throws Exception { Exception exception = new Exception(); InternalException internalException = new InternalException(exception); doThrow(internalException).when(requestDispatcher).dispatch(request, response); when(providers.getExceptionMapper(Exception.class)).thenReturn(null); thrown.expect(UnhandledException.class); thrown.expectCause(exceptionSameInstanceMatcher(exception)); requestHandler.handleRequest(request, response); }