1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278:
<?php
namespace Guzzle\Tests\Plugin\Redirect;
use Guzzle\Http\Client;
use Guzzle\Http\EntityBody;
use Guzzle\Http\RedirectPlugin;
use Guzzle\Http\Exception\TooManyRedirectsException;
use Guzzle\Plugin\History\HistoryPlugin;
class RedirectPluginTest extends \Guzzle\Tests\GuzzleTestCase
{
public function testRedirectsRequests()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$history = new HistoryPlugin();
$client->addSubscriber($history);
$request = $client->get('/foo');
$response = $request->send();
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('/redirect2', $response->getEffectiveUrl());
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('/foo', $requests[0]->getResource());
$this->assertEquals('GET', $requests[0]->getMethod());
$this->assertEquals('/redirect1', $requests[1]->getResource());
$this->assertEquals('GET', $requests[1]->getMethod());
$this->assertEquals('/redirect2', $requests[2]->getResource());
$this->assertEquals('GET', $requests[2]->getMethod());
$this->assertEquals(2, $request->getParams()->get(RedirectPlugin::REDIRECT_COUNT));
$this->assertCount(3, $history);
$requestHistory = $history->getAll();
$this->assertEquals(301, $requestHistory[0]['response']->getStatusCode());
$this->assertEquals('/redirect1', (string) $requestHistory[0]['response']->getHeader('Location'));
$this->assertEquals(301, $requestHistory[1]['response']->getStatusCode());
$this->assertEquals('/redirect2', (string) $requestHistory[1]['response']->getHeader('Location'));
$this->assertEquals(200, $requestHistory[2]['response']->getStatusCode());
}
public function testCanLimitNumberOfRedirects()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect3\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect4\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect5\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect6\r\nContent-Length: 0\r\n\r\n"
));
try {
$client = new Client($this->getServer()->getUrl());
$client->get('/foo')->send();
$this->fail('Did not throw expected exception');
} catch (TooManyRedirectsException $e) {
$this->assertContains(
"5 redirects were issued for this request:\nGET /foo HTTP/1.1\r\n",
$e->getMessage()
);
}
}
public function testDefaultBehaviorIsToRedirectWithGetForEntityEnclosingRequests()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$client->post('/foo', array('X-Baz' => 'bar'), 'testing')->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertEquals('GET', $requests[1]->getMethod());
$this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz'));
$this->assertEquals('GET', $requests[2]->getMethod());
}
public function testCanRedirectWithStrictRfcCompliance()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$request = $client->post('/foo', array('X-Baz' => 'bar'), 'testing');
$request->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, true);
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertEquals('POST', $requests[1]->getMethod());
$this->assertEquals('bar', (string) $requests[1]->getHeader('X-Baz'));
$this->assertEquals('POST', $requests[2]->getMethod());
}
public function testRedirect303WithGet()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 303 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$request = $client->post('/foo');
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertEquals('GET', $requests[1]->getMethod());
}
public function testRedirect303WithGetWithStrictRfcCompliance()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 303 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$request = $client->post('/foo');
$request->getParams()->set(RedirectPlugin::STRICT_REDIRECTS, true);
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('POST', $requests[0]->getMethod());
$this->assertEquals('GET', $requests[1]->getMethod());
}
public function testRewindsStreamWhenRedirectingIfNeeded()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$request = $client->put();
$request->configureRedirects(true);
$body = EntityBody::factory('foo');
$body->read(1);
$request->setBody($body);
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('foo', (string) $requests[0]->getBody());
}
public function testThrowsExceptionWhenStreamCannotBeRewound()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nhi",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect\r\nContent-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$request = $client->put();
$request->configureRedirects(true);
$body = EntityBody::factory(fopen($this->getServer()->getUrl(), 'r'));
$body->read(1);
$request->setBody($body)->send();
}
public function testRedirectsCanBeDisabledPerRequest()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array("HTTP/1.1 301 Foo\r\nLocation: /foo\r\nContent-Length: 0\r\n\r\n"));
$client = new Client($this->getServer()->getUrl());
$request = $client->put();
$request->configureRedirects(false, 0);
$this->assertEquals(301, $request->send()->getStatusCode());
}
public function testCanRedirectWithNoLeadingSlashAndQuery()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: redirect?foo=bar\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
));
$client = new Client($this->getServer()->getUrl());
$request = $client->get('?foo=bar');
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals($this->getServer()->getUrl() . '?foo=bar', $requests[0]->getUrl());
$this->assertEquals($this->getServer()->getUrl() . 'redirect?foo=bar', $requests[1]->getUrl());
$this->assertEquals($this->getServer()->getUrl() . '?foo=bar', $request->getUrl());
}
public function testRedirectWithStrictRfc386Compliance()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: redirect\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$request = $client->get('/foo');
$request->send();
$requests = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('/redirect', $requests[1]->getResource());
}
public function testResetsHistoryEachSend()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect1\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect2\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$history = new HistoryPlugin();
$client->addSubscriber($history);
$request = $client->get('/foo');
$response = $request->send();
$this->assertEquals(3, count($history));
$this->assertTrue($request->getParams()->hasKey('redirect.count'));
$this->assertContains('/redirect2', $response->getEffectiveUrl());
$request->send();
$this->assertFalse($request->getParams()->hasKey('redirect.count'));
}
public function testHandlesRedirectsWithSpacesProperly()
{
$this->getServer()->flush();
$this->getServer()->enqueue(array(
"HTTP/1.1 301 Moved Permanently\r\nLocation: /redirect 1\r\nContent-Length: 0\r\n\r\n",
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
));
$client = new Client($this->getServer()->getUrl());
$request = $client->get('/foo');
$request->send();
$reqs = $this->getServer()->getReceivedRequests(true);
$this->assertEquals('/redirect%201', $reqs[1]->getResource());
}
}