@@ -649,21 +649,32 @@ private static String baggageHeader(int start, int count) {
649649 @ Test
650650 void extract_limit_maxBytes_exceedsLimit () {
651651 W3CBaggagePropagator propagator = W3CBaggagePropagator .getInstance ();
652- // Single header over 8192 bytes — should not be extracted
653- char [] chars = new char [8192 ];
654- Arrays .fill (chars , 'v' );
655- String header = "k=" + new String (chars );
652+ // Single header over 8192 bytes — truncated to the byte limit; the entry within budget is
653+ // extracted with a truncated value
654+ String header = "k=" + fillChars ('v' , 8192 ); // 8194 bytes; truncated to 8192 → k=<8190 v's>
656655 Context result = propagator .extract (Context .root (), ImmutableMap .of ("baggage" , header ), getter );
657- assertThat (Baggage .fromContext (result )).isEqualTo (Baggage .empty ());
656+ assertThat (Baggage .fromContext (result ).getEntryValue ("k" )).isEqualTo (fillChars ('v' , 8190 ));
657+ }
658+
659+ @ Test
660+ void extract_limit_maxBytes_partialHeader () {
661+ W3CBaggagePropagator propagator = W3CBaggagePropagator .getInstance ();
662+ // A header where the first entry is complete within the byte budget but the second entry's
663+ // key is cut off by the truncation — only the first entry is extracted.
664+ // "k1=" (3) + 8186 'v's + "," (1) + "k2=v2" (5) = 8195 bytes;
665+ // truncated to 8192 → "k1=<8186 v's>,k2" (k2's "=" is beyond the budget)
666+ String header = "k1=" + fillChars ('v' , 8186 ) + ",k2=v2" ;
667+ Context result = propagator .extract (Context .root (), ImmutableMap .of ("baggage" , header ), getter );
668+ Baggage baggage = Baggage .fromContext (result );
669+ assertThat (baggage .getEntryValue ("k1" )).isEqualTo (fillChars ('v' , 8186 ));
670+ assertThat (baggage .getEntryValue ("k2" )).isNull ();
658671 }
659672
660673 @ Test
661674 void extract_limit_maxBytes_acrossMultipleHeaders () {
662675 W3CBaggagePropagator propagator = W3CBaggagePropagator .getInstance ();
663676 // First header just under 8192 bytes is extracted; second header pushes total over the limit
664- char [] almostMaxChars = new char [8189 ];
665- Arrays .fill (almostMaxChars , 'v' );
666- String almostMax = "k=" + new String (almostMaxChars ); // "k=vvv..."
677+ String almostMax = "k=" + fillChars ('v' , 8189 ); // "k=vvv..."
667678 String second = "k2=v2" ;
668679 Context result =
669680 propagator .extract (
@@ -690,10 +701,7 @@ void inject_limit_maxEntries() {
690701 void inject_limit_maxBytes () {
691702 W3CBaggagePropagator propagator = W3CBaggagePropagator .getInstance ();
692703 // One entry whose encoded form alone exceeds the byte limit — should produce empty header
693- char [] longValueChars = new char [8192 ];
694- Arrays .fill (longValueChars , 'v' );
695- String longValue = new String (longValueChars );
696- Baggage baggage = Baggage .builder ().put ("k" , longValue ).build ();
704+ Baggage baggage = Baggage .builder ().put ("k" , fillChars ('v' , 8192 )).build ();
697705 Map <String , String > carrier = new HashMap <>();
698706 propagator .inject (Context .root ().with (baggage ), carrier , Map ::put );
699707 assertThat (carrier ).doesNotContainKey ("baggage" );
@@ -703,15 +711,19 @@ void inject_limit_maxBytes() {
703711 void inject_limit_maxBytes_metadata () {
704712 // Value alone fits easily (k=v is 3 bytes), but k=v;{metadata} exceeds 8192 bytes.
705713 // Verifies that metadata length is included in the byte limit check.
706- char [] metaChars = new char [8190 ];
707- Arrays .fill (metaChars , 'x' );
708714 Baggage baggage =
709- Baggage .builder ().put ("k" , "v" , BaggageEntryMetadata .create (new String ( metaChars ))).build ();
715+ Baggage .builder ().put ("k" , "v" , BaggageEntryMetadata .create (fillChars ( 'x' , 8190 ))).build ();
710716 Map <String , String > carrier = new HashMap <>();
711717 W3CBaggagePropagator .getInstance ().inject (Context .root ().with (baggage ), carrier , Map ::put );
712718 assertThat (carrier ).doesNotContainKey ("baggage" );
713719 }
714720
721+ private static String fillChars (char c , int count ) {
722+ char [] chars = new char [count ];
723+ Arrays .fill (chars , c );
724+ return new String (chars );
725+ }
726+
715727 @ Test
716728 void toString_Valid () {
717729 assertThat (W3CBaggagePropagator .getInstance ().toString ()).isEqualTo ("W3CBaggagePropagator" );
0 commit comments