Skip to content

Commit ba4fc81

Browse files
committed
Added BaseUriPlugin
1 parent 3fdf49f commit ba4fc81

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

spec/BaseUriPluginSpec.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

src/BaseUriPlugin.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Http\Message\UriFactory;
6+
use Http\Promise\Promise;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class BaseUriPlugin implements Plugin
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $baseUri;
15+
16+
/**
17+
* @var UriFactory
18+
*/
19+
private $factory;
20+
21+
/**
22+
* @param UriFactory $factory
23+
* @param string $baseUri
24+
*/
25+
public function __construct(UriFactory $factory, $baseUri)
26+
{
27+
$this->factory = $factory;
28+
$this->baseUri = $baseUri;
29+
}
30+
31+
/**
32+
* Handle the request and return the response coming from the next callable.
33+
*
34+
* @param RequestInterface $request
35+
* @param callable $next Next middleware in the chain, the request is passed as the first argument
36+
* @param callable $first First middleware in the chain, used to to restart a request
37+
*
38+
* @return Promise
39+
*/
40+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
41+
{
42+
$uri = $request->getUri();
43+
$uriString = $uri->__toString();
44+
if (!strpos($uriString, '://')) {
45+
if (substr($uriString, 0, 1) !== '/') {
46+
// Append baseUri this url
47+
$request = $request->withUri($this->factory->createUri($this->baseUri.$uriString));
48+
} else {
49+
// Replace add/replace path and query to the baseUri
50+
$modifiedUri = $this->factory->createUri($this->baseUri);
51+
$modifiedUri = $modifiedUri->withPath($uri->getPath());
52+
$modifiedUri = $modifiedUri->withQuery($uri->getQuery());
53+
$request = $request->withUri($modifiedUri);
54+
}
55+
}
56+
57+
return $next($request);
58+
}
59+
}

0 commit comments

Comments
 (0)