Skip to content

Commit

Permalink
optionnaly handles redirect before return the response
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément Hallet committed Nov 8, 2011
1 parent 606c30b commit 62ae6d5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/Cake/Network/Http/HttpResponse.php
Expand Up @@ -123,6 +123,15 @@ public function getHeader($name, $headers = null) {
public function isOk() {
return $this->code == 200;
}

/**
* If return is a valid 3xx (Redirection)
*
* @return boolean
*/
public function isRedirect() {
return in_array($this->code, array(301, 302, 303, 307)) && !is_null($this->getHeader('Location'));
}

/**
* Parses the given message and breaks it down in parts.
Expand Down
5 changes: 5 additions & 0 deletions lib/Cake/Network/Http/HttpSocket.php
Expand Up @@ -91,6 +91,7 @@ class HttpSocket extends CakeSocket {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
Expand Down Expand Up @@ -377,6 +378,10 @@ public function request($request = array()) {
}
$this->config['request']['cookies'][$Host] = array_merge($this->config['request']['cookies'][$Host], $this->response->cookies);
}
if($this->config['redirect'] && $this->response->isRedirect()) {
$request['uri'] = $this->response->getHeader('Location');
$this->response = $this->request($request);
}

return $this->response;
}
Expand Down
36 changes: 36 additions & 0 deletions lib/Cake/Test/Case/Network/Http/HttpResponseTest.php
Expand Up @@ -164,6 +164,42 @@ public function testIsOk() {
$this->assertTrue($this->HttpResponse->isOk());
}

/**
* testIsRedirect
*
* @return void
*/
public function testIsRedirect() {
$this->HttpResponse->code = 0;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = -1;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 201;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 'what?';
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 301;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 302;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 303;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 307;
$this->assertFalse($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 301;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 302;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 303;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
$this->HttpResponse->code = 307;
$this->HttpResponse->headers['Location'] = 'http://somewhere/';
$this->assertTrue($this->HttpResponse->isRedirect());
}

/**
* Test that HttpSocket::parseHeader can take apart a given (and valid) $header string and turn it into an array.
*
Expand Down
25 changes: 24 additions & 1 deletion lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Expand Up @@ -252,6 +252,7 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 23,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'https',
Expand All @@ -276,6 +277,7 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
Expand Down Expand Up @@ -316,13 +318,14 @@ public function testRequest() {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array (
'scheme' => 'http',
'host' => 'www.cakephp.org',
'port' => 80
),
'cookies' => array(),
'cookies' => array()
)
),
'request' => array(
Expand Down Expand Up @@ -712,6 +715,26 @@ public function testRequestCustomResponse() {
$this->assertIsA($response, 'CustomResponse');
$this->assertEqual($response->first10, 'HTTP/1.x 2');
}


/**
* testRequestWithRedirect method
*
* @return void
*/
public function testRequestWithRedirect() {
$request = array(
'uri' => 'http://localhost/oneuri'
);
$serverResponse1 = "HTTP/1.x 302 Found\r\nDate: Mon, 16 Apr 2007 04:14:16 GMT\r\nServer: CakeHttp Server\r\nContent-Type: text/html\r\nLocation: http://localhost/anotheruri\r\n\r\n";
$serverResponse2 = "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>You have been redirected</h1>";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
$this->Socket->config['redirect'] = true;

$response = $this->Socket->request($request);
$this->assertEquals('<h1>You have been redirected</h1>', $response->body());
}

/**
* testProxy method
Expand Down

0 comments on commit 62ae6d5

Please sign in to comment.