-
Notifications
You must be signed in to change notification settings - Fork 6
AddHostPlugin #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AddHostPlugin #42
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Plugin; | ||
|
||
use Http\Message\StreamFactory; | ||
use Http\Message\UriFactory; | ||
use Http\Promise\FulfilledPromise; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class AddHostPluginSpec extends ObjectBehavior | ||
{ | ||
function let(UriInterface $uri) | ||
{ | ||
$this->beConstructedWith($uri); | ||
} | ||
|
||
function it_is_initializable(UriInterface $uri) | ||
{ | ||
$uri->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$this->beConstructedWith($uri); | ||
$this->shouldHaveType('Http\Client\Plugin\AddHostPlugin'); | ||
} | ||
|
||
function it_is_a_plugin(UriInterface $uri) | ||
{ | ||
$uri->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$this->beConstructedWith($uri); | ||
|
||
$this->shouldImplement('Http\Client\Plugin\Plugin'); | ||
} | ||
|
||
function it_adds_domain( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getScheme()->shouldBeCalled()->willReturn('http://'); | ||
$host->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); | ||
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); | ||
$uri->getHost()->shouldBeCalled()->willReturn(''); | ||
|
||
$this->beConstructedWith($host); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_replaces_domain( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getScheme()->shouldBeCalled()->willReturn('http://'); | ||
$host->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); | ||
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); | ||
|
||
|
||
$this->beConstructedWith($host, ['replace'=>true]); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_nothing_when_domain_exists( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$uri->getHost()->shouldBeCalled()->willReturn('default.com'); | ||
|
||
$this->beConstructedWith($host); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Http\Client\Plugin; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Add schema and host to a request. Can be set to overwrite the schema and host if desired. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class AddHostPlugin implements Plugin | ||
{ | ||
/** | ||
* @var UriInterface | ||
*/ | ||
private $host; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $replace; | ||
|
||
/** | ||
* @param UriInterface $host | ||
* @param array $config { | ||
* | ||
* @var bool $replace True will replace all hosts, false will only add host when none is specified. | ||
* } | ||
*/ | ||
public function __construct(UriInterface $host, array $config = []) | ||
{ | ||
if ($host->getHost() === '') { | ||
throw new \LogicException('Host can not be empty'); | ||
} | ||
|
||
$this->host = $host; | ||
|
||
$resolver = new OptionsResolver(); | ||
$this->configureOptions($resolver); | ||
$options = $resolver->resolve($config); | ||
|
||
$this->replace = $options['replace']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
if ($this->replace || $request->getUri()->getHost() === '') { | ||
$uri = $request->getUri()->withHost($this->host->getHost()); | ||
$uri = $uri->withScheme($this->host->getScheme()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is $this->host->getScheme() is empty? does this set the scheme to the default scheme? we validate the host above, but not scheme. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the PSR7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is this the wanted behavior, replace / remove existing if scheme is null (i think it's OK ?) |
||
|
||
$request = $request->withUri($uri); | ||
} | ||
|
||
return $next($request); | ||
} | ||
|
||
/** | ||
* @param OptionsResolver $resolver | ||
*/ | ||
private function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'replace' => false, | ||
]); | ||
$resolver->setAllowedTypes('replace', 'bool'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dbu @joelwurtz Did you mean like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