|
| 1 | +package datadog.trace.core.otlp.common; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | + |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.util.zip.GZIPInputStream; |
| 12 | +import okhttp3.MediaType; |
| 13 | +import okio.Buffer; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +class OtlpGrpcRequestBodyTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void contentTypeIsApplicationGrpc() { |
| 20 | + OtlpGrpcRequestBody body = |
| 21 | + new OtlpGrpcRequestBody( |
| 22 | + new OtlpPayload(ByteBuffer.wrap(new byte[] {1, 2, 3}), "application/grpc+proto"), |
| 23 | + false); |
| 24 | + |
| 25 | + assertEquals(MediaType.get("application/grpc"), body.contentType()); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void contentLengthIsHeaderPlusPayloadWhenUncompressed() { |
| 30 | + OtlpGrpcRequestBody body = |
| 31 | + new OtlpGrpcRequestBody( |
| 32 | + new OtlpPayload(ByteBuffer.wrap(new byte[] {1, 2, 3, 4}), "application/grpc+proto"), |
| 33 | + false); |
| 34 | + |
| 35 | + // 5-byte header (1 flag + 4 length) + 4-byte payload = 9 |
| 36 | + assertEquals(9, body.contentLength()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void contentLengthIsNegativeOneWhenGzipped() { |
| 41 | + OtlpGrpcRequestBody body = |
| 42 | + new OtlpGrpcRequestBody( |
| 43 | + new OtlpPayload(ByteBuffer.wrap(new byte[] {1, 2, 3, 4}), "application/grpc+proto"), |
| 44 | + true); |
| 45 | + |
| 46 | + assertEquals(-1, body.contentLength()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void writeToProducesGrpcFrameWithUncompressedFlagWhenNotGzipped() throws IOException { |
| 51 | + byte[] data = {10, 20, 30, 40, 50}; |
| 52 | + OtlpGrpcRequestBody body = |
| 53 | + new OtlpGrpcRequestBody( |
| 54 | + new OtlpPayload(ByteBuffer.wrap(data), "application/grpc+proto"), false); |
| 55 | + Buffer sink = new Buffer(); |
| 56 | + |
| 57 | + body.writeTo(sink); |
| 58 | + |
| 59 | + assertEquals(0, sink.readByte()); // uncompressed flag |
| 60 | + assertEquals(data.length, sink.readInt()); // 4-byte big-endian length |
| 61 | + assertArrayEquals(data, sink.readByteArray()); // payload |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void writeToProducesGrpcFrameWithCompressedFlagAndGzipDataWhenGzipped() throws IOException { |
| 66 | + byte[] data = "the quick brown fox jumps over the lazy dog".getBytes(); |
| 67 | + OtlpGrpcRequestBody body = |
| 68 | + new OtlpGrpcRequestBody( |
| 69 | + new OtlpPayload(ByteBuffer.wrap(data), "application/grpc+proto"), true); |
| 70 | + Buffer sink = new Buffer(); |
| 71 | + |
| 72 | + body.writeTo(sink); |
| 73 | + |
| 74 | + assertEquals(1, sink.readByte()); // compressed flag |
| 75 | + int gzipLength = sink.readInt(); // 4-byte big-endian gzip content length |
| 76 | + byte[] gzipped = sink.readByteArray(gzipLength); |
| 77 | + assertArrayEquals(data, gunzip(gzipped)); |
| 78 | + } |
| 79 | + |
| 80 | + private static byte[] gunzip(byte[] gzipped) throws IOException { |
| 81 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 82 | + try (InputStream gz = new GZIPInputStream(new ByteArrayInputStream(gzipped))) { |
| 83 | + byte[] buf = new byte[256]; |
| 84 | + int n; |
| 85 | + while ((n = gz.read(buf)) > 0) { |
| 86 | + out.write(buf, 0, n); |
| 87 | + } |
| 88 | + } |
| 89 | + return out.toByteArray(); |
| 90 | + } |
| 91 | +} |
0 commit comments