Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rbairwell committed Apr 19, 2016
1 parent cd697d5 commit 2583d57
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 15 deletions.
30 changes: 22 additions & 8 deletions src/MiddlewareCors/Traits/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,21 @@ protected function parseOrigin(ServerRequestInterface $request) : string
$origin = strtolower($origin);
$parsed = parse_url($origin);
$protocol='';
if (true === is_array($parsed) && true === isset($parsed['host'])) {
$this->addLog('Parsed a hostname from origin: '.$parsed['host']);
$origin = $parsed['host'];
}
if (true===is_array($parsed) && true===isset($parsed['scheme'])) {
$this->addLog('Parsed a protocol from origin: '.$parsed['scheme']);
$protocol=$parsed['scheme'].'://';
if (true === is_array($parsed)) {
if (true === isset($parsed['host'])) {
$this->addLog('Parsed a hostname from origin: '.$parsed['host']);
$origin = $parsed['host'];
} else {
$this->addLog('Unable to parse hostname from origin');
}
if (true===isset($parsed['scheme'])) {
$this->addLog('Parsed a protocol from origin: '.$parsed['scheme']);
$protocol=$parsed['scheme'].'://';
} else {
$this->addLog('Unable to parse protocol/scheme from origin');
}
} else {
$this->addLog('Unable to parse URL from origin of '.$origin);
}

// read the current origin setting
Expand All @@ -205,7 +213,11 @@ protected function parseOrigin(ServerRequestInterface $request) : string
// if anything else but '' was returned, then we have a valid match.
if ('' !== $matched) {
$this->addLog('Iterator found a matched origin of '.$matched);
return $protocol.$matched;
if ('*'===$matched) {
return $matched;
} else {
return $protocol.$matched;
}
}
}
}
Expand All @@ -220,6 +232,8 @@ protected function parseOrigin(ServerRequestInterface $request) : string
// return the matched setting (may be '' to indicate nothing matched)
if (''===$matched) {
return '';
} elseif ('*'===$matched) {
return $matched;
} else {
return $protocol.$matched;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/MiddlewareCors/Traits/RunInvokeArrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private function arraysAreSimilar(array $a, array $b, string $message = '')
$msg .= ' "'.$b[$k].'"';
}

