@Test(expected = IOException.class) public void testDispatchIncludeHasNoValidResource() throws Exception { when(mockRequest.getRequestDispatcher(Mockito.anyString())).thenReturn(mockDispatcher); doAnswer( new Answer<Void>() { public Void answer(final InvocationOnMock invocation) throws Throwable { throw new IOException("Include doesn't work... nothing found"); } }) .when(mockDispatcher) .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class)); victim.getInputStream(mockRequest, mockResponse, "/static/*.js"); }
@Test public void shouldReturnsResourceIncludedByDispatcher() throws Exception { final String content = "SomeNonEmptyContent"; when(mockRequest.getRequestDispatcher(Mockito.anyString())).thenReturn(mockDispatcher); doAnswer( new Answer<Void>() { public Void answer(final InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; response.getOutputStream().write(content.getBytes()); return null; } }) .when(mockDispatcher) .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class)); assertEquals( content, IOUtils.toString(victim.getInputStream(mockRequest, mockResponse, "/static/*.js"))); verify(mockUriLocator, never()).locate(Mockito.anyString()); }
@Test public void shouldFallbackToExternalResourceLocatorWhenDispatcherReturns404() throws Exception { victim = new DispatcherStreamLocator() { @Override UriLocator createExternalResourceLocator() { return mockUriLocator; } }; final String location = "/some/location.js"; when(mockRequest.getRequestDispatcher(location)).thenReturn(mockDispatcher); Mockito.doAnswer( new Answer<Void>() { public Void answer(final InvocationOnMock invocation) throws Throwable { // simulate the dispatched response is empty return null; } }) .when(mockDispatcher) .include(Mockito.any(HttpServletRequest.class), Mockito.any(HttpServletResponse.class)); victim.getInputStream(mockRequest, mockResponse, location); verify(mockUriLocator).locate(Mockito.anyString()); }
@Test public void canLocateValidResource() throws Exception { when(mockRequest.getRequestURL()).thenReturn(new StringBuffer("")); assertNotNull(victim.getInputStream(mockRequest, mockResponse, "http://www.google.com")); }
@Test(expected = IOException.class) public void cannotLocateInvalidLocation() throws Exception { victim.getInputStream(mockRequest, mockResponse, "/INVALID"); }
@Test(expected = IOException.class) public void cannotLocateNullLocation() throws Exception { victim.getInputStream(mockRequest, mockResponse, null); }
@Test(expected = NullPointerException.class) public void shouldNotAcceptNullRequestOrResponse() throws Exception { victim.getInputStream(null, null, null); }