|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace tests\Http\Client\Common\Plugin; |
| 4 | + |
| 5 | +use Http\Client\Common\Plugin; |
| 6 | +use Http\Client\Common\Plugin\AddPathPlugin; |
| 7 | +use Http\Client\Common\PluginClient; |
| 8 | +use Http\Client\HttpClient; |
| 9 | +use Nyholm\Psr7\Request; |
| 10 | +use Nyholm\Psr7\Response; |
| 11 | +use Nyholm\Psr7\Uri; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | + |
| 15 | +class AddPathPluginTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var Plugin |
| 19 | + */ |
| 20 | + private $plugin; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var callable empty |
| 24 | + */ |
| 25 | + private $first; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->first = function () {}; |
| 30 | + $this->plugin = new AddPathPlugin(new Uri('/api')); |
| 31 | + } |
| 32 | + |
| 33 | + public function testRewriteSameUrl() |
| 34 | + { |
| 35 | + $verify = function (RequestInterface $request) { |
| 36 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 37 | + }; |
| 38 | + |
| 39 | + $request = new Request('GET', 'https://example.com/foo', ['Content-Type'=>'text/html']); |
| 40 | + $this->plugin->handleRequest($request, $verify, $this->first); |
| 41 | + |
| 42 | + // Make a second call with the same $request object |
| 43 | + $this->plugin->handleRequest($request, $verify, $this->first); |
| 44 | + |
| 45 | + // Make a new call with a new object but same URL |
| 46 | + $request = new Request('GET', 'https://example.com/foo', ['Content-Type'=>'text/plain']); |
| 47 | + $this->plugin->handleRequest($request, $verify, $this->first); |
| 48 | + } |
| 49 | + |
| 50 | + public function testRewriteCallingThePluginTwice() |
| 51 | + { |
| 52 | + $request = new Request('GET', 'https://example.com/foo'); |
| 53 | + $this->plugin->handleRequest($request, function (RequestInterface $request) { |
| 54 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 55 | + |
| 56 | + // Run the plugin again with the modified request |
| 57 | + $this->plugin->handleRequest($request, function (RequestInterface $request) { |
| 58 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 59 | + }, $this->first); |
| 60 | + }, $this->first); |
| 61 | + } |
| 62 | + |
| 63 | + public function testRewriteWithDifferentUrl() |
| 64 | + { |
| 65 | + $request = new Request('GET', 'https://example.com/foo'); |
| 66 | + $this->plugin->handleRequest($request, function (RequestInterface $request) { |
| 67 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 68 | + }, $this->first); |
| 69 | + |
| 70 | + $request = new Request('GET', 'https://example.com/bar'); |
| 71 | + $this->plugin->handleRequest($request, function (RequestInterface $request) { |
| 72 | + $this->assertEquals('https://example.com/api/bar', $request->getUri()->__toString()); |
| 73 | + }, $this->first); |
| 74 | + } |
| 75 | + |
| 76 | + public function testRewriteWhenPathIsIncluded() |
| 77 | + { |
| 78 | + $verify = function (RequestInterface $request) { |
| 79 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 80 | + }; |
| 81 | + |
| 82 | + $request = new Request('GET', 'https://example.com/api/foo'); |
| 83 | + $this->plugin->handleRequest($request, $verify, $this->first); |
| 84 | + } |
| 85 | +} |
0 commit comments