$this->assertSame($v, $b[$k], $msg);
$this->assertSame($v, $b[$k], 'Comparing arrays:'.$msg);
}
}//end foreach
}//end arraysAreSimilar()
Expand Down
77 changes: 71 additions & 6 deletions tests/MiddlewareCorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function testInvokerWithOriginHeader()
$results = $this->runInvoke(
[
'method' => 'GET',
'setHeaders' => ['origin' => 'example.com'],
'setHeaders' => ['origin' => 'http://example.com'],
'configuration' => []
]
);
Expand All @@ -181,7 +181,9 @@ public function testInvokerWithOriginHeader()
// check logs
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "example.com"',
'Processing origin of "http://example.com"',
'Parsed a hostname from origin: example.com',
'Parsed a protocol from origin: http',
'Attempting to match origin as string',
'Checking configuration origin of "*" against user "example.com"',
'Origin is either an empty string or wildcarded star. Returning *',
Expand Down Expand Up @@ -224,6 +226,7 @@ public function testInvokerWithFullyQualifiedOriginHeader()
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "http://example.com:83/text.html"',
'Parsed a hostname from origin: example.com',
'Parsed a protocol from origin: http',
'Attempting to match origin as string',
'Checking configuration origin of "*" against user "example.com"',
'Origin is either an empty string or wildcarded star. Returning *',
Expand All @@ -234,6 +237,48 @@ public function testInvokerWithFullyQualifiedOriginHeader()
$this->assertEquals($expectedLogs,$logEntries);

}//end testInvokerWithOriginHeader()
/**
* Runs a test based on this having:
* - Method: GET
* - * allowed origin (default)
* - Origin set to example.com (matching wildcard)
* should get
* Access-Control-Allow-Origin
* and next called.
*
* @test
* @covers \Bairwell\MiddlewareCors::__construct
* @covers \Bairwell\MiddlewareCors::__invoke
* @covers \Bairwell\MiddlewareCors\Traits\Parse::parseOriginMatch
* @covers \Bairwell\MiddlewareCors\Traits\Parse::parseOrigin
*/
public function testInvokerWithFullyQualifiedOriginHeaderAndProtocol()
{
$results = $this->runInvoke(
[
'method' => 'GET',
'setHeaders' => ['origin' => 'https://example.com:83/text.html'],
'configuration' => ['origin' => 'example.com']
]
);
$expected = ['withHeader:Access-Control-Allow-Origin' => 'https://example.com', 'calledNext' => 'called'];
$this->arraysAreSimilar($results, $expected);
// check logs
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "https://example.com:83/text.html"',
'Parsed a hostname from origin: example.com',
'Parsed a protocol from origin: https',
'Attempting to match origin as string',
'Checking configuration origin of "example.com" against user "example.com"',
'Origin is an exact case insensitive match',
'Processing with origin of "https://example.com"',
'Calling next bit of middleware'
];
$logEntries=$this->getLoggerStrings();
$this->assertEquals($expectedLogs,$logEntries);

}//end testInvokerWithOriginHeaderAndProtocol()
/**
* Runs a test based on this having:
* - Method: GET
Expand All @@ -254,20 +299,22 @@ public function testInvokerWithCustomOriginHeader()
$results = $this->runInvoke(
[
'method' => 'GET',
'setHeaders' => ['origin' => 'example.com'],
'setHeaders' => ['origin' => 'http://example.com'],
'configuration' => ['origin' => 'example.com']
]
);
$expected = ['withHeader:Access-Control-Allow-Origin' => 'example.com', 'calledNext' => 'called'];
$expected = ['withHeader:Access-Control-Allow-Origin' => 'http://example.com', 'calledNext' => 'called'];
$this->arraysAreSimilar($results, $expected);
// check logs
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "example.com"',
'Processing origin of "http://example.com"',
'Parsed a hostname from origin: example.com',
'Parsed a protocol from origin: http',
'Attempting to match origin as string',
'Checking configuration origin of "example.com" against user "example.com"',
'Origin is an exact case insensitive match',
'Processing with origin of "example.com"',
'Processing with origin of "http://example.com"',
'Calling next bit of middleware'
];
$logEntries=$this->getLoggerStrings();
Expand Down Expand Up @@ -309,6 +356,8 @@ public function testInvokerWithCustomOriginHeaderInvalid()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Attempting to match origin as string',
'Checking configuration origin of "example.com" against user "dummy.com"',
'Unable to match "example.com" against user "dummy.com"'
Expand Down Expand Up @@ -383,6 +432,8 @@ public function testInvokerWithCustomOriginHeaderDummyCallback()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Attempting to match origin as string',
'Checking configuration origin of "example.com" against user "dummy.com"',
'Unable to match "example.com" against user "dummy.com"'
Expand Down Expand Up @@ -428,6 +479,8 @@ public function testInvokerWithCustomOriginHeaderCustomCallbacks()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Origin server request is being passed to callback',
'Attempting to match origin as string',
'Checking configuration origin of "hello" against user "dummy.com"',
Expand Down Expand Up @@ -470,6 +523,8 @@ public function testInvokerWithCustomOriginHeaderCustomAllowedCallbacks()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Origin server request is being passed to callback',
'Attempting to match origin as string',
'Checking configuration origin of "dummy.com" against user "dummy.com"',
Expand Down Expand Up @@ -511,6 +566,8 @@ public function testInvokerWithOriginArray()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Iterating through Origin array',
'Checking configuration origin of "example.com" against user "dummy.com"',
'Unable to match "example.com" against user "dummy.com"',
Expand Down Expand Up @@ -554,6 +611,8 @@ public function testInvokerWithOriginArrayWildcard()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "www.dummy.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Iterating through Origin array',
'Checking configuration origin of "example.com" against user "www.dummy.com"',
'Unable to match "example.com" against user "www.dummy.com"',
Expand Down Expand Up @@ -602,6 +661,8 @@ public function testInvokerWithOriginArrayInvalid()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "bbc.co.uk"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Iterating through Origin array',
'Checking configuration origin of "example.com" against user "bbc.co.uk"',
'Unable to match "example.com" against user "bbc.co.uk"',
Expand Down Expand Up @@ -646,6 +707,8 @@ public function testInvokerWithOriginHeaderAndCredentials()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "example.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Attempting to match origin as string',
'Checking configuration origin of "*" against user "example.com"',
'Origin is either an empty string or wildcarded star. Returning *',
Expand Down Expand Up @@ -693,6 +756,8 @@ public function testInvokerWithOriginHeaderAndCredentialsWithHeaders()
$expectedLogs=[
'Request has an origin setting and is being treated like a CORs request',
'Processing origin of "example.com"',
'Unable to parse hostname from origin',
'Unable to parse protocol/scheme from origin',
'Attempting to match origin as string',
'Checking configuration origin of "*" against user "example.com"',
'Origin is either an empty string or wildcarded star. Returning *',
Expand Down

0 comments on commit 2583d57

Please sign in to comment.