Skip to content

Commit 086ec77

Browse files
authored
Add W3C Baggage boundary tests and entry-counter intent comment (#1976)
Follow-up to #1975. Pins the exact off-by-one behaviour of the W3C Baggage caps that the existing tests (1024 pairs / 500 pairs) only exercised well past the limits: - byte cap is inclusive: 8192 bytes accepted, 8193 bytes rejected - list-member cap: 180 entries accepted, 181st dropped Also documents that $entries counts only ACCEPTED pairs (incremented at the bottom of the loop), not raw comma-separated list-members, so a future change does not silently move the counter and alter semantics. Signed-off-by: tonghuaroot <tonghuaroot@gmail.com>
1 parent 73d88a0 commit 086ec77

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎src/API/Baggage/Propagation/Parser.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public function parseInto(BaggageBuilderInterface $baggageBuilder): void
3333
return;
3434
}
3535

36+
// Counts only ACCEPTED entries (incremented at the bottom of the loop), not raw
37+
// comma-separated list-members, so empty/invalid pairs do not consume the W3C cap.
3638
$entries = 0;
3739
foreach (explode(',', $this->baggageHeader) as $baggageString) {
3840
if ($entries >= self::MAX_BAGGAGE_ENTRIES) {

‎tests/Unit/API/Baggage/Propagation/ParserTest.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,75 @@ public function test_parse_into_caps_entries_at_w3c_list_member_limit(): void
138138

139139
$parser->parseInto($this->builder);
140140
}
141+
142+
public function test_parse_into_accepts_header_at_exactly_w3c_byte_limit(): void
143+
{
144+
// The byte cap is inclusive: strlen() > 8192 is rejected, so exactly 8192 is accepted.
145+
// Single valid pair padded to exactly 8192 bytes with a non-excluded value char.
146+
$prefix = 'key=';
147+
$header = $prefix . str_repeat('a', 8192 - strlen($prefix));
148+
$this->assertSame(8192, strlen($header));
149+
150+
$parser = new Parser($header);
151+
152+
$this->builder
153+
->expects($this->once())
154+
->method('set');
155+
156+
$parser->parseInto($this->builder);
157+
}
158+
159+
public function test_parse_into_rejects_header_one_byte_over_w3c_byte_limit(): void
160+
{
161+
// One byte past the inclusive cap (8193 > 8192) discards the entire header.
162+
$prefix = 'key=';
163+
$header = $prefix . str_repeat('a', 8193 - strlen($prefix));
164+
$this->assertSame(8193, strlen($header));
165+
166+
$parser = new Parser($header);
167+
168+
$this->builder
169+
->expects($this->never())
170+
->method('set');
171+
172+
$parser->parseInto($this->builder);
173+
}
174+
175+
public function test_parse_into_accepts_exactly_w3c_list_member_limit(): void
176+
{
177+
// 180 valid list-members are all accepted; the >= cap only trips on the 181st.
178+
$pairs = [];
179+
for ($i = 0; $i < 180; $i++) {
180+
$pairs[] = "k{$i}=v{$i}";
181+
}
182+
$header = implode(',', $pairs);
183+
$this->assertLessThanOrEqual(8192, strlen($header));
184+
185+
$parser = new Parser($header);
186+
187+
$this->builder
188+
->expects($this->exactly(180))
189+
->method('set');
190+
191+
$parser->parseInto($this->builder);
192+
}
193+
194+
public function test_parse_into_caps_at_w3c_list_member_limit_plus_one(): void
195+
{
196+
// 181 valid list-members: the 181st is dropped once $entries reaches 180.
197+
$pairs = [];
198+
for ($i = 0; $i < 181; $i++) {
199+
$pairs[] = "k{$i}=v{$i}";
200+
}
201+
$header = implode(',', $pairs);
202+
$this->assertLessThanOrEqual(8192, strlen($header));
203+
204+
$parser = new Parser($header);
205+
206+
$this->builder
207+
->expects($this->exactly(180))
208+
->method('set');
209+
210+
$parser->parseInto($this->builder);
211+
}
141212
}

0 commit comments

Comments
 (0)