@@ -75,7 +75,7 @@ public function testExcludeByIp(string $ipAddr, $subnets, bool $mustThrow)
75
75
$ this ->expectExceptionMessage (sprintf ('IP "%s" is blocked for "%s". ' , $ ipAddr , $ url ));
76
76
}
77
77
78
- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
78
+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
79
79
$ client = new NoPrivateNetworkHttpClient ($ previousHttpClient , $ subnets );
80
80
$ response = $ client ->request ('GET ' , $ url );
81
81
@@ -91,14 +91,15 @@ public function testExcludeByIp(string $ipAddr, $subnets, bool $mustThrow)
91
91
public function testExcludeByHost (string $ ipAddr , $ subnets , bool $ mustThrow )
92
92
{
93
93
$ 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 );
95
96
96
97
if ($ mustThrow ) {
97
98
$ 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 ));
99
100
}
100
101
101
- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
102
+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
102
103
$ client = new NoPrivateNetworkHttpClient ($ previousHttpClient , $ subnets );
103
104
$ response = $ client ->request ('GET ' , $ url );
104
105
@@ -119,7 +120,7 @@ public function testCustomOnProgressCallback()
119
120
++$ executionCount ;
120
121
};
121
122
122
- $ previousHttpClient = $ this ->getHttpClientMock ( $ url , $ ipAddr , $ content );
123
+ $ previousHttpClient = $ this ->getMockHttpClient ( $ ipAddr , $ content );
123
124
$ client = new NoPrivateNetworkHttpClient ($ previousHttpClient );
124
125
$ response = $ client ->request ('GET ' , $ url , ['on_progress ' => $ customCallback ]);
125
126
@@ -132,7 +133,6 @@ public function testNonCallableOnProgressCallback()
132
133
{
133
134
$ ipAddr = '104.26.14.6 ' ;
134
135
$ url = sprintf ('http://%s/ ' , $ ipAddr );
135
- $ content = 'bar ' ;
136
136
$ customCallback = sprintf ('cb_%s ' , microtime (true ));
137
137
138
138
$ this ->expectException (InvalidArgumentException::class);
@@ -150,38 +150,8 @@ public function testConstructor()
150
150
new NoPrivateNetworkHttpClient (new MockHttpClient (), 3 );
151
151
}
152
152
153
- private function getHttpClientMock ( string $ url , string $ ipAddr , string $ content )
153
+ private function getMockHttpClient ( string $ ipAddr , string $ content )
154
154
{
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 ]));
186
156
}
187
157
}
0 commit comments