|
4 | 4 |
|
5 | 5 | use Http\Client\Common\Plugin;
|
6 | 6 | use Http\Client\Common\Plugin\AddPathPlugin;
|
| 7 | +use Http\Client\Common\PluginClient; |
| 8 | +use Http\Client\HttpClient; |
7 | 9 | use Nyholm\Psr7\Request;
|
| 10 | +use Nyholm\Psr7\Response; |
8 | 11 | use Nyholm\Psr7\Uri;
|
9 | 12 | use PHPUnit\Framework\TestCase;
|
10 | 13 | use Psr\Http\Message\RequestInterface;
|
@@ -44,7 +47,7 @@ public function testRewriteSameUrl()
|
44 | 47 | $this->plugin->handleRequest($request, $verify, $this->callable);
|
45 | 48 | }
|
46 | 49 |
|
47 |
| - public function testRewriteOnRestart() |
| 50 | + public function testRewriteCallingThePluginTwice() |
48 | 51 | {
|
49 | 52 | $request = new Request('GET', 'https://example.com/foo');
|
50 | 53 | $this->plugin->handleRequest($request, function (RequestInterface $request) {
|
@@ -82,4 +85,52 @@ public function testRewriteWithDifferentUrlWhenSecondUrlIncludesAddedPath()
|
82 | 85 | $request = new Request('GET', 'https://example.com/api/foo');
|
83 | 86 | $this->plugin->handleRequest($request, $verify, $this->callable);
|
84 | 87 | }
|
| 88 | + |
| 89 | + public function testRewriteWhenPathIsIncluded() |
| 90 | + { |
| 91 | + $verify = function (RequestInterface $request) { |
| 92 | + $this->assertEquals('https://example.com/api/api/foo', $request->getUri()->__toString()); |
| 93 | + }; |
| 94 | + |
| 95 | + $request = new Request('GET', 'https://example.com/api/foo'); |
| 96 | + $this->plugin->handleRequest($request, $verify, $this->callable); |
| 97 | + } |
| 98 | + public function testRewriteOnRestart() |
| 99 | + { |
| 100 | + $secondPlugin = new class implements Plugin { |
| 101 | + private $firstRun = true; |
| 102 | + |
| 103 | + public function handleRequest(RequestInterface $request, callable $next, callable $first) |
| 104 | + { |
| 105 | + if ($this->firstRun) { |
| 106 | + $this->firstRun = false; |
| 107 | + return $first($request)->wait(); |
| 108 | + } |
| 109 | + return $next($request); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + $verify = function (RequestInterface $request) { |
| 114 | + $this->assertEquals('https://example.com/api/foo', $request->getUri()->__toString()); |
| 115 | + }; |
| 116 | + |
| 117 | + $client = new class($verify) implements HttpClient { |
| 118 | + private $callable; |
| 119 | + public function __construct(callable $callable) |
| 120 | + { |
| 121 | + $this->callable = $callable; |
| 122 | + } |
| 123 | + |
| 124 | + public function sendRequest(RequestInterface $request) |
| 125 | + { |
| 126 | + $verify = $this->callable; |
| 127 | + $verify($request); |
| 128 | + return new Response(); |
| 129 | + } |
| 130 | + }; |
| 131 | + |
| 132 | + $pluginClient = new PluginClient($client, [$this->plugin, $secondPlugin]); |
| 133 | + $request = new Request('GET', 'https://example.com/foo'); |
| 134 | + $response = $pluginClient->sendAsyncRequest($request); |
| 135 | + } |
85 | 136 | }
|
0 commit comments