AssetsController { public Result serveStatic() { Object renderable = new Renderable() { @Override public void render(Context context, Result result) { String fileName = getFileNameFromPathOrReturnRequestPath(context); URL url = getStaticFileFromAssetsDir(fileName); streamOutUrlEntity(url, context, result); } }; return Results.ok().render(renderable); } @Inject  AssetsController(AssetsControllerHelper assetsControllerHelper,
                            HttpCacheToolkit httpCacheToolkit,
                            MimeTypes mimeTypes,
                            NinjaProperties ninjaProperties); Result serveStatic(); Result serveWebJars(); final static String ASSETS_DIR; final static String FILENAME_PATH_PARAM; }
@Test public void testServeStatic404() throws Exception { when(contextRenderable.getRequestPath()).thenReturn("notAvailable"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); renderable.render(contextRenderable, result); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Results.notFound().getStatusCode(), resultCaptor.getValue().getStatusCode()); }
@Test public void testServeStaticSecurityClassesWithoutSlash() throws Exception { when(contextRenderable.getRequestPath()).thenReturn("ninja/Ninja.class"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); renderable.render(contextRenderable, result); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Results.notFound().getStatusCode(), resultCaptor.getValue().getStatusCode()); }
@Test public void testServeStaticSecurityClassesAbsolute() throws Exception { when(contextRenderable.getRequestPath()).thenReturn("/ninja/Ninja.class"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); renderable.render(contextRenderable, result); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Results.notFound().getStatusCode(), resultCaptor.getValue().getStatusCode()); }
@Test public void testServeStaticSecurityNoRelativPathWorks() throws Exception { when(contextRenderable.getRequestPath()).thenReturn("/assets/../../conf/heroku.conf"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); renderable.render(contextRenderable, result); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Results.notFound().getStatusCode(), resultCaptor.getValue().getStatusCode()); }
@Test public void testServeStaticDirectory() throws Exception { AssetsControllerHelper assetsControllerHelper = Mockito.mock(AssetsControllerHelper.class, Mockito.CALLS_REAL_METHODS); assetsController = new AssetsController( assetsControllerHelper, httpCacheToolkit, mimeTypes, ninjaProperties); when(contextRenderable.getRequestPath()).thenReturn("/"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); renderable.render(contextRenderable, result); verify(assetsControllerHelper).isDirectoryURL(this.getClass().getResource("/assets/")); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Results.notFound().getStatusCode(), resultCaptor.getValue().getStatusCode()); }
@Test public void testServeStatic304NotModified() throws Exception { when(contextRenderable.getRequestPath()).thenReturn( "/assets/testasset.txt"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); Result result = Results.ok(); result.status(Result.SC_304_NOT_MODIFIED); renderable.render(contextRenderable, result); verify(httpCacheToolkit).addEtag(Mockito.eq(contextRenderable), Mockito.eq(result), Mockito.anyLong()); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Result.SC_304_NOT_MODIFIED, resultCaptor.getValue() .getStatusCode()); }
@Test public void testStaticDirectoryIsFileSystemInDevMode() throws Exception { Mockito.when(ninjaProperties.isDev()).thenReturn(true); AssetsControllerHelper assetsControllerHelper = Mockito.mock(AssetsControllerHelper.class, Mockito.CALLS_REAL_METHODS); assetsController = new AssetsController( assetsControllerHelper, httpCacheToolkit, mimeTypes, ninjaProperties); when(contextRenderable.getRequestPath()).thenReturn( "/assets/testasset-not-existent.txt"); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); renderable.render(contextRenderable, Results.ok()); verify(assetsControllerHelper).normalizePathWithoutLeadingSlash("/assets/testasset-not-existent.txt", true); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(404, resultCaptor.getValue().getStatusCode()); }
@Test public void testStaticDirectoryClassPathWhenFileNotInFileSystemInDevMode() throws Exception { Mockito.when(ninjaProperties.isDev()).thenReturn(true); AssetsControllerHelper assetsControllerHelper = Mockito.mock(AssetsControllerHelper.class, Mockito.CALLS_REAL_METHODS); assetsController = new AssetsController( assetsControllerHelper, httpCacheToolkit, mimeTypes, ninjaProperties); when(contextRenderable.getRequestPath()).thenReturn( "/assets/testasset.txt"); when(contextRenderable.finalizeHeadersWithoutFlashAndSessionCookie(Mockito.any(Result.class))).thenReturn( responseStreams); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); when(responseStreams.getOutputStream()).thenReturn( byteArrayOutputStream); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); renderable.render(contextRenderable, Results.ok()); verify(assetsControllerHelper).normalizePathWithoutLeadingSlash("/assets/testasset.txt", true); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(200, resultCaptor.getValue().getStatusCode()); }
@Test public void testStaticDirectoryIsClassPathInProdMode() throws Exception { Mockito.when(ninjaProperties.isDev()).thenReturn(false); AssetsControllerHelper assetsControllerHelper = Mockito.mock(AssetsControllerHelper.class, Mockito.CALLS_REAL_METHODS); assetsController = new AssetsController( assetsControllerHelper, httpCacheToolkit, mimeTypes, ninjaProperties); when(contextRenderable.getRequestPath()).thenReturn( "/assets/testasset.txt"); when(contextRenderable.finalizeHeadersWithoutFlashAndSessionCookie(Mockito.any(Result.class))).thenReturn( responseStreams); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); when(responseStreams.getOutputStream()).thenReturn( byteArrayOutputStream); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); renderable.render(contextRenderable, Results.ok()); verify(assetsControllerHelper).normalizePathWithoutLeadingSlash("/assets/testasset.txt", true); }
@Test public void testServeStaticNormalOperationModifiedNoCaching() throws Exception { Result result = Results.ok(); when(contextRenderable.getRequestPath()).thenReturn( "/assets/testasset.txt"); when(mimeTypes.getContentType(Mockito.eq(contextRenderable), Mockito.anyString())).thenReturn("mimetype"); when(contextRenderable.finalizeHeadersWithoutFlashAndSessionCookie(Mockito.eq(result))).thenReturn( responseStreams); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); when(responseStreams.getOutputStream()).thenReturn( byteArrayOutputStream); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); renderable.render(contextRenderable, result); verify(httpCacheToolkit).addEtag(Mockito.eq(contextRenderable), Mockito.eq(result), Mockito.anyLong()); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Result.SC_200_OK, resultCaptor.getValue().getStatusCode()); assertEquals("mimetype", result.getContentType()); assertEquals("testasset", byteArrayOutputStream.toString()); }
@Test public void testServeStaticRobotsTxt() throws Exception { Result result = Results.ok(); when(contextRenderable.getRequestPath()).thenReturn( "/robots.txt"); when(mimeTypes.getContentType(Mockito.eq(contextRenderable), Mockito.anyString())).thenReturn("mimetype"); when(contextRenderable.finalizeHeadersWithoutFlashAndSessionCookie(Mockito.eq(result))).thenReturn( responseStreams); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); when(responseStreams.getOutputStream()).thenReturn( byteArrayOutputStream); Result result2 = assetsController.serveStatic(); Renderable renderable = (Renderable) result2.getRenderable(); renderable.render(contextRenderable, result); verify(httpCacheToolkit).addEtag(Mockito.eq(contextRenderable), Mockito.eq(result), Mockito.anyLong()); verify(contextRenderable).finalizeHeadersWithoutFlashAndSessionCookie(resultCaptor.capture()); assertEquals(Result.SC_200_OK, resultCaptor.getValue().getStatusCode()); assertEquals("mimetype", result.getContentType()); String content = byteArrayOutputStream.toString(); assertThat(content, containsString("User-agent: *")); assertThat(content, containsString("Disallow: /")); }