@Test public void testContentType() { final AbstractJacksonLayout layout = JsonLayout.createDefaultLayout(); assertEquals("application/json; charset=UTF-8", layout.getContentType()); }
@Test public void testDefaultCharset() { final AbstractJacksonLayout layout = JsonLayout.createDefaultLayout(); assertEquals(StandardCharsets.UTF_8, layout.getCharset()); }
@Test(expected = AppenderLoggingException.class) public void testAppendConnectError() throws Exception { final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .withIgnoreExceptions(false) .setUrl(new URL("http://localhost:"+(wireMockRule.port()+1)+"/test/log4j/")) .build(); appender.append(createLogEvent()); }
@Test(expected = AppenderLoggingException.class) public void testAppendError() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(FAILURE_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .withIgnoreExceptions(false) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .build(); appender.append(createLogEvent()); }
@Test public void testAppend() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Test public void testAppendMethodPut() throws Exception { wireMockRule.stubFor(put(urlEqualTo("/test/log4j/1234")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setMethod("PUT") .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/1234")) .build(); appender.append(createLogEvent()); wireMockRule.verify(putRequestedFor(urlEqualTo("/test/log4j/1234")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
.withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/"))
@Test public void testAppendHttps() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("https://localhost:" + wireMockRule.httpsPort() + "/test/log4j/")) .setSslConfiguration(SslConfiguration.createSSLConfiguration(null, KeyStoreConfiguration.createKeyStoreConfiguration(TestConstants.KEYSTORE_FILE, TestConstants.KEYSTORE_PWD(), TestConstants.KEYSTORE_TYPE, null), TrustStoreConfiguration.createKeyStoreConfiguration(TestConstants.TRUSTSTORE_FILE, TestConstants.TRUSTSTORE_PWD(), TestConstants.TRUSTSTORE_TYPE, null))) .setVerifyHostname(false) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }
@Test public void testAppendCustomHeader() throws Exception { wireMockRule.stubFor(post(urlEqualTo("/test/log4j/")) .willReturn(SUCCESS_RESPONSE)); final Appender appender = HttpAppender.newBuilder() .withName("Http") .withLayout(JsonLayout.createDefaultLayout()) .setConfiguration(ctx.getConfiguration()) .setUrl(new URL("http://localhost:" + wireMockRule.port() + "/test/log4j/")) .setHeaders(new Property[] { Property.createProperty("X-Test", "header value"), Property.createProperty("X-Runtime", "${java:runtime}") }) .build(); appender.append(createLogEvent()); wireMockRule.verify(postRequestedFor(urlEqualTo("/test/log4j/")) .withHeader("Host", containing("localhost")) .withHeader("X-Test", equalTo("header value")) .withHeader("X-Runtime", equalTo(JAVA_LOOKUP.getRuntime())) .withHeader("Content-Type", containing("application/json")) .withRequestBody(containing("\"message\" : \"" + LOG_MESSAGE + "\""))); }