Skip to content

Commit fe78690

Browse files
committed
Added AddHostPlugin to modify the host and the schema
1 parent 3d77e7d commit fe78690

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

spec/AddHostPluginSpec.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamInterface;
11+
use Psr\Http\Message\UriInterface;
12+
use PhpSpec\ObjectBehavior;
13+
use Prophecy\Argument;
14+
15+
class AddHostPluginSpec extends ObjectBehavior
16+
{
17+
function let(UriInterface $uri)
18+
{
19+
$this->beConstructedWith($uri);
20+
}
21+
22+
function it_is_initializable(UriInterface $uri)
23+
{
24+
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
25+
$this->beConstructedWith($uri);
26+
$this->shouldHaveType('Http\Client\Plugin\AddHostPlugin');
27+
}
28+
29+
function it_is_a_plugin(UriInterface $uri)
30+
{
31+
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
32+
$this->beConstructedWith($uri);
33+
34+
$this->shouldImplement('Http\Client\Plugin\Plugin');
35+
}
36+
37+
function it_adds_domain(
38+
RequestInterface $request,
39+
UriInterface $host,
40+
UriInterface $uri
41+
) {
42+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
43+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
44+
45+
$request->getUri()->shouldBeCalled()->willReturn($uri);
46+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
47+
48+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
49+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
50+
$uri->getHost()->shouldBeCalled()->willReturn('');
51+
52+
$this->beConstructedWith($host);
53+
$this->handleRequest($request, function () {}, function () {});
54+
}
55+
56+
function it_replaces_domain(
57+
RequestInterface $request,
58+
UriInterface $host,
59+
UriInterface $uri
60+
) {
61+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
62+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
63+
64+
$request->getUri()->shouldBeCalled()->willReturn($uri);
65+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
66+
67+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
68+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
69+
70+
71+
$this->beConstructedWith($host, ['replace'=>true]);
72+
$this->handleRequest($request, function () {}, function () {});
73+
}
74+
75+
function it_does_nothing_when_domain_exists(
76+
RequestInterface $request,
77+
UriInterface $host,
78+
UriInterface $uri
79+
) {
80+
$request->getUri()->shouldBeCalled()->willReturn($uri);
81+
$uri->getHost()->shouldBeCalled()->willReturn('default.com');
82+
83+
$this->beConstructedWith($host);
84+
$this->handleRequest($request, function () {}, function () {});
85+
}
86+
}

src/AddHostPlugin.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\UriInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
/**
10+
* Add schema and host to a request. Can be set to overwrite the schema and host if desired.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class AddHostPlugin implements Plugin
15+
{
16+
/**
17+
* @var UriInterface
18+
*/
19+
private $host;
20+
21+
/**
22+
* @var bool
23+
*/
24+
private $replace;
25+
26+
/**
27+
* @param UriInterface $host
28+
* @param array $config {
29+
* @var bool $replace True will replace all hosts, false will only add host when none is specified.
30+
* }
31+
*/
32+
public function __construct(UriInterface $host, array $config = [])
33+
{
34+
if ($host->getHost() === '') {
35+
throw new \LogicException('Host can not be empty');
36+
}
37+
38+
$this->host = $host;
39+
40+
$resolver = new OptionsResolver();
41+
$this->configureOptions($resolver);
42+
$options = $resolver->resolve($config);
43+
44+
$this->replace = $options['replace'];
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
51+
{
52+
if ($this->replace || $request->getUri()->getHost() === '') {
53+
$uri = $request->getUri()->withHost($this->host->getHost());
54+
$uri = $uri->withScheme($this->host->getScheme());
55+
56+
$request = $request->withUri($uri);
57+
}
58+
59+
return $next($request);
60+
}
61+
62+
/**
63+
* @param OptionsResolver $resolver
64+
*/
65+
private function configureOptions(OptionsResolver $resolver)
66+
{
67+
$resolver->setDefaults([
68+
'replace' => false,
69+
]);
70+
$resolver->setAllowedTypes('replace', 'bool');
71+
}
72+
}

0 commit comments

Comments
 (0)