Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#5659 from lucascorbeau…
Browse files Browse the repository at this point in the history
…x/hotfix/ZF-5658

Fix a fatal error when assert WWW-Authenticate header is sent - Hotfix/5658
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 22 deletions.
93 changes: 72 additions & 21 deletions src/PHPUnit/Controller/AbstractHttpControllerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,30 @@ public function assertResponseHeaderContains($header, $match)
$header
));
}
if ($match != $responseHeader->getFieldValue()) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(

if (!$responseHeader instanceof \ArrayIterator) {
$responseHeader = array($responseHeader);
}

$headerMatched = false;

foreach ($responseHeader as $currentHeader) {
if ($match == $currentHeader->getFieldValue()) {
$headerMatched = true;
break;
}
}

if (!$headerMatched) {
throw new \PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" exists and contains "%s", actual content is "%s"',
$header,
$match,
$responseHeader->getFieldValue()
$currentHeader->getFieldValue()
));
}
$this->assertEquals($match, $responseHeader->getFieldValue());

$this->assertEquals($match, $currentHeader->getFieldValue());
}

/**
Expand All @@ -114,14 +129,22 @@ public function assertNotResponseHeaderContains($header, $match)
$header
));
}
if ($match == $responseHeader->getFieldValue()) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" DOES NOT CONTAIN "%s"',
$header,
$match
));

if (!$responseHeader instanceof \ArrayIterator) {
$responseHeader = array($responseHeader);
}

foreach ($responseHeader as $currentHeader) {
if ($match == $currentHeader->getFieldValue()) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" DOES NOT CONTAIN "%s"',
$header,
$match
));
}
}
$this->assertNotEquals($match, $responseHeader->getFieldValue());

$this->assertNotEquals($match, $currentHeader->getFieldValue());
}

/**
Expand All @@ -139,15 +162,31 @@ public function assertResponseHeaderRegex($header, $pattern)
$header
));
}
if (!preg_match($pattern, $responseHeader->getFieldValue())) {

if (!$responseHeader instanceof \ArrayIterator) {
$responseHeader = array($responseHeader);
}

$headerMatched = false;

foreach ($responseHeader as $currentHeader) {
$headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue());

if ($headerMatched) {
break;
}
}

if (!$headerMatched) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" exists and matches regex "%s", actual content is "%s"',
$header,
$pattern,
$responseHeader->getFieldValue()
$currentHeader->getFieldValue()
));
}
$this->assertTrue((bool) preg_match($pattern, $responseHeader->getFieldValue()));

$this->assertTrue($headerMatched);
}

/**
Expand All @@ -165,14 +204,26 @@ public function assertNotResponseHeaderRegex($header, $pattern)
$header
));
}
if (preg_match($pattern, $responseHeader->getFieldValue())) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" DOES NOT MATCH regex "%s"',
$header,
$pattern
));

if (!$responseHeader instanceof \ArrayIterator) {
$responseHeader = array($responseHeader);
}
$this->assertFalse((bool) preg_match($pattern, $responseHeader->getFieldValue()));

$headerMatched = false;

foreach ($responseHeader as $currentHeader) {
$headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue());

if ($headerMatched) {
throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
'Failed asserting response header "%s" DOES NOT MATCH regex "%s"',
$header,
$pattern
));
}
}

$this->assertFalse($headerMatched);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/PHPUnit/Controller/AbstractHttpControllerTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public function testAssertResponseHeaderContains()
$this->assertResponseHeaderContains('Content-Type', 'text/json');
}

public function testAssertResponseHeaderContainsMultipleHeaderInterface()
{
$this->dispatch('/tests');
$this->assertResponseHeaderContains('WWW-Authenticate', 'Basic realm="ZF2"');
}

public function testAssertNotResponseHeaderContains()
{
$this->dispatch('/tests');
Expand All @@ -93,6 +99,12 @@ public function testAssertNotResponseHeaderContains()
$this->assertNotResponseHeaderContains('Content-Type', 'text/html');
}

public function testAssertNotResponseHeaderContainsMultipleHeaderInterface()
{
$this->dispatch('/tests');
$this->assertNotResponseHeaderContains('WWW-Authenticate', 'Basic realm="ZF3"');
}

public function testAssertResponseHeaderRegex()
{
$this->dispatch('/tests');
Expand All @@ -105,6 +117,12 @@ public function testAssertResponseHeaderRegex()
$this->assertResponseHeaderRegex('Content-Type', '#json#');
}

public function testAssertResponseHeaderRegexMultipleHeaderInterface()
{
$this->dispatch('/tests');
$this->assertResponseHeaderRegex('WWW-Authenticate', '#"ZF2"$#');
}

public function testAssertNotResponseHeaderRegex()
{
$this->dispatch('/tests');
Expand All @@ -114,6 +132,12 @@ public function testAssertNotResponseHeaderRegex()
$this->assertNotResponseHeaderRegex('Content-Type', '#html$#');
}

public function testAssertNotResponseHeaderRegexMultipleHeaderInterface()
{
$this->dispatch('/tests');
$this->assertNotResponseHeaderRegex('WWW-Authenticate', '#"ZF3"$#');
}

public function testAssertRedirect()
{
$this->dispatch('/redirect');
Expand Down
5 changes: 4 additions & 1 deletion test/_files/Baz/src/Baz/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class IndexController extends AbstractActionController
{
public function unittestsAction()
{
$this->getResponse()->getHeaders()->addHeaderLine('Content-Type: text/html');
$this->getResponse()
->getHeaders()
->addHeaderLine('Content-Type: text/html')
->addHeaderLine('WWW-Authenticate: Basic realm="ZF2"');

$num_get = $this->getRequest()->getQuery()->get('num_get', 0);
$num_post = $this->getRequest()->getPost()->get('num_post', 0);
Expand Down

0 comments on commit ed4a766

Please sign in to comment.