14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \HttpClient \Exception \InvalidArgumentException ;
16
16
use Symfony \Component \HttpClient \MockHttpClient ;
17
- use Symfony \Component \HttpClient \Response \MockResponse ;
18
17
use Symfony \Component \HttpClient \ScopingHttpClient ;
19
18
20
19
class ScopingHttpClientTest extends TestCase
21
20
{
22
21
public function testRelativeUrl ()
23
22
{
24
- $ mockClient = new MockHttpClient ([] );
23
+ $ mockClient = new MockHttpClient ();
25
24
$ client = new ScopingHttpClient ($ mockClient , []);
26
25
27
26
$ this ->expectException (InvalidArgumentException::class);
@@ -30,7 +29,7 @@ public function testRelativeUrl()
30
29
31
30
public function testRelativeUrlWithDefaultRegexp ()
32
31
{
33
- $ mockClient = new MockHttpClient (new MockResponse () );
32
+ $ mockClient = new MockHttpClient ();
34
33
$ client = new ScopingHttpClient ($ mockClient , ['.* ' => ['base_uri ' => 'http://example.com ' ]], '.* ' );
35
34
36
35
$ this ->assertSame ('http://example.com/foo ' , $ client ->request ('GET ' , '/foo ' )->getInfo ('url ' ));
@@ -41,7 +40,7 @@ public function testRelativeUrlWithDefaultRegexp()
41
40
*/
42
41
public function testMatchingUrls (string $ regexp , string $ url , array $ options )
43
42
{
44
- $ mockClient = new MockHttpClient (new MockResponse () );
43
+ $ mockClient = new MockHttpClient ();
45
44
$ client = new ScopingHttpClient ($ mockClient , $ options );
46
45
47
46
$ response = $ client ->request ('GET ' , $ url );
@@ -69,13 +68,7 @@ public function testMatchingUrlsAndOptions()
69
68
'.* ' => ['headers ' => ['content-type ' => 'text/html ' ]],
70
69
];
71
70
72
- $ mockResponses = [
73
- new MockResponse (),
74
- new MockResponse (),
75
- new MockResponse (),
76
- ];
77
-
78
- $ mockClient = new MockHttpClient ($ mockResponses );
71
+ $ mockClient = new MockHttpClient ();
79
72
$ client = new ScopingHttpClient ($ mockClient , $ defaultOptions );
80
73
81
74
$ response = $ client ->request ('GET ' , 'http://example.com/foo-bar ' , ['json ' => ['url ' => 'http://example.com ' ]]);
@@ -93,4 +86,16 @@ public function testMatchingUrlsAndOptions()
93
86
$ this ->assertEquals ($ requestOptions ['headers ' ]['x-app ' ][0 ], 'unit-test ' );
94
87
$ this ->assertEquals ($ requestOptions ['headers ' ]['content-type ' ][0 ], 'text/html ' );
95
88
}
89
+
90
+ public function testForBaseUri ()
91
+ {
92
+ $ client = ScopingHttpClient::forBaseUri (new MockHttpClient (), 'http://example.com/foo ' );
93
+
94
+ $ response = $ client ->request ('GET ' , '/bar ' );
95
+ $ this ->assertSame ('http://example.com/foo ' , implode ('' , $ response ->getRequestOptions ()['base_uri ' ]));
96
+ $ this ->assertSame ('http://example.com/bar ' , $ response ->getInfo ('url ' ));
97
+
98
+ $ response = $ client ->request ('GET ' , 'http://foo.bar/ ' );
99
+ $ this ->assertNull ($ response ->getRequestOptions ()['base_uri ' ]);
100
+ }
96
101
}
0 commit comments