Skip to content

Commit

Permalink
more tests yiisoft#119
Browse files Browse the repository at this point in the history
  • Loading branch information
Somogyi Márton committed Nov 24, 2019
1 parent 3d8f1c7 commit 8afc22a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Middleware/TrustedHostsNetworkResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public function withAddedTrustedHosts(

throw new \InvalidArgumentException("Not supported IP header type: $type");
}
if (count($hosts) === 0) {
throw new \InvalidArgumentException("Empty hosts not allowed");
}
$data = [
self::DATA_KEY_HOSTS => $hosts,
self::DATA_KEY_IP_HEADERS => $ipHeaders,
Expand Down
48 changes: 47 additions & 1 deletion tests/Middleware/TrustedHostsNetworkResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public function testNotTrustedMiddleware(): void

$middleware = new TrustedHostsNetworkResolver(new Psr17Factory());
$content = 'Another branch.';
$middleware = $middleware->withNotTrustedBranch(new class($content) implements MiddlewareInterface {
$middleware = $middleware->withNotTrustedBranch(new class($content) implements MiddlewareInterface
{
private $content;

public function __construct(string $content)
Expand All @@ -188,4 +189,49 @@ public function process(
$body->rewind();
$this->assertSame($content, $body->getContents());
}

public function addedTrustedHostsInvalidParameterDataProvider(): array
{
return [
'hostsEmpty' => ['hosts' => []],
'hostsEmptyString' => ['hosts' => ['']],
'hostsNumeric' => ['hosts' => [888]],
'hostsSpaces' => ['hosts' => [' ']],
'hostsNotDomain' => ['host' => ['-apple']],
'urlHeadersEmpty' => ['urlHeaders' => ['']],
'urlHeadersNumeric' => ['urlHeaders' => [888]],
'urlHeadersSpaces' => ['urlHeaders' => [' ']],
'trustedHeadersEmpty' => ['trustedHeaders' => ['']],
'trustedHeadersNumeric' => ['trustedHeaders' => [888]],
'trustedHeadersSpaces' => ['trustedHeaders' => [' ']],
'protocolHeadersNumeric' => ['protocolHeaders' => ['http' => 888]],
'ipHeadersEmptyString' => ['ipHeaders' => [' ']],
'ipHeadersNumeric' => ['ipHeaders' => [888]],
'ipHeadersInvalidType' => ['ipHeaders' => [['---', 'aaa']]],
'ipHeadersInvalidTypeValue' => [
'ipHeaders' => [
[
TrustedHostsNetworkResolver::IP_HEADER_TYPE_RFC7239,
888
]
]
],
];
}

/**
* @dataProvider addedTrustedHostsInvalidParameterDataProvider
*/
public function testAddedTrustedHostsInvalidParameter(array $data): void
{
$this->expectException(\InvalidArgumentException::class);
(new TrustedHostsNetworkResolver(new Psr17Factory()))
->withAddedTrustedHosts($data['hosts'] ?? [],
$data['ipHeaders'] ?? null,
$data['protocolHeaders'] ?? null,
$data['hostHeaders'] ?? null,
$data['urlHeaders'] ?? null,
$data['trustedHeaders'] ?? null
);
}
}

0 comments on commit 8afc22a

Please sign in to comment.