|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace spec\Http\Client\Plugin; |
| 4 | + |
| 5 | +use Http\Message\StreamFactory; |
| 6 | +use Http\Message\UriFactory; |
| 7 | +use Http\Promise\FulfilledPromise; |
| 8 | +use PhpSpec\ObjectBehavior; |
| 9 | +use Prophecy\Argument; |
| 10 | +use Psr\Cache\CacheItemInterface; |
| 11 | +use Psr\Cache\CacheItemPoolInterface; |
| 12 | +use Psr\Http\Message\RequestInterface; |
| 13 | +use Psr\Http\Message\ResponseInterface; |
| 14 | +use Psr\Http\Message\StreamInterface; |
| 15 | +use Psr\Http\Message\UriInterface; |
| 16 | + |
| 17 | +class BaseUriPluginSpec extends ObjectBehavior |
| 18 | +{ |
| 19 | + function let(UriFactory $uriFactory) |
| 20 | + { |
| 21 | + $this->beConstructedWith($uriFactory, 'http://httplug.io/'); |
| 22 | + } |
| 23 | + |
| 24 | + function it_is_initializable() |
| 25 | + { |
| 26 | + $this->shouldHaveType('Http\Client\Plugin\BaseUriPlugin'); |
| 27 | + } |
| 28 | + |
| 29 | + function it_is_a_plugin() |
| 30 | + { |
| 31 | + $this->shouldImplement('Http\Client\Plugin\Plugin'); |
| 32 | + } |
| 33 | + |
| 34 | + function it_appends_base_uri( |
| 35 | + UriFactory $uriFactory, |
| 36 | + RequestInterface $request, |
| 37 | + ResponseInterface $response, |
| 38 | + UriInterface $uri, |
| 39 | + RequestInterface $modifiedRequest, |
| 40 | + UriInterface $modifiedUri |
| 41 | + ) { |
| 42 | + $uriFactory->createUri('http://httplug.io/bar')->willReturn($modifiedUri); |
| 43 | + $this->beConstructedWith($uriFactory, 'http://httplug.io/'); |
| 44 | + |
| 45 | + $request->getUri()->shouldBeCalled()->willReturn($uri); |
| 46 | + $uri->__toString()->shouldBeCalled()->willReturn('bar'); |
| 47 | + $request->withUri($modifiedUri)->shouldBeCalled()->willReturn($modifiedRequest); |
| 48 | + $modifiedRequest->getUri()->willReturn($modifiedUri); |
| 49 | + |
| 50 | + $next = function (RequestInterface $receivedRequest) use($modifiedUri, $response) { |
| 51 | + if (Argument::is($modifiedUri->getWrappedObject())->scoreArgument($receivedRequest->getUri())) { |
| 52 | + return new FulfilledPromise($response->getWrappedObject()); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + $finalPromise = $this->handleRequest($request, $next, function () {}); |
| 57 | + $finalPromise->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); |
| 58 | + } |
| 59 | + |
| 60 | + function it_replaces_path_on_base_uri( |
| 61 | + UriFactory $uriFactory, |
| 62 | + RequestInterface $request, |
| 63 | + ResponseInterface $response, |
| 64 | + UriInterface $uri, |
| 65 | + RequestInterface $modifiedRequest, |
| 66 | + UriInterface $modifiedUri |
| 67 | + ) { |
| 68 | + $baseUri = 'http://httplug.io/'; |
| 69 | + $uriFactory->createUri($baseUri)->willReturn($modifiedUri); |
| 70 | + $this->beConstructedWith($uriFactory, $baseUri); |
| 71 | + |
| 72 | + $request->getUri()->shouldBeCalled()->willReturn($uri); |
| 73 | + $uri->__toString()->shouldBeCalled()->willReturn('/foo'); |
| 74 | + $uri->getPath()->shouldBeCalled()->willReturn('/foo'); |
| 75 | + $uri->getQuery()->shouldBeCalled()->willReturn(''); |
| 76 | + |
| 77 | + $modifiedUri->withPath('/foo')->shouldBeCalled()->willReturn($modifiedUri); |
| 78 | + $modifiedUri->withQuery('')->shouldBeCalled()->willReturn($modifiedUri); |
| 79 | + |
| 80 | + $request->withUri($modifiedUri)->shouldBeCalled()->willReturn($modifiedRequest); |
| 81 | + $modifiedRequest->getUri()->willReturn($modifiedUri); |
| 82 | + |
| 83 | + $next = function (RequestInterface $receivedRequest) use($modifiedUri, $response) { |
| 84 | + if (Argument::is($modifiedUri->getWrappedObject())->scoreArgument($receivedRequest->getUri())) { |
| 85 | + return new FulfilledPromise($response->getWrappedObject()); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + $finalPromise = $this->handleRequest($request, $next, function () {}); |
| 90 | + $finalPromise->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); |
| 91 | + } |
| 92 | + |
| 93 | + function it_ignores_absolute_url(RequestInterface $request, UriInterface $uri) |
| 94 | + { |
| 95 | + $request->getUri()->shouldBeCalled()->willReturn($uri); |
| 96 | + $uri->__toString()->shouldBeCalled()->willReturn('http://foo.io/bar'); |
| 97 | + |
| 98 | + $this->handleRequest($request, function () {}, function () {}); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments