Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optionnaly handles redirect before return the response

  • Loading branch information...
commit 62ae6d57e901d2f9560267360c0b4b9358e58004 1 parent 606c30b
Clément Hallet authored
View
9 lib/Cake/Network/Http/HttpResponse.php
@@ -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.
View
5 lib/Cake/Network/Http/HttpSocket.php
@@ -91,6 +91,7 @@ class HttpSocket extends CakeSocket {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
+ 'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
@@ -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;
}
View
36 lib/Cake/Test/Case/Network/Http/HttpResponseTest.php
@@ -165,6 +165,42 @@ public function testIsOk() {
}
/**
+ * 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.
*
* @return void
View
25 lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
@@ -252,6 +252,7 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 23,
'timeout' => 30,
+ 'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'https',
@@ -276,6 +277,7 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
+ 'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
@@ -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(
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.