Allowing the user change the response class.

This commit is contained in:
Juan Basso 2010-12-14 02:46:31 -02:00
parent 176da15417
commit 2f64afe44e
2 changed files with 52 additions and 1 deletions

View file

@ -76,6 +76,13 @@ class HttpSocket extends CakeSocket {
*/ */
public $response = null; public $response = null;
/**
* Response classname
*
* @var string
*/
public $responseClass = 'HttpResponse';
/** /**
* Configuration settings for the HttpSocket and the requests * Configuration settings for the HttpSocket and the requests
* *
@ -367,7 +374,11 @@ class HttpSocket extends CakeSocket {
$this->disconnect(); $this->disconnect();
} }
$this->response = new HttpResponse($response); if (!App::import('Lib', $this->responseClass)) {
throw new Exception(__('Class %s not found.', $this->responseClass));
}
$responseClass = $this->responseClass;
$this->response = new $responseClass($response);
if (!empty($this->response->cookies)) { if (!empty($this->response->cookies)) {
if (!isset($this->config['request']['cookies'][$Host])) { if (!isset($this->config['request']['cookies'][$Host])) {
$this->config['request']['cookies'][$Host] = array(); $this->config['request']['cookies'][$Host] = array();

View file

@ -51,6 +51,29 @@ class TestAuthentication {
} }
/**
* CustomResponse
*
*/
class CustomResponse {
/**
* First 10 chars
*
* @var string
*/
public $first10;
/**
* Constructor
*
*/
public function __construct($message) {
$this->first10 = substr($message, 0, 10);
}
}
/** /**
* TestHttpSocket * TestHttpSocket
* *
@ -659,6 +682,23 @@ class HttpSocketTest extends CakeTestCase {
$this->assertEqual($this->Socket->config['request']['cookies'], $expected); $this->assertEqual($this->Socket->config['request']['cookies'], $expected);
} }
/**
* testRequestCustomResponse
*
* @return void
*/
public function testRequestCustomResponse() {
$this->Socket->connected = true;
$serverResponse = "HTTP/1.x 200 OK\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\n\r\n<h1>This is a test!</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse));
$this->Socket->expects($this->at(2))->method('read')->will($this->returnValue(false));
$this->Socket->responseClass = 'CustomResponse';
$response = $this->Socket->request('http://www.cakephp.org/');
$this->assertIsA($response, 'CustomResponse');
$this->assertEqual($response->first10, 'HTTP/1.x 2');
}
/** /**
* testProxy method * testProxy method
* *