File tree 3 files changed +94
-1
lines changed
3 files changed +94
-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
+
6
+ use Http \Client \Common \Plugin ;
7
+ use Psr \Http \Message \RequestInterface ;
8
+ use Psr \Http \Message \UriInterface ;
9
+
10
+ /**
11
+ * Add base path to the request URI. Useful for base API URLs like http://domain.com/api
12
+ *
13
+ * @author Sullivan Senechal <[email protected] >
14
+ */
15
+ final class AddPathPlugin implements Plugin
16
+ {
17
+ /**
18
+ * @var UriInterface
19
+ */
20
+ private $ host ;
21
+
22
+ /**
23
+ * @param UriInterface $host
24
+ */
25
+ public function __construct (UriInterface $ host )
26
+ {
27
+ if ($ host ->getPath () === '' ) {
28
+ throw new \LogicException ('Host path can not be empty ' );
29
+ }
30
+
31
+ $ this ->host = $ host ;
32
+ }
33
+
34
+ /**
35
+ * {@inheritdoc}
36
+ */
37
+ public function handleRequest (RequestInterface $ request , callable $ next , callable $ first )
38
+ {
39
+ $ request = $ request ->withUri ($ request ->getUri ()
40
+ ->withPath ($ this ->host ->getPath ().$ request ->getUri ()->getPath ())
41
+ );
42
+
43
+ return $ next ($ request );
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments