File tree Expand file tree Collapse file tree 3 files changed +93
-1
lines changed Expand file tree Collapse file tree 3 files changed +93
-1
lines changed Original file line number Diff line number Diff line change 8
8
- Fix Emulated Trait to use Http based promise which respect the HttpAsyncClient interface
9
9
- Require Httplug 1.1 where we use HTTP specific promises.
10
10
- RedirectPlugin: use the full URL instead of the URI to properly keep track of redirects
11
-
11
+ - Add AddPathPlugin for API URLs with base path
12
12
13
13
## 1.2.1 - 2016-07-26
14
14
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace spec \Http \Client \Common \Plugin ;
4
+
5
+ use Http \Message \StreamFactory ;
6
+ use Http \Message \UriFactory ;
7
+ use Psr \Http \Message \RequestInterface ;
8
+ use Psr \Http \Message \UriInterface ;
9
+ use PhpSpec \ObjectBehavior ;
10
+
11
+ class AddPathPluginSpec extends ObjectBehavior
12
+ {
13
+ function let (UriInterface $ uri )
14
+ {
15
+ $ this ->beConstructedWith ($ uri );
16
+ }
17
+
18
+ function it_is_initializable (UriInterface $ uri )
19
+ {
20
+ $ uri ->getPath ()->shouldBeCalled ()->willReturn ('/api ' );
21
+
22
+ $ this ->shouldHaveType ('Http\Client\Common\Plugin\AddPathPlugin ' );
23
+ }
24
+
25
+ function it_is_a_plugin (UriInterface $ uri )
26
+ {
27
+ $ uri ->getPath ()->shouldBeCalled ()->willReturn ('/api ' );
28
+
29
+ $ this ->shouldImplement ('Http\Client\Common\Plugin ' );
30
+ }
31
+
32
+ function it_adds_path (
33
+ RequestInterface $ request ,
34
+ UriInterface $ host ,
35
+ UriInterface $ uri
36
+ ) {
37
+ $ host ->getPath ()->shouldBeCalled ()->willReturn ('/api ' );
38
+
39
+ $ request ->getUri ()->shouldBeCalled ()->willReturn ($ uri );
40
+ $ request ->withUri ($ uri )->shouldBeCalled ()->willReturn ($ request );
41
+
42
+ $ uri ->withPath ('/api/users ' )->shouldBeCalled ()->willReturn ($ uri );
43
+ $ uri ->getPath ()->shouldBeCalled ()->willReturn ('/users ' );
44
+
45
+ $ this ->beConstructedWith ($ host );
46
+ $ this ->handleRequest ($ request , function () {}, function () {});
47
+ }
48
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Http \Client \Common \Plugin ;
4
+
5
+ use Http \Client \Common \Plugin ;
6
+ use Psr \Http \Message \RequestInterface ;
7
+ use Psr \Http \Message \UriInterface ;
8
+
9
+ /**
10
+ * Add base path to the request URI. Useful for base API URLs like http://domain.com/api.
11
+ *
12
+ * @author Sullivan Senechal <[email protected] >
13
+ */
14
+ final class AddPathPlugin implements Plugin
15
+ {
16
+ /**
17
+ * @var UriInterface
18
+ */
19
+ private $ host ;
20
+
21
+ /**
22
+ * @param UriInterface $host
23
+ */
24
+ public function __construct (UriInterface $ host )
25
+ {
26
+ if ($ host ->getPath () === '' ) {
27
+ throw new \LogicException ('Host path can not be empty ' );
28
+ }
29
+
30
+ $ this ->host = $ host ;
31
+ }
32
+
33
+ /**
34
+ * {@inheritdoc}
35
+ */
36
+ public function handleRequest (RequestInterface $ request , callable $ next , callable $ first )
37
+ {
38
+ $ request = $ request ->withUri ($ request ->getUri ()
39
+ ->withPath ($ this ->host ->getPath ().$ request ->getUri ()->getPath ())
40
+ );
41
+
42
+ return $ next ($ request );
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments