|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Common; |
| 4 | + |
| 5 | +use Http\Client\Exception; |
| 6 | +use Http\Client\Exception\TransferException; |
| 7 | +use Http\Client\HttpAsyncClient; |
| 8 | +use Http\Client\HttpClient; |
| 9 | +use Http\Promise\Promise; |
| 10 | +use Http\Promise\RejectedPromise; |
| 11 | +use PhpSpec\ObjectBehavior; |
| 12 | +use Prophecy\Argument; |
| 13 | +use Psr\Http\Message\RequestInterface; |
| 14 | +use Psr\Http\Message\ResponseInterface; |
| 15 | + |
| 16 | +class HttpClientPoolItemSpec extends ObjectBehavior |
| 17 | +{ |
| 18 | + public function let(HttpClient $httpClient) |
| 19 | + { |
| 20 | + $this->beConstructedWith($httpClient); |
| 21 | + } |
| 22 | + |
| 23 | + public function it_is_an_http_client() |
| 24 | + { |
| 25 | + $this->shouldImplement('Http\Client\HttpClient'); |
| 26 | + } |
| 27 | + |
| 28 | + public function it_is_an_async_http_client() |
| 29 | + { |
| 30 | + $this->shouldImplement('Http\Client\HttpAsyncClient'); |
| 31 | + } |
| 32 | + |
| 33 | + public function it_sends_request(HttpClient $httpClient, RequestInterface $request, ResponseInterface $response) |
| 34 | + { |
| 35 | + $httpClient->sendRequest($request)->willReturn($response); |
| 36 | + |
| 37 | + $this->sendRequest($request)->shouldReturn($response); |
| 38 | + } |
| 39 | + |
| 40 | + public function it_sends_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request, Promise $promise) |
| 41 | + { |
| 42 | + $this->beConstructedWith($httpAsyncClient); |
| 43 | + |
| 44 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 45 | + $promise->then(Argument::type('callable'), Argument::type('callable'))->willReturn($promise); |
| 46 | + |
| 47 | + $this->sendAsyncRequest($request)->shouldReturn($promise); |
| 48 | + } |
| 49 | + |
| 50 | + public function it_disable_himself_on_send_request(HttpClient $httpClient, RequestInterface $request) |
| 51 | + { |
| 52 | + $exception = new TransferException(); |
| 53 | + $httpClient->sendRequest($request)->willThrow($exception); |
| 54 | + $this->shouldThrow($exception)->duringSendRequest($request); |
| 55 | + $this->isDisabled()->shouldReturn(true); |
| 56 | + $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendRequest($request); |
| 57 | + } |
| 58 | + |
| 59 | + public function it_disable_himself_on_send_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request) |
| 60 | + { |
| 61 | + $this->beConstructedWith($httpAsyncClient); |
| 62 | + |
| 63 | + $promise = new RejectedPromise(new TransferException()); |
| 64 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 65 | + |
| 66 | + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); |
| 67 | + $this->isDisabled()->shouldReturn(true); |
| 68 | + $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request); |
| 69 | + } |
| 70 | + |
| 71 | + public function it_reactivate_himself_on_send_request(HttpClient $httpClient, RequestInterface $request) |
| 72 | + { |
| 73 | + $this->beConstructedWith($httpClient, 0); |
| 74 | + |
| 75 | + $exception = new TransferException(); |
| 76 | + $httpClient->sendRequest($request)->willThrow($exception); |
| 77 | + |
| 78 | + $this->shouldThrow($exception)->duringSendRequest($request); |
| 79 | + $this->isDisabled()->shouldReturn(false); |
| 80 | + $this->shouldThrow($exception)->duringSendRequest($request); |
| 81 | + } |
| 82 | + |
| 83 | + public function it_reactivate_himself_on_send_async_request(HttpAsyncClient $httpAsyncClient, RequestInterface $request) |
| 84 | + { |
| 85 | + $this->beConstructedWith($httpAsyncClient, 0); |
| 86 | + |
| 87 | + $promise = new RejectedPromise(new TransferException()); |
| 88 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 89 | + |
| 90 | + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); |
| 91 | + $this->isDisabled()->shouldReturn(false); |
| 92 | + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); |
| 93 | + } |
| 94 | + |
| 95 | + public function it_increments_request_count(HttpAsyncClient $httpAsyncClient, RequestInterface $request, ResponseInterface $response) |
| 96 | + { |
| 97 | + $this->beConstructedWith($httpAsyncClient, 0); |
| 98 | + |
| 99 | + $promise = new NotResolvingPromise($response->getWrappedObject()); |
| 100 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 101 | + |
| 102 | + $this->getSendingRequestCount()->shouldReturn(0); |
| 103 | + $this->sendAsyncRequest($request)->shouldReturn($promise); |
| 104 | + $this->getSendingRequestCount()->shouldReturn(1); |
| 105 | + $this->sendAsyncRequest($request)->shouldReturn($promise); |
| 106 | + $this->getSendingRequestCount()->shouldReturn(2); |
| 107 | + } |
| 108 | + |
| 109 | + public function it_decrements_request_count(HttpAsyncClient $httpAsyncClient, RequestInterface $request, ResponseInterface $response) |
| 110 | + { |
| 111 | + $this->beConstructedWith($httpAsyncClient, 0); |
| 112 | + |
| 113 | + $promise = new NotResolvingPromise($response->getWrappedObject()); |
| 114 | + $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); |
| 115 | + |
| 116 | + $this->getSendingRequestCount()->shouldReturn(0); |
| 117 | + $this->sendAsyncRequest($request)->shouldReturn($promise); |
| 118 | + $this->getSendingRequestCount()->shouldReturn(1); |
| 119 | + |
| 120 | + $promise->wait(false); |
| 121 | + |
| 122 | + $this->getSendingRequestCount()->shouldReturn(0); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class NotResolvingPromise implements Promise |
| 127 | +{ |
| 128 | + private $queue = []; |
| 129 | + |
| 130 | + private $state = Promise::PENDING; |
| 131 | + |
| 132 | + private $response; |
| 133 | + |
| 134 | + private $exception; |
| 135 | + |
| 136 | + public function __construct(ResponseInterface $response = null, Exception $exception = null) |
| 137 | + { |
| 138 | + $this->response = $response; |
| 139 | + $this->exception = $exception; |
| 140 | + } |
| 141 | + |
| 142 | + public function then(callable $onFulfilled = null, callable $onRejected = null) |
| 143 | + { |
| 144 | + $this->queue[] = [ |
| 145 | + $onFulfilled, |
| 146 | + $onRejected, |
| 147 | + ]; |
| 148 | + |
| 149 | + return $this; |
| 150 | + } |
| 151 | + |
| 152 | + public function getState() |
| 153 | + { |
| 154 | + return $this->state; |
| 155 | + } |
| 156 | + |
| 157 | + public function wait($unwrap = true) |
| 158 | + { |
| 159 | + if ($this->state === Promise::FULFILLED) { |
| 160 | + if (!$unwrap) { |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + return $this->response; |
| 165 | + } |
| 166 | + |
| 167 | + if ($this->state === Promise::REJECTED) { |
| 168 | + if (!$unwrap) { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + throw $this->exception; |
| 173 | + } |
| 174 | + |
| 175 | + while (count($this->queue) > 0) { |
| 176 | + $callbacks = array_shift($this->queue); |
| 177 | + |
| 178 | + if ($this->response !== null) { |
| 179 | + try { |
| 180 | + $this->response = $callbacks[0]($this->response); |
| 181 | + $this->exception = null; |
| 182 | + } catch (Exception $exception) { |
| 183 | + $this->response = null; |
| 184 | + $this->exception = $exception; |
| 185 | + } |
| 186 | + } elseif ($this->exception !== null) { |
| 187 | + try { |
| 188 | + $this->response = $callbacks[1]($this->exception); |
| 189 | + $this->exception = null; |
| 190 | + } catch (Exception $exception) { |
| 191 | + $this->response = null; |
| 192 | + $this->exception = $exception; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if ($this->response !== null) { |
| 198 | + $this->state = Promise::FULFILLED; |
| 199 | + |
| 200 | + if ($unwrap) { |
| 201 | + return $this->response; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if ($this->exception !== null) { |
| 206 | + $this->state = Promise::REJECTED; |
| 207 | + |
| 208 | + if ($unwrap) { |
| 209 | + throw $this->exception; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments