@@ -75,7 +75,7 @@ public function testExcludeByIp(string $ipAddr, $subnets, bool $mustThrow)
7575 $ this ->expectExceptionMessage (sprintf ('IP "%s" is blocked for "%s". ' , $ ipAddr , $ url ));
7676 }
7777
78- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
78+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
7979 $ client = new NoPrivateNetworkHttpClient ($ previousHttpClient , $ subnets );
8080 $ response = $ client ->request ('GET ' , $ url );
8181
@@ -91,14 +91,15 @@ public function testExcludeByIp(string $ipAddr, $subnets, bool $mustThrow)
9191 public function testExcludeByHost (string $ ipAddr , $ subnets , bool $ mustThrow )
9292 {
9393 $ content = 'foo ' ;
94- $ url = sprintf ('http://%s/ ' , str_contains ($ ipAddr , ': ' ) ? sprintf ('[%s] ' , $ ipAddr ) : $ ipAddr );
94+ $ host = str_contains ($ ipAddr , ': ' ) ? sprintf ('[%s] ' , $ ipAddr ) : $ ipAddr ;
95+ $ url = sprintf ('http://%s/ ' , $ host );
9596
9697 if ($ mustThrow ) {
9798 $ this ->expectException (TransportException::class);
98- $ this ->expectExceptionMessage (sprintf ('Host "%s" is blocked for "%s". ' , $ ipAddr , $ url ));
99+ $ this ->expectExceptionMessage (sprintf ('Host "%s" is blocked for "%s". ' , $ host , $ url ));
99100 }
100101
101- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
102+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
102103 $ client = new NoPrivateNetworkHttpClient ($ previousHttpClient , $ subnets );
103104 $ response = $ client ->request ('GET ' , $ url );
104105
@@ -119,7 +120,7 @@ public function testCustomOnProgressCallback()
119120 ++$ executionCount ;
120121 };
121122
122- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
123+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
123124 $ client = new NoPrivateNetworkHttpClient ($ previousHttpClient );
124125 $ response = $ client ->request ('GET ' , $ url , ['on_progress ' => $ customCallback ]);
125126
@@ -132,7 +133,6 @@ public function testNonCallableOnProgressCallback()
132133 {
133134 $ ipAddr = '104.26.14.6 ' ;
134135 $ url = sprintf ('http://%s/ ' , $ ipAddr );
135- $ content = 'bar ' ;
136136 $ customCallback = sprintf ('cb_%s ' , microtime (true ));
137137
138138 $ this ->expectException (InvalidArgumentException::class);
@@ -150,38 +150,8 @@ public function testConstructor()
150150 new NoPrivateNetworkHttpClient (new MockHttpClient (), 3 );
151151 }
152152
153- private function getHttpClientMock ( string $ url , string $ ipAddr , string $ content )
153+ private function getMockHttpClient ( string $ ipAddr , string $ content )
154154 {
155- $ previousHttpClient = $ this
156- ->getMockBuilder (HttpClientInterface::class)
157- ->getMock ();
158-
159- $ previousHttpClient
160- ->expects ($ this ->once ())
161- ->method ('request ' )
162- ->with (
163- 'GET ' ,
164- $ url ,
165- $ this ->callback (function ($ options ) {
166- $ this ->assertArrayHasKey ('on_progress ' , $ options );
167- $ onProgress = $ options ['on_progress ' ];
168- $ this ->assertIsCallable ($ onProgress );
169-
170- return true ;
171- })
172- )
173- ->willReturnCallback (function ($ method , $ url , $ options ) use ($ ipAddr , $ content ): ResponseInterface {
174- $ info = [
175- 'primary_ip ' => $ ipAddr ,
176- 'url ' => $ url ,
177- ];
178-
179- $ onProgress = $ options ['on_progress ' ];
180- $ onProgress (0 , 0 , $ info );
181-
182- return MockResponse::fromRequest ($ method , $ url , [], new MockResponse ($ content ));
183- });
184-
185- return $ previousHttpClient ;
155+ return new MockHttpClient (new MockResponse ($ content , ['primary_ip ' => $ ipAddr ]));
186156 }
187157}
0 commit comments