private HttpResponseSetting bindToSession(HttpServer server) { if (isMount()) { return server.mount(mount.getDir(), to(mount.getUri()), mount.getMountPredicates()); } if (isProxy()) { if (proxy.hasUrl()) { throw new IllegalArgumentException("It's not allowed to have URL in proxy from server"); } return server.proxy(proxy.getProxyConfig(), proxy.getFailover()); } if (isAnyResponse()) { return server.response(getResponseHandler()); } if (isRedirectResponse()) { return server.request(getRequestMatcher()).redirectTo(redirectTo); } return server.request(getRequestMatcher()).response(getResponseHandler()); }
@Test public void should_log_request_and_response_into_file() throws Exception { File file = folder.newFile(); HttpServer server = httpServer(port(), log(file.getAbsolutePath())); server.request(by("0XCAFE")).response("0XBABE"); running( server, new Runnable() { @Override public void run() throws Exception { assertThat(helper.postContent(root(), "0XCAFE"), is("0XBABE")); } }); String actual = Files.toString(file, Charset.defaultCharset()); assertThat(actual, containsString("0XCAFE")); assertThat(actual, containsString("0XCAFE")); }
@Test public void should_log_request_and_response_with_exception() throws Exception { File file = folder.newFile(); HttpServer server = httpServer(port(), log(file.getAbsolutePath())); ResponseHandler mock = mock(ResponseHandler.class); doThrow(RuntimeException.class).when(mock).writeToResponse(any(SessionContext.class)); server.request(by("0XCAFE")).response(mock); running( server, new Runnable() { @Override public void run() throws Exception { try { helper.postContent(root(), "0XCAFE"); } catch (IOException ignored) { } } }); String actual = Files.toString(file, Charset.defaultCharset()); assertThat(actual, containsString("RuntimeException")); }