@Test public void removesContentLengthHeaderIfDecorating() throws Exception { response.header("Content-Length", 140); layout.handle(request, response); response.done(); assertNoExecutionError(); assertThat(response).hasNoHeader("Content-Length"); }
@Test public void leavesContentUntouchedIfNoDecorationOccurs() throws Exception { layout.connectTo((request, response) -> response.body("original content")); page.become("unselected"); layout.handle(request, response); response.done(); assertNoExecutionError(); assertThat(response).hasBodyText("original content"); }
@Test public void preservesOriginalResponseEncodingWhenDecorating() throws Exception { layout.connectTo((request, response) -> response.body("encoded content (éçëœ)")); response.contentType("text/html; charset=utf-8"); layout.handle(request, response); response.done(); assertNoExecutionError(); assertThat(response) .hasContentType("text/html; charset=utf-8") .hasBodyEncoding(UTF_8) .hasBodyText(containsString("éçëœ")); }
@Test public void logsEmptyStringWhenNoRefererInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"\" \"Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7)\"")); }
@Test public void logsEmptyStringWhenNoUserAgentInRequest() throws Exception { request .method(GET) .uri("/products?keyword=dogs") .addHeader(HeaderNames.REFERER, "http://lama/wool"); apacheCommonLogger.handle(request, response); response.status(OK).body("a response with a size of 28").done(); response.await(); logRecords.assertEntries( contains( "192.168.0.1 - - [27/Jun/2012:18:04:00 +0100] \"GET /products?keyword=dogs HTTP/1.1\" 200 28 \"http://lama/wool\" \"\"")); }
@Test public void runsContentThroughDecoratorWhenPageIsSelected() throws Exception { layout.handle(request, response); response.body("raw content").done(); assertNoExecutionError(); assertThat(response).hasBodyText("<decorated>raw content</decorated>"); }
public void handle(Request request, Response response) throws Exception { forward(request, response); String msg = String.format( COMMON_LOG_FORMAT, request.remoteIp(), "-", currentTime(), request.method(), request.uri(), request.protocol(), response.statusCode(), contentLengthOrHyphen(response)); logger.info(msg); }
private Object contentLengthOrHyphen(Response response) { return response.size() > 0 ? response.size() : "-"; }
public void pong(Request request, Response response) throws Exception { response.contentType(JSON).body(gson.toJson(new Pong())); }
private void assertNoExecutionError() throws ExecutionException, InterruptedException { response.await(); }