Skip to content

Commit 3c94066

Browse files
committed
The AddHost plugin should take care of the base path
With the following PHP code example: ```php $httpClient = new HttpMethodsClient( new PluginClient(HttpClientDiscovery::find(), [ new AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://logs.domain.com/api')), new ErrorPlugin(), ]), MessageFactoryDiscovery::find() ); $httpClient->get('/users'); ``` The https://logs.domain.com/users URL will be called instead of /api/users. The base path should be kept on this plugin.
1 parent 4493b10 commit 3c94066

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
- Fix Emulated Trait to use Http based promise which respect the HttpAsyncClient interface
1313
- RedirectPlugin: use the full URL instead of the URI to properly keep track of redirects
14-
14+
- AddHostPlugin now keeps the base path of the URI
1515

1616
## 1.2.1 - 2016-07-26
1717

spec/Plugin/AddHostPluginSpec.php

+6
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ function it_adds_domain(
3737
$host->getScheme()->shouldBeCalled()->willReturn('http://');
3838
$host->getHost()->shouldBeCalled()->willReturn('example.com');
3939
$host->getPort()->shouldBeCalled()->willReturn(8000);
40+
$host->getPath()->shouldBeCalled()->willReturn('/api');
4041

4142
$request->getUri()->shouldBeCalled()->willReturn($uri);
4243
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
4344

4445
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
4546
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
4647
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
48+
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
4749
$uri->getHost()->shouldBeCalled()->willReturn('');
50+
$uri->getPath()->shouldBeCalled()->willReturn('/users');
4851

4952
$this->beConstructedWith($host);
5053
$this->handleRequest($request, function () {}, function () {});
@@ -58,13 +61,16 @@ function it_replaces_domain(
5861
$host->getScheme()->shouldBeCalled()->willReturn('http://');
5962
$host->getHost()->shouldBeCalled()->willReturn('example.com');
6063
$host->getPort()->shouldBeCalled()->willReturn(8000);
64+
$host->getPath()->shouldBeCalled()->willReturn('/api');
6165

6266
$request->getUri()->shouldBeCalled()->willReturn($uri);
6367
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
6468

6569
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
6670
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
6771
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
72+
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
73+
$uri->getPath()->shouldBeCalled()->willReturn('/users');
6874

6975
$this->beConstructedWith($host, ['replace' => true]);
7076
$this->handleRequest($request, function () {}, function () {});

src/Plugin/AddHostPlugin.php

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
5656
->withHost($this->host->getHost())
5757
->withScheme($this->host->getScheme())
5858
->withPort($this->host->getPort())
59+
->withPath($this->host->getPath().$request->getUri()->getPath())
5960
;
6061

6162
$request = $request->withUri($uri);

0 commit comments

Comments
 (0)