ResultHandler { public void handleResult(Result result, Context context) { if (result == null || result instanceof AsyncResult) { return; } Object objectToBeRendered = result.getRenderable(); if (objectToBeRendered instanceof Renderable) { handleRenderable((Renderable) objectToBeRendered, context, result); } else { if (!result.getHeaders().containsKey(Result.CACHE_CONTROL)) { result.doNotCacheContent(); } if (objectToBeRendered instanceof NoHttpBody) { context.finalizeHeaders(result); } else { renderWithTemplateEngineOrRaw(context, result); } } } @Inject  ResultHandler(Logger logger, TemplateEngineManager templateEngineManager); void handleResult(Result result, Context context);  }
@Test public void testAddingOfDefaultHeadersWorks() { Result result = Results.json(); result.render(new Object()); assertNull(result.getHeaders().get(Result.CACHE_CONTROL)); assertNull(result.getHeaders().get(Result.DATE)); assertNull(result.getHeaders().get(Result.EXPIRES)); resultHandler.handleResult(result, context); assertEquals(Result.CACHE_CONTROL_DEFAULT_NOCACHE_VALUE, result .getHeaders().get(Result.CACHE_CONTROL)); assertNotNull(result.getHeaders().get(Result.DATE)); assertEquals(DateUtil.formatForHttpHeader(0L), result.getHeaders().get(Result.EXPIRES)); }
@Test public void testCacheControlDoesNotGetTouchedWhenSet() { Result result = Results.json(); result.addHeader(Result.CACHE_CONTROL, "must-revalidate"); result.render(new Object()); resultHandler.handleResult(result, context); assertEquals("must-revalidate", result.getHeaders().get(Result.CACHE_CONTROL)); assertNull(result.getHeaders().get(Result.DATE)); assertNull(result.getHeaders().get(Result.EXPIRES)); }
@Test public void testRenderPlainStringAndSetDefaultContentType() { final String toRender = "this is just a plain string"; Result result = Results.ok(); result.renderRaw(toRender); resultHandler.handleResult(result, context); assertEquals(Result.TEXT_PLAIN, result.getContentType()); }
@Test public void testContentNegotiation() { when(context.getAcceptContentType()).thenReturn("text/html"); Result result = Results.ok(); resultHandler.handleResult(result, context); assertEquals("text/html", result.getContentType()); verify(templateEngineHtml).invoke(context, result); }
@Test public void testRenderPlainStringLeavesExplicitlySetContentTypeUntouched() { final String toRender = "this is just a plain string"; final String contentType = "any/contenttype"; Result result = Results.ok(); result.contentType(contentType); result.renderRaw(toRender); resultHandler.handleResult(result, context); assertEquals(contentType, result.getContentType()); }
@Test public void testRenderPictureFromBytes() { final byte[] toRender = new byte[] { 1, 2, 3 }; final String contentType = "image/png"; Result result = Results.ok(); result.contentType(contentType); result.renderRaw(toRender); resultHandler.handleResult(result, context); assertEquals(contentType, result.getContentType()); }
@Test public void testThatNoHttpBodyWorks() { Result result = new Result(200); result.render(new NoHttpBody()); resultHandler.handleResult(result, context); verify(context).finalizeHeaders(result); }
@Test public void testThatFallbackContentTypeWorks() { Result result = new Result(200) .fallbackContentType(Result.TEXT_HTML) .contentType(null); resultHandler.handleResult(result, context); assertThat(result.getContentType(), equalTo(Result.TEXT_HTML)); }