Skip to content

Commit 06f2da1

Browse files
committed
refactor: uses new casing in header overrides
1 parent 1962c95 commit 06f2da1

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/Providers/Http/Collections/HeadersCollection.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,17 @@ private function set(string $name, $value): void
115115
}
116116
$lowerName = strtolower($name);
117117

118-
// If header exists with different casing, use the existing casing
118+
// If header exists with different casing, remove the old casing
119119
if (isset($this->headersMap[$lowerName])) {
120-
$actualName = $this->headersMap[$lowerName];
121-
$this->headers[$actualName] = $normalizedValues;
122-
} else {
123-
// New header, use provided casing
124-
$this->headers[$name] = $normalizedValues;
125-
$this->headersMap[$lowerName] = $name;
120+
$oldName = $this->headersMap[$lowerName];
121+
if ($oldName !== $name) {
122+
unset($this->headers[$oldName]);
123+
}
126124
}
125+
126+
// Always use the new casing
127+
$this->headers[$name] = $normalizedValues;
128+
$this->headersMap[$lowerName] = $name;
127129
}
128130

129131
/**

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ public function testCaseInsensitiveReplacement(): void
8383
{
8484
$headers = new HeadersCollection(['Content-Type' => 'application/json']);
8585

86-
// withHeader with different casing should replace existing
86+
// withHeader with different casing should replace existing with new casing
8787
$newHeaders = $headers->withHeader('content-type', 'text/html');
8888

8989
$this->assertEquals(['text/html'], $newHeaders->get('Content-Type'));
90+
$this->assertEquals(['text/html'], $newHeaders->get('content-type'));
9091

91-
// Original casing should be preserved
92+
// New casing should be used
9293
$all = $newHeaders->getAll();
93-
$this->assertArrayHasKey('Content-Type', $all);
94-
$this->assertArrayNotHasKey('content-type', $all);
94+
$this->assertArrayHasKey('content-type', $all);
95+
$this->assertArrayNotHasKey('Content-Type', $all);
9596
}
9697

9798
/**

0 commit comments

Comments
 (0)