Skip to content
Browse files

fixed counting redirects in Zend\Http\Client, maxredirects=1 now works

  • Loading branch information...
1 parent 555316c commit e196bc32d2b5a1c5504a7d83434b2f23cd43c069 @driehle committed Apr 12, 2013
Showing with 38 additions and 1 deletion.
  1. +1 −1 library/Zend/Http/Client.php
  2. +37 −0 tests/ZendTest/Http/ClientTest.php
View
2 library/Zend/Http/Client.php
@@ -933,7 +933,7 @@ public function send(Request $request = null)
break;
}
- } while ($this->redirectCounter < $this->config['maxredirects']);
+ } while ($this->redirectCounter <= $this->config['maxredirects']);
$this->response = $response;
return $response;
View
37 tests/ZendTest/Http/ClientTest.php
@@ -18,6 +18,7 @@
use Zend\Http\Header\SetCookie;
use Zend\Http\Request;
use Zend\Http\Response;
+use Zend\Http\Client\Adapter\Test;
class ClientTest extends \PHPUnit_Framework_TestCase
@@ -198,4 +199,40 @@ public function testEncodeAuthHeaderThrowsExceptionWhenInvalidAuthTypeIsUsed()
{
$encoded = Client::encodeAuthHeader('test', 'test', 'test');
}
+
+ public function testIfMaxredirectWorksCorrectly()
+ {
+ $testAdapter = new Test();
+ // first response, contains a redirect
+ $testAdapter->setResponse(
+ "HTTP/1.1 303 See Other\r\n"
+ . "Location: http://www.example.org/part2\r\n\r\n"
+ . "Page #1"
+ );
+ // seconds response, contains a redirect
+ $testAdapter->addResponse(
+ "HTTP/1.1 303 See Other\r\n"
+ . "Location: http://www.example.org/part3\r\n\r\n"
+ . "Page #2"
+ );
+ // third response
+ $testAdapter->addResponse(
+ "HTTP/1.1 303 See Other\r\n\r\n"
+ . "Page #3"
+ );
+
+ // create a client which allows one redirect at most!
+ $client = new Client('http://www.example.org/part1', array(
+ 'adapter' => $testAdapter,
+ 'maxredirects' => 1,
+ 'storeresponse' => true
+ ));
+
+ // do the request
+ $response = $client->setMethod('GET')->send();
+
+ // response should be the second response, since third response should not
+ // be requested, due to the maxredirects = 1 limit
+ $this->assertEquals($response->getContent(), "Page #2");
+ }
}

0 comments on commit e196bc3

Please sign in to comment.
Something went wrong with that request. Please try again.