Skip to content

Commit c9902db

Browse files
Pin that a report's body goes out as UTF-8 with its length in bytes (#50)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 990ba7b commit c9902db

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

‎src/test/java/software/stephenson/trace/TraceClientTest.java‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,83 @@ void report_sendsAUserAgentNamingTheClientAndTheApplication() throws Exception {
808808
client.close();
809809
}
810810

811+
/** What reached a server, as bytes, with the two headers that say how to read them. */
812+
private static final class RawRequest {
813+
final byte[] body;
814+
final String contentType;
815+
final String contentLength;
816+
817+
RawRequest(byte[] body, String contentType, String contentLength) {
818+
this.body = body;
819+
this.contentType = contentType;
820+
this.contentLength = contentLength;
821+
}
822+
}
823+
824+
/** Reports {@code name} with {@code tags} to a server of its own and returns the one request it received. */
825+
private static RawRequest reportAndCaptureRawRequest(String name, Map<String, String> tags) throws Exception {
826+
List<RawRequest> raw = new CopyOnWriteArrayList<>();
827+
CountDownLatch got = new CountDownLatch(1);
828+
HttpServer capturing = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
829+
capturing.createContext("/", exchange -> {
830+
raw.add(new RawRequest(readAll(exchange.getRequestBody()),
831+
exchange.getRequestHeaders().getFirst("Content-Type"),
832+
exchange.getRequestHeaders().getFirst("Content-Length")));
833+
exchange.sendResponseHeaders(201, -1);
834+
exchange.close();
835+
got.countDown();
836+
});
837+
capturing.start();
838+
try {
839+
TraceClient client = TraceClient.builder("http://127.0.0.1:" + capturing.getAddress().getPort(), "MyPlugin", "1.2.3")
840+
.key("k").build();
841+
client.report(name, null, tags);
842+
assertTrue(got.await(5, TimeUnit.SECONDS), "the report should reach the server");
843+
client.close();
844+
return raw.get(0);
845+
} finally {
846+
capturing.stop(0);
847+
}
848+
}
849+
850+
@Test
851+
void report_sendsNonAsciiTextAsUtf8WithALengthCountedInBytes() throws Exception {
852+
// Arrange
853+
// A two-byte, a three-byte and a four-byte (surrogate pair) character.
854+
// The body is streamed with a fixed length: counted in chars rather
855+
// than bytes, every such report would fail to send.
856+
Map<String, String> tags = Collections.singletonMap("place", "€ 🏠");
857+
String expected = "{\"application\":\"MyPlugin\",\"name\":\"démarrage\","
858+
+ "\"tags\":{\"place\":\"€ 🏠\",\"version\":\"1.2.3\"}}";
859+
860+
// Act
861+
RawRequest request = reportAndCaptureRawRequest("démarrage", tags);
862+
863+
// Assert
864+
assertArrayEquals(expected.getBytes(StandardCharsets.UTF_8), request.body, "the body is the JSON in UTF-8");
865+
assertTrue(request.body.length > expected.length(), "the multi-byte characters must have been sent as such");
866+
assertEquals(String.valueOf(request.body.length), request.contentLength, "the length is counted in bytes");
867+
assertEquals("application/json; charset=utf-8", request.contentType);
868+
}
869+
870+
@Test
871+
void report_aLoneSurrogateIsSentAsAQuestionMarkRatherThanDroppingTheReport() throws Exception {
872+
// Arrange
873+
// Half of a surrogate pair is not text UTF-8 can encode. quote()
874+
// passes it through, and String.getBytes(UTF_8) then writes its
875+
// replacement byte -- so the report still goes out, with '?' there.
876+
Map<String, String> tags = Collections.singletonMap("broken", "a\uD83Cb");
877+
878+
// Act
879+
RawRequest request = reportAndCaptureRawRequest("command", tags);
880+
881+
// Assert
882+
assertEquals("{\"application\":\"MyPlugin\",\"name\":\"command\","
883+
+ "\"tags\":{\"broken\":\"a?b\",\"version\":\"1.2.3\"}}",
884+
new String(request.body, StandardCharsets.UTF_8));
885+
assertEquals(String.valueOf(request.body.length), request.contentLength);
886+
}
887+
811888
@Test
812889
void report_logsASuccessStatusOtherThan201() throws Exception {
813890
// Arrange

0 commit comments

Comments
 (0)