Skip to content

Commit

Permalink
Make caseless remove robust (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Oct 4, 2021
1 parent 7e5aa3c commit c7b89ff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function caselessRemove(array $keys, array $data): array
}

foreach ($data as $k => $v) {
if (!in_array(strtolower($k), $keys)) {
if (!is_string($k) || !in_array(strtolower($k), $keys)) {
$result[$k] = $v;
}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,38 @@ public function testModifyServerRequestRetainsAttributes(): void

self::assertSame(['foo' => 'bar'], $modifiedRequest->getAttributes());
}

/**
* @return list<array{0: string[], 1: array, 2: array}>
*/
public function providesCaselessRemoveCases(): array
{
return [
[
['foo-bar'],
['Foo-Bar' => 'hello'],
[]
],
[
['foo-bar'],
['hello'],
['hello']
],
[
['foo-Bar'],
['Foo-Bar' => 'hello', 123 => '', 'Foo-BAR' => 'hello123', 'foobar' => 'baz'],
[123 => '', 'foobar' => 'baz'],
],
];
}

/**
* @dataProvider providesCaselessRemoveCases
*
* @param string[] $keys
*/
public function testCaselessRemove(array $keys, array $data, array $expected): void
{
self::assertSame($expected, Psr7\Utils::caselessRemove($keys, $data));
}
}

0 comments on commit c7b89ff

Please sign in to comment.