55 lines
2.1 KiB
Java
55 lines
2.1 KiB
Java
package com.baiyue.common.web;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
class TraceParentTest {
|
|
|
|
@Test
|
|
void parsesCanonicalVersion00AndCreatesLocalChild() {
|
|
TraceParent remote = TraceParent.parse(
|
|
"00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01").orElseThrow();
|
|
|
|
TraceParent local = TraceParent.childOf(remote);
|
|
|
|
assertThat(local.traceId()).isEqualTo(remote.traceId());
|
|
assertThat(local.parentId()).matches("[0-9a-f]{16}")
|
|
.isNotEqualTo(remote.parentId())
|
|
.isNotEqualTo("0000000000000000");
|
|
assertThat(local.traceFlags()).isEqualTo("01");
|
|
assertThat(local.value()).hasSize(TraceParent.HEADER_LENGTH);
|
|
}
|
|
|
|
@Test
|
|
void rejectsNonCanonicalUnsupportedAndZeroIdentifiers() {
|
|
assertThat(TraceParent.parse(
|
|
"00-4BF92F3577B34DA6A3CE929D0E0E4736-00f067aa0ba902b7-01")).isEmpty();
|
|
assertThat(TraceParent.parse(
|
|
"01-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01")).isEmpty();
|
|
assertThat(TraceParent.parse(
|
|
"00-00000000000000000000000000000000-00f067aa0ba902b7-01")).isEmpty();
|
|
assertThat(TraceParent.parse(
|
|
"00-4bf92f3577b34da6a3ce929d0e0e4736-0000000000000000-01")).isEmpty();
|
|
assertThat(TraceParent.parse("x".repeat(10_000))).isEmpty();
|
|
}
|
|
|
|
@Test
|
|
void createsCanonicalNonZeroRootAndNormalizesLegacyHex() {
|
|
TraceParent root = TraceParent.newRoot("ABCDEF0123456789ABCDEF0123456789");
|
|
|
|
assertThat(root.traceId()).isEqualTo("abcdef0123456789abcdef0123456789");
|
|
assertThat(root.parentId()).matches("[0-9a-f]{16}").isNotEqualTo("0000000000000000");
|
|
assertThat(root.value()).matches(
|
|
"00-abcdef0123456789abcdef0123456789-[0-9a-f]{16}-00");
|
|
}
|
|
|
|
@Test
|
|
void createsSampledOutboundChildForTrustedCurrentTrace() {
|
|
TraceParent child = TraceParent.outboundChild("4BF92F3577B34DA6A3CE929D0E0E4736");
|
|
|
|
assertThat(child.value()).matches(
|
|
"00-4bf92f3577b34da6a3ce929d0e0e4736-[0-9a-f]{16}-01");
|
|
}
|
|
}
|