Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BC introduced in 19188 #19194

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/filters/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private function insertResponseHeaderCollectionIntoData(Response $response, arra
return;
}

$all = $response->headers->toArray(true);
$all = $response->headers->toOriginalArray();
if (is_array($this->cacheHeaders)) {
$filtered = [];
foreach ($this->cacheHeaders as $name) {
Expand Down
17 changes: 11 additions & 6 deletions framework/web/HeaderCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,20 @@ public function removeAll()
* Returns the collection as a PHP array.
* @return array the array representation of the collection.
* The array keys are header names, and the array values are the corresponding header values.
* Since 2.0.45 you can pass true here to get the headers list of original header names (case-sensitive) instead of
* default normalized (case-insensitive) ones.
*/
public function toArray($originalNames = false)
public function toArray()
{
if ($originalNames === false) {
return $this->_headers;
}
return $this->_headers;
}

/**
* Returns the collection as a PHP array but instead of using normalized header names as keys (like [[toArray()]])
* it uses original header names (case-sensitive).
* @return array the array representation of the collection.
* @since 2.0.45
*/
public function toOriginalArray()
{
return \array_map(function ($normalizedName) {
return $this->_headers[$normalizedName];
}, \array_flip($this->_originalHeaderNames));
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/filters/PageCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function testCache($testCase)
}
// Headers
if (isset($testCase['headers'])) {
$headersExpected = Yii::$app->response->headers->toArray(true);
$headersExpected = Yii::$app->response->headers->toOriginalArray();
foreach ($testCase['headers'] as $name => $expected) {
$this->assertSame($expected, Yii::$app->response->headers->has($name), $testCase['name']);
if ($expected) {
Expand Down
8 changes: 4 additions & 4 deletions tests/framework/web/HeaderCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function testSetter()
$this->assertSame('1', $headerCollection->get('x-hEadER'));
$this->assertSame(['1'], $headerCollection->get('x-hEadER', null, false));
$this->assertSame(['x-header' => ['1']], $headerCollection->toArray());
$this->assertSame(['X-Header' => ['1']], $headerCollection->toArray(true));
$this->assertSame(['X-Header' => ['1']], $headerCollection->toOriginalArray());

$headerCollection->set('X-HEADER', '2');
$this->assertSame('2', $headerCollection->get('X-Header'));
$this->assertSame('2', $headerCollection->get('x-header'));
$this->assertSame('2', $headerCollection->get('x-hEadER'));
$this->assertSame(['x-header' => ['2']], $headerCollection->toArray());
$this->assertSame(['X-HEADER' => ['2']], $headerCollection->toArray(true));
$this->assertSame(['X-HEADER' => ['2']], $headerCollection->toOriginalArray());

$headerCollection->offsetSet('X-HEADER', '3');
$this->assertSame('3', $headerCollection->get('X-Header'));
Expand All @@ -77,15 +77,15 @@ public function testAdder()
$this->assertSame('1', $headerCollection->get('x-hEadER'));
$this->assertSame(['1'], $headerCollection->get('x-hEadER', null, false));
$this->assertSame(['x-header' => ['1']], $headerCollection->toArray());
$this->assertSame(['X-Header' => ['1']], $headerCollection->toArray(true));
$this->assertSame(['X-Header' => ['1']], $headerCollection->toOriginalArray());

$headerCollection->add('X-HEADER', '2');
$this->assertSame('1', $headerCollection->get('X-Header'));
$this->assertSame('1', $headerCollection->get('x-header'));
$this->assertSame('1', $headerCollection->get('x-hEadER'));
$this->assertSame(['1', '2'], $headerCollection->get('x-header', null, false));
$this->assertSame(['x-header' => ['1', '2']], $headerCollection->toArray());
$this->assertSame(['X-Header' => ['1', '2']], $headerCollection->toArray(true));
$this->assertSame(['X-Header' => ['1', '2']], $headerCollection->toOriginalArray());
}

public function testRemover()
Expand Down