ProxyTest.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace GuzzleHttp\Test\Handler;
  3. use GuzzleHttp\Handler\MockHandler;
  4. use GuzzleHttp\Handler\Proxy;
  5. use GuzzleHttp\Psr7\Request;
  6. use GuzzleHttp\RequestOptions;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @covers \GuzzleHttp\Handler\Proxy
  10. */
  11. class ProxyTest extends TestCase
  12. {
  13. public function testSendsToNonSync()
  14. {
  15. $a = $b = null;
  16. $m1 = new MockHandler([function ($v) use (&$a) { $a = $v; }]);
  17. $m2 = new MockHandler([function ($v) use (&$b) { $b = $v; }]);
  18. $h = Proxy::wrapSync($m1, $m2);
  19. $h(new Request('GET', 'http://foo.com'), []);
  20. $this->assertNotNull($a);
  21. $this->assertNull($b);
  22. }
  23. public function testSendsToSync()
  24. {
  25. $a = $b = null;
  26. $m1 = new MockHandler([function ($v) use (&$a) { $a = $v; }]);
  27. $m2 = new MockHandler([function ($v) use (&$b) { $b = $v; }]);
  28. $h = Proxy::wrapSync($m1, $m2);
  29. $h(new Request('GET', 'http://foo.com'), [RequestOptions::SYNCHRONOUS => true]);
  30. $this->assertNull($a);
  31. $this->assertNotNull($b);
  32. }
  33. public function testSendsToStreaming()
  34. {
  35. $a = $b = null;
  36. $m1 = new MockHandler([function ($v) use (&$a) { $a = $v; }]);
  37. $m2 = new MockHandler([function ($v) use (&$b) { $b = $v; }]);
  38. $h = Proxy::wrapStreaming($m1, $m2);
  39. $h(new Request('GET', 'http://foo.com'), []);
  40. $this->assertNotNull($a);
  41. $this->assertNull($b);
  42. }
  43. public function testSendsToNonStreaming()
  44. {
  45. $a = $b = null;
  46. $m1 = new MockHandler([function ($v) use (&$a) { $a = $v; }]);
  47. $m2 = new MockHandler([function ($v) use (&$b) { $b = $v; }]);
  48. $h = Proxy::wrapStreaming($m1, $m2);
  49. $h(new Request('GET', 'http://foo.com'), ['stream' => true]);
  50. $this->assertNull($a);
  51. $this->assertNotNull($b);
  52. }
  53. }