diff --git a/src/TestSuite/IntegrationTestCase.php b/src/TestSuite/IntegrationTestCase.php index 42c97aa64e8..5b32ebc9183 100644 --- a/src/TestSuite/IntegrationTestCase.php +++ b/src/TestSuite/IntegrationTestCase.php @@ -312,6 +312,44 @@ public function assertRedirect($url, $message = '') { $this->assertEquals(Router::url($url, ['_full' => true]), $result['Location'], $message); } +/** + * Assert response headers + * + * @param string $header The header to check + * @param string $content The content to check for. + * @param string $message The failure message that will be appended to the generated message. + * @return void + */ + public function assertHeader($header, $content, $message = '') { + if (!$this->_response) { + $this->fail('No response set, cannot assert headers. ' . $message); + } + $headers = $this->_response->header(); + if (!isset($headers[$header])) { + $this->fail("The '$header' header is not set. " . $message); + } + $this->assertEquals($headers[$header], $content, $message); + } + +/** + * Assert content type + * + * @param string $type The content-type to check for. + * @param string $message The failure message that will be appended to the generated message. + * @return void + */ + public function assertContentType($type, $message = '') { + if (!$this->_response) { + $this->fail('No response set, cannot assert content-type. ' . $message); + } + $alias = $this->_response->getMimeType($type); + if ($alias !== false) { + $type = $alias; + } + $result = $this->_response->type(); + $this->assertEquals($type, $result, $message); + } + /** * Assert content exists in the response body. * diff --git a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php index 26f762f5683..3371ffb9c2c 100644 --- a/tests/TestCase/TestSuite/IntegrationTestCaseTest.php +++ b/tests/TestCase/TestSuite/IntegrationTestCaseTest.php @@ -118,6 +118,31 @@ public function testAssertRedirect() { $this->assertRedirect(['controller' => 'Tasks', 'action' => 'index']); } +/** + * Test the header assertino + * + * @return void + */ + public function testAssertHeader() { + $this->_response = new Response(); + $this->_response->header('Etag', 'abc123'); + + $this->assertHeader('Etag', 'abc123'); + } + +/** + * Test the content type assertino + * + * @return void + */ + public function testAssertContentType() { + $this->_response = new Response(); + $this->_response->type('json'); + + $this->assertContentType('json'); + $this->assertContentType('application/json'); + } + /** * Test the content assertion. *