PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/guzzle/guzzle/tests/Guzzle/Tests/Http/RedirectPluginTest.php

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