Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
fixed counting redirects in Zend\Http\Client, maxredirects=1 now works
Browse files Browse the repository at this point in the history
  • Loading branch information
driehle committed Apr 12, 2013
1 parent 555316c commit e196bc3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/Zend/Http/Client.php
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/ZendTest/Http/ClientTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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.