HandlerStackTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace GuzzleHttp\Tests;
  3. use GuzzleHttp\Cookie\CookieJar;
  4. use GuzzleHttp\Handler\MockHandler;
  5. use GuzzleHttp\HandlerStack;
  6. use GuzzleHttp\Psr7\Request;
  7. use GuzzleHttp\Psr7\Response;
  8. use PHPUnit\Framework\TestCase;
  9. class HandlerStackTest extends TestCase
  10. {
  11. public function testSetsHandlerInCtor()
  12. {
  13. $f = function () {};
  14. $m1 = function () {};
  15. $h = new HandlerStack($f, [$m1]);
  16. $this->assertTrue($h->hasHandler());
  17. }
  18. /**
  19. * @doesNotPerformAssertions
  20. */
  21. public function testCanSetDifferentHandlerAfterConstruction()
  22. {
  23. $f = function () {};
  24. $h = new HandlerStack();
  25. $h->setHandler($f);
  26. $h->resolve();
  27. }
  28. /**
  29. * @expectedException \LogicException
  30. */
  31. public function testEnsuresHandlerIsSet()
  32. {
  33. $h = new HandlerStack();
  34. $h->resolve();
  35. }
  36. public function testPushInOrder()
  37. {
  38. $meths = $this->getFunctions();
  39. $builder = new HandlerStack();
  40. $builder->setHandler($meths[1]);
  41. $builder->push($meths[2]);
  42. $builder->push($meths[3]);
  43. $builder->push($meths[4]);
  44. $composed = $builder->resolve();
  45. $this->assertSame('Hello - test123', $composed('test'));
  46. $this->assertSame(
  47. [['a', 'test'], ['b', 'test1'], ['c', 'test12']],
  48. $meths[0]
  49. );
  50. }
  51. public function testUnshiftsInReverseOrder()
  52. {
  53. $meths = $this->getFunctions();
  54. $builder = new HandlerStack();
  55. $builder->setHandler($meths[1]);
  56. $builder->unshift($meths[2]);
  57. $builder->unshift($meths[3]);
  58. $builder->unshift($meths[4]);
  59. $composed = $builder->resolve();
  60. $this->assertSame('Hello - test321', $composed('test'));
  61. $this->assertSame(
  62. [['c', 'test'], ['b', 'test3'], ['a', 'test32']],
  63. $meths[0]
  64. );
  65. }
  66. public function testCanRemoveMiddlewareByInstance()
  67. {
  68. $meths = $this->getFunctions();
  69. $builder = new HandlerStack();
  70. $builder->setHandler($meths[1]);
  71. $builder->push($meths[2]);
  72. $builder->push($meths[2]);
  73. $builder->push($meths[3]);
  74. $builder->push($meths[4]);
  75. $builder->push($meths[2]);
  76. $builder->remove($meths[3]);
  77. $composed = $builder->resolve();
  78. $this->assertSame('Hello - test1131', $composed('test'));
  79. }
  80. public function testCanPrintMiddleware()
  81. {
  82. $meths = $this->getFunctions();
  83. $builder = new HandlerStack();
  84. $builder->setHandler($meths[1]);
  85. $builder->push($meths[2], 'a');
  86. $builder->push([__CLASS__, 'foo']);
  87. $builder->push([$this, 'bar']);
  88. $builder->push(__CLASS__ . '::' . 'foo');
  89. $lines = explode("\n", (string) $builder);
  90. $this->assertContains("> 4) Name: 'a', Function: callable(", $lines[0]);
  91. $this->assertContains("> 3) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[1]);
  92. $this->assertContains("> 2) Name: '', Function: callable(['GuzzleHttp\\Tests\\HandlerStackTest', 'bar'])", $lines[2]);
  93. $this->assertContains("> 1) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[3]);
  94. $this->assertContains("< 0) Handler: callable(", $lines[4]);
  95. $this->assertContains("< 1) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[5]);
  96. $this->assertContains("< 2) Name: '', Function: callable(['GuzzleHttp\\Tests\\HandlerStackTest', 'bar'])", $lines[6]);
  97. $this->assertContains("< 3) Name: '', Function: callable(GuzzleHttp\\Tests\\HandlerStackTest::foo)", $lines[7]);
  98. $this->assertContains("< 4) Name: 'a', Function: callable(", $lines[8]);
  99. }
  100. public function testCanAddBeforeByName()
  101. {
  102. $meths = $this->getFunctions();
  103. $builder = new HandlerStack();
  104. $builder->setHandler($meths[1]);
  105. $builder->push($meths[2], 'foo');
  106. $builder->before('foo', $meths[3], 'baz');
  107. $builder->before('baz', $meths[4], 'bar');
  108. $builder->before('baz', $meths[4], 'qux');
  109. $lines = explode("\n", (string) $builder);
  110. $this->assertContains('> 4) Name: \'bar\'', $lines[0]);
  111. $this->assertContains('> 3) Name: \'qux\'', $lines[1]);
  112. $this->assertContains('> 2) Name: \'baz\'', $lines[2]);
  113. $this->assertContains('> 1) Name: \'foo\'', $lines[3]);
  114. }
  115. /**
  116. * @expectedException \InvalidArgumentException
  117. */
  118. public function testEnsuresHandlerExistsByName()
  119. {
  120. $builder = new HandlerStack();
  121. $builder->before('foo', function () {});
  122. }
  123. public function testCanAddAfterByName()
  124. {
  125. $meths = $this->getFunctions();
  126. $builder = new HandlerStack();
  127. $builder->setHandler($meths[1]);
  128. $builder->push($meths[2], 'a');
  129. $builder->push($meths[3], 'b');
  130. $builder->after('a', $meths[4], 'c');
  131. $builder->after('b', $meths[4], 'd');
  132. $lines = explode("\n", (string) $builder);
  133. $this->assertContains('4) Name: \'a\'', $lines[0]);
  134. $this->assertContains('3) Name: \'c\'', $lines[1]);
  135. $this->assertContains('2) Name: \'b\'', $lines[2]);
  136. $this->assertContains('1) Name: \'d\'', $lines[3]);
  137. }
  138. public function testPicksUpCookiesFromRedirects()
  139. {
  140. $mock = new MockHandler([
  141. new Response(301, [
  142. 'Location' => 'http://foo.com/baz',
  143. 'Set-Cookie' => 'foo=bar; Domain=foo.com'
  144. ]),
  145. new Response(200)
  146. ]);
  147. $handler = HandlerStack::create($mock);
  148. $request = new Request('GET', 'http://foo.com/bar');
  149. $jar = new CookieJar();
  150. $response = $handler($request, [
  151. 'allow_redirects' => true,
  152. 'cookies' => $jar
  153. ])->wait();
  154. $this->assertSame(200, $response->getStatusCode());
  155. $lastRequest = $mock->getLastRequest();
  156. $this->assertSame('http://foo.com/baz', (string) $lastRequest->getUri());
  157. $this->assertSame('foo=bar', $lastRequest->getHeaderLine('Cookie'));
  158. }
  159. private function getFunctions()
  160. {
  161. $calls = [];
  162. $a = function (callable $next) use (&$calls) {
  163. return function ($v) use ($next, &$calls) {
  164. $calls[] = ['a', $v];
  165. return $next($v . '1');
  166. };
  167. };
  168. $b = function (callable $next) use (&$calls) {
  169. return function ($v) use ($next, &$calls) {
  170. $calls[] = ['b', $v];
  171. return $next($v . '2');
  172. };
  173. };
  174. $c = function (callable $next) use (&$calls) {
  175. return function ($v) use ($next, &$calls) {
  176. $calls[] = ['c', $v];
  177. return $next($v . '3');
  178. };
  179. };
  180. $handler = function ($v) {
  181. return 'Hello - ' . $v;
  182. };
  183. return [&$calls, $handler, $a, $b, $c];
  184. }
  185. public static function foo() {}
  186. public function bar () {}
  187. }