Skip to content

Commit

Permalink
move 'redirect' to request, and allow to use int values as ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément Hallet committed Nov 13, 2011
1 parent 62ae6d5 commit ecb8879
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/Cake/Network/Http/HttpSocket.php
Expand Up @@ -63,6 +63,7 @@ class HttpSocket extends CakeSocket {
'User-Agent' => 'CakePHP'
),
'raw' => null,
'redirect' => false,
'cookies' => array()
);

Expand Down Expand Up @@ -91,13 +92,13 @@ class HttpSocket extends CakeSocket {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
'host' => 'localhost',
'port' => 80
),
'redirect' => false,
'cookies' => array()
)
);
Expand Down Expand Up @@ -378,8 +379,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()) {

if($this->request['redirect'] && $this->response->isRedirect()) {
$request['uri'] = $this->response->getHeader('Location');
$request['redirect'] = is_int($this->request['redirect']) ? $this->request['redirect'] - 1 : $this->request['redirect'];
$this->response = $this->request($request);
}

Expand Down
44 changes: 38 additions & 6 deletions lib/Cake/Test/Case/Network/Http/HttpSocketTest.php
Expand Up @@ -252,13 +252,13 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 23,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'https',
'host' => 'www.cakephp.org',
'port' => 23
),
'redirect' => false,
'cookies' => array()
)
);
Expand All @@ -277,13 +277,13 @@ public function testConfigUri() {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array(
'scheme' => 'http',
'host' => 'www.foo.com',
'port' => 80
),
'redirect' => false,
'cookies' => array()
)
);
Expand Down Expand Up @@ -318,13 +318,13 @@ public function testRequest() {
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30,
'redirect' => false,
'request' => array(
'uri' => array (
'scheme' => 'http',
'host' => 'www.cakephp.org',
'port' => 80
),
'redirect' => false,
'cookies' => array()
)
),
Expand All @@ -345,6 +345,7 @@ public function testRequest() {
'line' => "GET /?foo=bar HTTP/1.1\r\n",
'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n",
'raw' => "",
'redirect' => false,
'cookies' => array(),
'proxy' => array(),
'auth' => array()
Expand Down Expand Up @@ -722,19 +723,50 @@ public function testRequestCustomResponse() {
*
* @return void
*/
public function testRequestWithRedirect() {
public function testRequestWithRedirectAsTrue() {
$request = array(
'uri' => 'http://localhost/oneuri'
'uri' => 'http://localhost/oneuri',
'redirect' => true
);
$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());
}

public function testRequestWithRedirectAsInt() {
$request = array(
'uri' => 'http://localhost/oneuri',
'redirect' => 2
);
$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));

$response = $this->Socket->request($request);
$this->assertEquals(1, $this->Socket->request['redirect']);
}

public function testRequestWithRedirectAsIntReachingZero() {
$request = array(
'uri' => 'http://localhost/oneuri',
'redirect' => 1
);
$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/oneruri\r\n\r\n";
$serverResponse2 = "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";
$this->Socket->expects($this->at(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));

$response = $this->Socket->request($request);
$this->assertEquals(0, $this->Socket->request['redirect']);
$this->assertEquals(302, $response->code);
$this->assertEquals('http://localhost/anotheruri', $response->getHeader('Location'));
}


/**
* testProxy method
Expand Down

0 comments on commit ecb8879

Please sign in to comment.