Skip to content

Commit 2d9d93e

Browse files
committed
fix: explodes set header strings
1 parent 720c294 commit 2d9d93e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Providers/Http/Collections/HeadersCollection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ public function has(string $name): bool
107107
*/
108108
private function set(string $name, $value): void
109109
{
110-
$normalizedValues = is_array($value) ? array_values($value) : [$value];
110+
if (is_array($value)) {
111+
$normalizedValues = array_values($value);
112+
} else {
113+
// Split comma-separated string into array
114+
$normalizedValues = array_map('trim', explode(',', $value));
115+
}
111116
$lowerName = strtolower($name);
112117

113118
// If header exists with different casing, use the existing casing

tests/unit/Providers/Http/Collections/HeadersCollectionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ public function testConstructorWithHeaders(): void
3030
$this->assertEquals(['value1', 'value2'], $headers->get('X-Custom'));
3131
}
3232

33+
/**
34+
* Tests constructor with comma-separated string values.
35+
*
36+
* @return void
37+
*/
38+
public function testConstructorWithCommaSeparatedString(): void
39+
{
40+
$headers = new HeadersCollection([
41+
'Accept' => 'application/json, text/html, application/xml',
42+
'Cache-Control' => 'no-cache, no-store',
43+
]);
44+
45+
$this->assertEquals(['application/json', 'text/html', 'application/xml'], $headers->get('Accept'));
46+
$this->assertEquals(['no-cache', 'no-store'], $headers->get('Cache-Control'));
47+
}
48+
3349
/**
3450
* Tests case-insensitive header access.
3551
*
@@ -126,4 +142,17 @@ public function testWithHeaderImmutability(): void
126142
$this->assertEquals(['text/html'], $new->get('Accept'));
127143
$this->assertEquals(['application/json'], $original->get('Content-Type'));
128144
}
145+
146+
/**
147+
* Tests withHeader with comma-separated string.
148+
*
149+
* @return void
150+
*/
151+
public function testWithHeaderCommaSeparatedString(): void
152+
{
153+
$headers = new HeadersCollection();
154+
$new = $headers->withHeader('Accept', 'application/json, text/html, application/xml');
155+
156+
$this->assertEquals(['application/json', 'text/html', 'application/xml'], $new->get('Accept'));
157+
}
129158
}

0 commit comments

Comments
 (0)