Skip to content
Permalink
Browse files

Implement assertResponseXX methods.

Add positive tests for assertResponseXX methods, the negative tests seem
less useful right now.
  • Loading branch information...
markstory committed Sep 2, 2014
1 parent 210fd89 commit 7038e6acda4f80e917ffb907212a5cc6c2ffdc01
Showing with 75 additions and 0 deletions.
  1. +44 −0 src/TestSuite/IntegrationTestCase.php
  2. +31 −0 tests/TestCase/TestSuite/IntegrationTestCaseTest.php
@@ -237,4 +237,48 @@ protected function _buildRequest($url, $method, $data) {
return new Request($props);
}
/**
* Assert that the response status code is in the 2xx range.
*
* @return void
*/
public function assertResponseOk() {
$this->_assertStatus(200, 204, 'Status code is not between 200 and 204');
}
/**
* Assert that the response status code is in the 4xx range.
*
* @return void
*/
public function assertResponseError() {
$this->_assertStatus(400, 417, 'Status code is not between 400 and 417');
}
/**
* Assert that the response status code is in the 5xx range.
*
* @return void
*/
public function assertResponseFailure() {
$this->_assertStatus(500, 505, 'Status code is not between 500 and 505');
}
/**
* Helper method for status assertions.
*
* @param int $min Min status code.
* @param int $max Max status code.
* @param string $message The error message.
* @return void
*/
protected function _assertStatus($min, $max, $message) {
if (!$this->_response) {
$this->fail('No response set, cannot assert status code.');
}
$status = $this->_response->statusCode();
$this->assertGreaterThanOrEqual($min, $status, $message);
$this->assertLessThanOrEqual($max, $status, $message);
}
}
@@ -15,6 +15,7 @@
namespace Cake\Test\TestCase\TestSuite;
use Cake\Core\Configure;
use Cake\Network\Response;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestCase;
@@ -74,4 +75,34 @@ public function testSendingGet() {
$this->assertEquals('This is a test', $this->_response->body());
}
/**
* Test the responseOk status assertion
*
* @return void
*/
public function testAssertResponseStatusCodes() {
$this->_response = new Response();
$this->_response->statusCode(200);
$this->assertResponseOk();
$this->_response->statusCode(201);
$this->assertResponseOk();
$this->_response->statusCode(204);
$this->assertResponseOk();
$this->_response->statusCode(400);
$this->assertResponseError();
$this->_response->statusCode(417);
$this->assertResponseError();
$this->_response->statusCode(500);
$this->assertResponseFailure();
$this->_response->statusCode(505);
$this->assertResponseFailure();
}
}

0 comments on commit 7038e6a

Please sign in to comment.
You can’t perform that action at this time.