move 'redirect' to request, and allow to use int values as ttl

This commit is contained in:
Clément Hallet 2011-11-13 17:45:33 +01:00
parent 62ae6d57e9
commit ecb88791c8
2 changed files with 43 additions and 8 deletions

View file

@ -63,6 +63,7 @@ class HttpSocket extends CakeSocket {
'User-Agent' => 'CakePHP' 'User-Agent' => 'CakePHP'
), ),
'raw' => null, 'raw' => null,
'redirect' => false,
'cookies' => array() 'cookies' => array()
); );
@ -91,13 +92,13 @@ class HttpSocket extends CakeSocket {
'protocol' => 'tcp', 'protocol' => 'tcp',
'port' => 80, 'port' => 80,
'timeout' => 30, 'timeout' => 30,
'redirect' => false,
'request' => array( 'request' => array(
'uri' => array( 'uri' => array(
'scheme' => 'http', 'scheme' => 'http',
'host' => 'localhost', 'host' => 'localhost',
'port' => 80 'port' => 80
), ),
'redirect' => false,
'cookies' => array() 'cookies' => array()
) )
); );
@ -378,8 +379,10 @@ class HttpSocket extends CakeSocket {
} }
$this->config['request']['cookies'][$Host] = array_merge($this->config['request']['cookies'][$Host], $this->response->cookies); $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['uri'] = $this->response->getHeader('Location');
$request['redirect'] = is_int($this->request['redirect']) ? $this->request['redirect'] - 1 : $this->request['redirect'];
$this->response = $this->request($request); $this->response = $this->request($request);
} }

View file

@ -252,13 +252,13 @@ class HttpSocketTest extends CakeTestCase {
'protocol' => 'tcp', 'protocol' => 'tcp',
'port' => 23, 'port' => 23,
'timeout' => 30, 'timeout' => 30,
'redirect' => false,
'request' => array( 'request' => array(
'uri' => array( 'uri' => array(
'scheme' => 'https', 'scheme' => 'https',
'host' => 'www.cakephp.org', 'host' => 'www.cakephp.org',
'port' => 23 'port' => 23
), ),
'redirect' => false,
'cookies' => array() 'cookies' => array()
) )
); );
@ -277,13 +277,13 @@ class HttpSocketTest extends CakeTestCase {
'protocol' => 'tcp', 'protocol' => 'tcp',
'port' => 80, 'port' => 80,
'timeout' => 30, 'timeout' => 30,
'redirect' => false,
'request' => array( 'request' => array(
'uri' => array( 'uri' => array(
'scheme' => 'http', 'scheme' => 'http',
'host' => 'www.foo.com', 'host' => 'www.foo.com',
'port' => 80 'port' => 80
), ),
'redirect' => false,
'cookies' => array() 'cookies' => array()
) )
); );
@ -318,13 +318,13 @@ class HttpSocketTest extends CakeTestCase {
'protocol' => 'tcp', 'protocol' => 'tcp',
'port' => 80, 'port' => 80,
'timeout' => 30, 'timeout' => 30,
'redirect' => false,
'request' => array( 'request' => array(
'uri' => array ( 'uri' => array (
'scheme' => 'http', 'scheme' => 'http',
'host' => 'www.cakephp.org', 'host' => 'www.cakephp.org',
'port' => 80 'port' => 80
), ),
'redirect' => false,
'cookies' => array() 'cookies' => array()
) )
), ),
@ -345,6 +345,7 @@ class HttpSocketTest extends CakeTestCase {
'line' => "GET /?foo=bar HTTP/1.1\r\n", 'line' => "GET /?foo=bar HTTP/1.1\r\n",
'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n", 'header' => "Host: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\n",
'raw' => "", 'raw' => "",
'redirect' => false,
'cookies' => array(), 'cookies' => array(),
'proxy' => array(), 'proxy' => array(),
'auth' => array() 'auth' => array()
@ -722,20 +723,51 @@ class HttpSocketTest extends CakeTestCase {
* *
* @return void * @return void
*/ */
public function testRequestWithRedirect() { public function testRequestWithRedirectAsTrue() {
$request = array( $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"; $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>"; $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(1))->method('read')->will($this->returnValue($serverResponse1));
$this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2)); $this->Socket->expects($this->at(4))->method('read')->will($this->returnValue($serverResponse2));
$this->Socket->config['redirect'] = true;
$response = $this->Socket->request($request); $response = $this->Socket->request($request);
$this->assertEquals('<h1>You have been redirected</h1>', $response->body()); $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 * testProxy method
* *