From 97e7252d44af3aa320a0080f9f3f9d457a1e7f86 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sat, 17 Oct 2026 02:42:16 -0600 Subject: [PATCH] Cover send() with a non-http base URL and a reply that has a body send() catches RuntimeException because a misconfigured URL surfaces as one, but no test drove that path: a file: base URL opens a connection that is not an HttpURLConnection, and "not a url" is a MalformedURLException. Both are now pinned as a FINE "could not deliver" line with the client still enabled. A second test answers 500 and then 201 with bodies larger than drain()'s buffer, and checks the second report still goes out and only the 500 is logged. Co-Authored-By: Claude Opus 5.5 --- .../stephenson/trace/TraceClientTest.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/src/test/java/software/stephenson/trace/TraceClientTest.java b/src/test/java/software/stephenson/trace/TraceClientTest.java index 88f837c..c678ed7 100644 --- a/src/test/java/software/stephenson/trace/TraceClientTest.java +++ b/src/test/java/software/stephenson/trace/TraceClientTest.java @@ -615,6 +615,82 @@ void report_logsASuccessStatusOtherThan201() throws Exception { client.close(); } + @Test + void report_aBaseUrlThatIsNotHttpIsLoggedFineAndNeverThrows() throws Exception { + // Arrange + // build() takes the base URL as a string and never parses it, so a + // typo only shows up in send(): "no protocol" is an IOException, and a + // file: URL opens a connection that is not an HttpURLConnection -- a + // ClassCastException, which is why send() catches RuntimeException too. + String[][] cases = { + {"not a url", "java.net.MalformedURLException"}, + {"file:///trace-client-test", "java.lang.ClassCastException"}, + }; + for (String[] misconfigured : cases) { + RecordingHandler log = new RecordingHandler(); + Logger logger = Logger.getLogger("TraceClientTest.misconfigured." + misconfigured[1]); + logger.setLevel(Level.ALL); + logger.addHandler(log); + TraceClient client = assertDoesNotThrow(() -> + TraceClient.builder(misconfigured[0], "MyPlugin", "1.2.3").key("k").logger(logger).build()); + assertTrue(client.isEnabled(), "a bad URL is not an opt-out"); + + // Act + assertDoesNotThrow(() -> client.report("startup")); + client.close(); + + // Assert + assertTrue(log.await(5, TimeUnit.SECONDS), misconfigured[0] + " should be mentioned at FINE"); + LogRecord record = log.records.get(0); + assertEquals(Level.FINE, record.getLevel()); + assertTrue(record.getMessage().startsWith("[trace] could not deliver {\"application\":\"MyPlugin\""), + record.getMessage()); + assertTrue(record.getMessage().contains(misconfigured[1]), record.getMessage()); + } + } + + @Test + void report_drainsAResponseBodyAndKeepsSendingAfterAnError() throws Exception { + // Arrange + // Unlike the shared server, this one answers with a body: an error page + // first, then a 201 with content. send() reads either to its end and + // the next report still goes out. + List bodies = new CopyOnWriteArrayList<>(); + CountDownLatch both = new CountDownLatch(2); + HttpServer chatty = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); + chatty.createContext("/", exchange -> { + bodies.add(new String(readAll(exchange.getRequestBody()), StandardCharsets.UTF_8)); + byte[] reply = new byte[4096]; // more than drain()'s 512-byte buffer + Arrays.fill(reply, (byte) 'x'); + exchange.sendResponseHeaders(bodies.size() == 1 ? 500 : 201, reply.length); + exchange.getResponseBody().write(reply); + exchange.close(); + both.countDown(); + }); + chatty.start(); + RecordingHandler log = new RecordingHandler(); + Logger logger = Logger.getLogger("TraceClientTest.chatty"); + logger.setLevel(Level.ALL); + logger.addHandler(log); + TraceClient client = TraceClient.builder("http://127.0.0.1:" + chatty.getAddress().getPort(), "MyPlugin", "1.2.3") + .key("k").logger(logger).build(); + + // Act + client.report("first"); + client.report("second"); + client.close(); + + // Assert + assertTrue(both.await(5, TimeUnit.SECONDS), "both reports should reach the server"); + assertEquals(2, bodies.size()); + assertTrue(bodies.get(1).contains("\"name\":\"second\""), bodies.get(1)); + assertEquals(1, log.records.size(), "only the 500 is worth a line: " + log.records); + assertEquals("[trace] trace server answered 500 for " + + "{\"application\":\"MyPlugin\",\"name\":\"first\",\"tags\":{\"version\":\"1.2.3\"}}", + log.records.get(0).getMessage()); + chatty.stop(0); + } + @Test void builder_trimsTheBaseUrlAndApplicationAndDropsEveryTrailingSlash() throws Exception { // Arrange