Skip to content

Commit

Permalink
Merge pull request #78 from l0gicgate/HeadersTestCoverage
Browse files Browse the repository at this point in the history
Ensure getHeader's default argument is validated and trimmed
  • Loading branch information
l0gicgate authored May 3, 2019
2 parents c51f3a1 + 7c266b6 commit 84c7a0a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"php-http/psr7-integration-tests": "dev-master",
"phpstan/phpstan": "^0.10",
"phpunit/phpunit": "^6.0|^7.0",
"squizlabs/php_codesniffer": "^3.3",
"ralouphie/getallheaders": "^2"
"ralouphie/getallheaders": "^2",
"squizlabs/php_codesniffer": "^3.3"
},
"provide": {
"psr/http-message-implementation": "1.0"
Expand Down
4 changes: 2 additions & 2 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function getHeader(string $name, $default = []): array
return count(array_keys($default)) ? $this->validateAndTrimHeader($name, $default) : $default;
}

if (is_string($default)) {
return [$default];
if (is_string($default) || is_numeric($default)) {
return $this->validateAndTrimHeader($name, $default);
}

throw new InvalidArgumentException('Default parameter of Headers::getHeader() must be a string or an array.');
Expand Down
20 changes: 20 additions & 0 deletions tests/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace Slim\Tests\Psr7;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Slim\Psr7\Headers;
use stdClass;

class HeadersTest extends TestCase
{
Expand Down Expand Up @@ -75,6 +77,24 @@ public function testGetHeader()
$this->assertEquals(['application/json', 'text/html'], $headers->getHeader('HTTP_ACCEPT'));
}

public function testGetHeaderReturnsValidatedAndTrimedHeaderDefaultValue()
{
$headers = new Headers([]);

$this->assertEquals(['application/json'], $headers->getHeader('accept', ' application/json'));
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Default parameter of Headers::getHeader() must be a string or an array.
*/
public function testGetHeaderThrowsExceptionWithInvalidDefaultArgument()
{
$headers = new Headers([]);

$headers->getHeader('accept', new stdClass());
}

public function testSetHeader()
{
$headers = new Headers([
Expand Down

0 comments on commit 84c7a0a

Please sign in to comment.