Skip to content
Permalink
Browse files

Starting to implement Response object.

  • Loading branch information...
markstory committed Dec 25, 2012
1 parent ef79fd4 commit f1be0f1d73d0445914a7ce815e76c3fb5266ae66
Showing with 208 additions and 9 deletions.
  1. +107 −9 lib/Cake/Network/Http/Response.php
  2. +101 −0 lib/Cake/Test/TestCase/Network/Http/ResponseTest.php
@@ -15,26 +15,124 @@
/**
* Implements methods for HTTP responses
*
* ### Get header values
*
* ### Get the response body
*
* ### Check the status code
*
*/
class Response {
const STATUS_OK = 200;
/**
* The status code of the response.
*
* @var int
*/
protected $_code;
/**
* The array of headers in the response.
*
* @var array
*/
protected $_headers;
/**
* The response content
*
* @var string
*/
protected $_content;
public function headers($headers = null) {
if ($headers === null) {
/**
* Constructor
*
* @param array $headers Unparsed headers.
* @param string $content The response body.
*/
public function __construct($headers, $content) {
$this->_parseHeaders($headers);
$this->_content = $content;
}
/**
* Parses headers if necessary.
*
* @param array $headers
*/
protected function _parseHeaders($headers) {
foreach ($headers as $key => $value) {
if (substr($value, 0, 5) === 'HTTP/') {
preg_match('/HTTP\/[\d.]+ ([0-9]+)/i', $value, $matches);
$this->_code = $matches[1];
continue;
}
if (is_int($key)) {
list($name, $value) = explode(':', $value, 2);
$this->_headers[$name] = $value;
continue;
}
}
}
/**
* Check if the response was OK
*
* @return boolean
*/
public function isOk() {
return $this->_code == static::STATUS_OK;
}
/**
* Check if the response had a redirect status code.
*
* @return boolean
*/
public function isRedirect() {
return (
in_array($this->_code, array(301, 302, 303, 307)) &&
$this->header('Location')
);
}
/**
* Get the status code from the response
*
* @return int
*/
public function statusCode() {
return $this->_code;
}
/**
* Get the encoding if it was set.
*
* @return string
*/
public function encoding() {
}
/**
* Read single/multiple header value(s) out.
*
* @param string $name The name of the header you want. Leave
* null to get all headers.
* @return null|string
*/
public function header($name = null) {
if ($name === null) {
return $this->_headers;
}
$this->_headers = $headers;
return $this;
return $this->_headers[$name];
}
public function content($content) {
if ($content === null) {
return $this->_content;
}
$this->_content = $content;
return $this;
return $this->_content;
}
}
@@ -0,0 +1,101 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Network\Http;
use Cake\Network\Http\Response;
use Cake\TestSuite\TestCase;
/**
* HTTP response test.
*/
class ResponseTest extends TestCase {
public function testHeaderParsing() {
$this->markTestIncomplete();
}
public function testContent() {
$this->markTestIncomplete();
}
/**
* Test isOk()
*
* @return void
*/
public function testIsOk() {
$headers = [
'HTTP/1.1 200 OK',
'Content-Type: text/html'
];
$response = new Response($headers, 'ok');
$this->assertTrue($response->isOk());
$headers = [
'HTTP/1.1 301 Moved Permanently',
'Content-Type: text/html'
];
$response = new Response($headers, '');
$this->assertFalse($response->isOk());
$headers = [
'HTTP/1.0 404 Not Found',
'Content-Type: text/html'
];
$response = new Response($headers, '');
$this->assertFalse($response->isOk());
}
/**
* Test isRedirect()
*
* @return void
*/
public function testIsRedirect() {
$headers = [
'HTTP/1.1 200 OK',
'Content-Type: text/html'
];
$response = new Response($headers, 'ok');
$this->assertFalse($response->isRedirect());
$headers = [
'HTTP/1.1 301 Moved Permanently',
'Location: /',
'Content-Type: text/html'
];
$response = new Response($headers, '');
$this->assertTrue($response->isRedirect());
$headers = [
'HTTP/1.0 404 Not Found',
'Content-Type: text/html'
];
$response = new Response($headers, '');
$this->assertFalse($response->isRedirect());
}
public function testCookie() {
$this->markTestIncomplete();
}
public function testStatusCode() {
$this->markTestIncomplete();
}
public function testEncoding() {
$this->markTestIncomplete();
}
}

0 comments on commit f1be0f1

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