Skip to content

Commit c00326a

Browse files
authored
Fixed mock-server URL (#20)
1 parent e21d9f2 commit c00326a

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

tests/AbstractDriverTest.php

+32-31
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ abstract class AbstractDriverTest extends PHPUnit
3131
'PUT',
3232
'DELETE', // must be the last in the list
3333
];
34-
protected string $httpBinHost = 'http://0.0.0.0:8087';
34+
35+
protected string $mockServerUrl = 'http://0.0.0.0:8087';
3536

3637
public function testSimple(): void
3738
{
@@ -46,7 +47,7 @@ public function testSimple(): void
4647

4748
public function testBinaryData(): void
4849
{
49-
$result = $this->getClient()->request("{$this->httpBinHost}/image/png");
50+
$result = $this->getClient()->request("{$this->mockServerUrl}/image/png");
5051

5152
isSame(200, $result->getCode());
5253
isSame('image/png', $result->getHeader('CONTENT-TYPE'));
@@ -62,7 +63,7 @@ public function testPostPayload(): void
6263
$uniq = \uniqid('', true);
6364
$payload = \json_encode(['key' => $uniq], \JSON_THROW_ON_ERROR);
6465

65-
$url = "{$this->httpBinHost}/anything?key=value";
66+
$url = "{$this->mockServerUrl}/anything?key=value";
6667
$result = $this->getClient(['exceptions' => true])->request($url, $payload, 'post');
6768
$body = $result->getJSON();
6869

@@ -74,7 +75,7 @@ public function testAllMethods(): void
7475
{
7576
foreach ($this->methods as $method) {
7677
$uniq = \uniqid('', true);
77-
$url = "{$this->httpBinHost}/anything?method={$method}&qwerty=remove_me";
78+
$url = "{$this->mockServerUrl}/anything?method={$method}&qwerty=remove_me";
7879

7980
$args = ['qwerty' => $uniq];
8081
$message = 'Method: ' . $method;
@@ -107,7 +108,7 @@ public function testAllMethods(): void
107108

108109
public function testAuth(): void
109110
{
110-
$url = "{$this->httpBinHost}/basic-auth/user/passwd";
111+
$url = "{$this->mockServerUrl}/basic-auth/user/passwd";
111112
$result = $this->getClient([
112113
'auth' => ['user', 'passwd'],
113114
])->request($url);
@@ -121,7 +122,7 @@ public function testGetQueryString(): void
121122
{
122123
$uniq = \uniqid('', true);
123124

124-
$siteUrl = "{$this->httpBinHost}/get?key=value";
125+
$siteUrl = "{$this->mockServerUrl}/get?key=value";
125126
$args = ['qwerty' => $uniq];
126127
$url = Url::addArg($args, $siteUrl);
127128
$result = $this->getClient()->request($url, $args);
@@ -137,7 +138,7 @@ public function testGetQueryString(): void
137138

138139
public function testUserAgent(): void
139140
{
140-
$result = $this->getClient()->request("{$this->httpBinHost}/user-agent");
141+
$result = $this->getClient()->request("{$this->mockServerUrl}/user-agent");
141142
$body = $result->getJSON();
142143

143144
isSame(200, $result->code);
@@ -148,22 +149,22 @@ public function testUserAgent(): void
148149
public function testPost(): void
149150
{
150151
$uniq = \uniqid('', true);
151-
$url = "{$this->httpBinHost}/post?key=value";
152+
$url = "{$this->mockServerUrl}/post?key=value";
152153
$args = ['qwerty' => $uniq];
153154

154155
$result = $this->getClient()->request($url, $args, 'post');
155156
$body = $result->getJSON();
156157

157158
isSame(200, $result->code);
158159
isContain('application/json', $result->getHeader('content-type'));
159-
$this->isSameUrl("{$this->httpBinHost}/post?key=value", $body->find('url'));
160+
$this->isSameUrl("{$this->mockServerUrl}/post?key=value", $body->find('url'));
160161
isSame($uniq, $body->find('form.qwerty'));
161162
isSame('value', $body->find('args.key'));
162163
}
163164

164165
public function testStatus404(): void
165166
{
166-
$result = $this->getClient()->request("{$this->httpBinHost}/status/404");
167+
$result = $this->getClient()->request("{$this->mockServerUrl}/status/404");
167168

168169
isSame(404, $result->code);
169170
}
@@ -183,12 +184,12 @@ public function testStatus404Exceptions(): void
183184

184185
$this->getClient([
185186
'exceptions' => true,
186-
])->request("{$this->httpBinHost}/status/404");
187+
])->request("{$this->mockServerUrl}/status/404");
187188
}
188189

189190
public function testStatus500(): void
190191
{
191-
$result = $this->getClient()->request("{$this->httpBinHost}/status/500");
192+
$result = $this->getClient()->request("{$this->mockServerUrl}/status/500");
192193
isTrue($result->code >= 500);
193194
}
194195

@@ -198,12 +199,12 @@ public function testStatus500Exceptions(): void
198199

199200
$this->getClient([
200201
'exceptions' => true,
201-
])->request("{$this->httpBinHost}/status/500");
202+
])->request("{$this->mockServerUrl}/status/500");
202203
}
203204

204205
public function testRedirect(): void
205206
{
206-
$url = Url::addArg(['url' => 'https://google.com'], "{$this->httpBinHost}/redirect-to");
207+
$url = Url::addArg(['url' => 'https://google.com'], "{$this->mockServerUrl}/redirect-to");
207208

208209
$result = $this->getClient()->request($url);
209210

@@ -214,7 +215,7 @@ public function testRedirect(): void
214215

215216
public function testHeaders(): void
216217
{
217-
$url = "{$this->httpBinHost}/headers";
218+
$url = "{$this->mockServerUrl}/headers";
218219

219220
$uniq = \uniqid('', true);
220221
$result = $this->getClient([
@@ -229,7 +230,7 @@ public function testHeaders(): void
229230

230231
public function testGzip(): void
231232
{
232-
$url = "{$this->httpBinHost}/gzip";
233+
$url = "{$this->mockServerUrl}/gzip";
233234

234235
$result = $this->getClient()->request($url);
235236

@@ -239,12 +240,12 @@ public function testGzip(): void
239240

240241
public function testMultiRedirects(): void
241242
{
242-
$url = "{$this->httpBinHost}/absolute-redirect/2";
243+
$url = "{$this->mockServerUrl}/absolute-redirect/2";
243244
$result = $this->getClient()->request($url);
244245
$body = $result->getJSON();
245246

246247
isSame(200, $result->code);
247-
$this->isSameUrl("{$this->httpBinHost}/get", $body->get('url'));
248+
$this->isSameUrl("{$this->mockServerUrl}/get", $body->get('url'));
248249
}
249250

250251
public function testDelayError(): void
@@ -254,15 +255,15 @@ public function testDelayError(): void
254255
$this->getClient([
255256
'timeout' => 2,
256257
'exceptions' => true,
257-
])->request("{$this->httpBinHost}/delay/5");
258+
])->request("{$this->mockServerUrl}/delay/5");
258259
}
259260

260261
public function testDelayErrorExceptionsDisable(): void
261262
{
262263
$result = $this->getClient([
263264
'timeout' => 2,
264265
'exceptions' => false,
265-
])->request("{$this->httpBinHost}/delay/5");
266+
])->request("{$this->mockServerUrl}/delay/5");
266267

267268
isSame(0, $result->getCode());
268269
isSame([], $result->getHeaders());
@@ -271,7 +272,7 @@ public function testDelayErrorExceptionsDisable(): void
271272

272273
public function testDelay(): void
273274
{
274-
$url = "{$this->httpBinHost}/delay/5";
275+
$url = "{$this->mockServerUrl}/delay/5";
275276
$result = $this->getClient()->request($url);
276277
$body = $result->getJSON();
277278

@@ -294,7 +295,7 @@ public function testSSL(): void
294295

295296
public function testXmlAsResponse(): void
296297
{
297-
$result = $this->getClient()->request("{$this->httpBinHost}/xml");
298+
$result = $this->getClient()->request("{$this->mockServerUrl}/xml");
298299

299300
isSame(200, $result->code);
300301
isSame('application/xml', $result->getHeader('Content-Type'));
@@ -443,10 +444,10 @@ public function testXmlAsResponse(): void
443444
public function testMultiRequest(): void
444445
{
445446
$responseList = $this->getClient(['user_agent' => 'Qwerty Agent v123'])->multiRequest([
446-
'request_0' => ["{$this->httpBinHost}/anything?qwerty=123456"],
447-
'request_1' => ["{$this->httpBinHost}/anything", ['key' => 'value']],
447+
'request_0' => ["{$this->mockServerUrl}/anything?qwerty=123456"],
448+
'request_1' => ["{$this->mockServerUrl}/anything", ['key' => 'value']],
448449
'request_2' => [
449-
"{$this->httpBinHost}/anything",
450+
"{$this->mockServerUrl}/anything",
450451
['key' => 'value'],
451452
'post',
452453
[
@@ -460,36 +461,36 @@ public function testMultiRequest(): void
460461

461462
// Response - 0
462463
$request0 = $responseList['request_0']->getRequest();
463-
isSame("{$this->httpBinHost}/anything?qwerty=123456", $request0->getUri());
464+
isSame("{$this->mockServerUrl}/anything?qwerty=123456", $request0->getUri());
464465
isSame('Qwerty Agent v123', $request0->getOptions()->getUserAgent());
465466
isSame('GET', $request0->getMethod());
466467

467468
$jsonBody0 = $responseList['request_0']->getJSON();
468-
isSame("{$this->httpBinHost}/anything?qwerty=123456", $jsonBody0->find('url'));
469+
isSame("{$this->mockServerUrl}/anything?qwerty=123456", $jsonBody0->find('url'));
469470
isSame('GET', $jsonBody0->find('method'));
470471
isSame('123456', $jsonBody0->find('args.qwerty'));
471472

472473
// Response - 1
473474
$request1 = $responseList['request_1']->getRequest();
474-
isSame("{$this->httpBinHost}/anything?key=value", $request1->getUri());
475+
isSame("{$this->mockServerUrl}/anything?key=value", $request1->getUri());
475476
isSame('Qwerty Agent v123', $request1->getOptions()->getUserAgent());
476477
isSame('GET', $request1->getMethod());
477478

478479
$jsonBody1 = $responseList['request_1']->getJSON();
479-
isSame("{$this->httpBinHost}/anything?key=value", $jsonBody1->find('url'));
480+
isSame("{$this->mockServerUrl}/anything?key=value", $jsonBody1->find('url'));
480481
isSame('GET', $jsonBody1->find('method'));
481482
isSame('value', $jsonBody1->find('args.key'));
482483

483484
// Response - 2
484485
$request2 = $responseList['request_2']->getRequest();
485-
isSame("{$this->httpBinHost}/anything", $request2->getUri());
486+
isSame("{$this->mockServerUrl}/anything", $request2->getUri());
486487
isSame('Qwerty Agent v456', $request2->getOptions()->getUserAgent());
487488
isSame('123', $request2->getHeaders()['x-custom-header']);
488489
isSame('POST', $request2->getMethod());
489490

490491
$jsonBody2 = $responseList['request_2']->getJSON();
491492
isSame('123', $jsonBody2->find('headers.X-Custom-Header'));
492-
isSame("{$this->httpBinHost}/anything", $jsonBody2->find('url'));
493+
isSame("{$this->mockServerUrl}/anything", $jsonBody2->find('url'));
493494
isSame('POST', $jsonBody2->find('method'));
494495
isSame('value', $jsonBody2->find('form.key'));
495496
}

tests/HttpClientOtherTest.php

+25-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
final class HttpClientOtherTest extends PHPUnit
2626
{
27+
protected string $mockServerUrl = 'http://0.0.0.0:8087';
28+
2729
protected string $jsonFixture = '{"key-1":"value-1","key-2":"value-2"}';
2830

2931
public function testGetSameJSONFromResponse(): void
@@ -50,12 +52,12 @@ public function testGetSameJSONFromResponse(): void
5052
public function testGetRequestDefault(): void
5153
{
5254
$client = new HttpClient();
53-
$response = $client->request('https://httpbin.org/get');
55+
$response = $client->request("{$this->mockServerUrl}/get");
5456

5557
isSame('JBZoo/Http-Client (Guzzle)', $response->getJSON()->find('headers.User-Agent'));
5658

5759
$request = $response->getRequest();
58-
isSame('https://httpbin.org/get', $request->getUri());
60+
isSame("{$this->mockServerUrl}/get", $request->getUri());
5961
isSame(null, $request->getArgs());
6062
isSame('GET', $request->getMethod());
6163
isSame([
@@ -74,12 +76,12 @@ public function testGetRequestDefault(): void
7476
public function testGetRequestGlobalOptions(): void
7577
{
7678
$client = new HttpClient(['user_agent' => 'Qwerty Client']);
77-
$response = $client->request('https://httpbin.org/get', ['param' => 'value']);
79+
$response = $client->request("{$this->mockServerUrl}/get", ['param' => 'value']);
7880

7981
isSame('Qwerty Client', $response->getJSON()->find('headers.User-Agent'));
8082

8183
$request = $response->getRequest();
82-
isSame('https://httpbin.org/get?param=value', $request->getUri());
84+
isSame("{$this->mockServerUrl}/get?param=value", $request->getUri());
8385
isSame(null, $request->getArgs());
8486
isSame('GET', $request->getMethod());
8587
isSame([
@@ -98,14 +100,14 @@ public function testGetRequestGlobalOptions(): void
98100
public function testGetRequestRequestOptions(): void
99101
{
100102
$client = new HttpClient();
101-
$response = $client->request('https://httpbin.org/post', ['param' => 'value'], 'POST', [
103+
$response = $client->request("{$this->mockServerUrl}/post", ['param' => 'value'], 'POST', [
102104
'user_agent' => 'Qwerty Client2',
103105
]);
104106

105107
isSame('Qwerty Client2', $response->getJSON()->find('headers.User-Agent'));
106108

107109
$request = $response->getRequest();
108-
isSame('https://httpbin.org/post', $request->getUri());
110+
isSame("{$this->mockServerUrl}/post", $request->getUri());
109111
isSame(['param' => 'value'], $request->getArgs());
110112
isSame('POST', $request->getMethod());
111113
isSame([
@@ -124,14 +126,14 @@ public function testGetRequestRequestOptions(): void
124126
public function testGetRequestRequestOptionsWithPostBody(): void
125127
{
126128
$client = new HttpClient();
127-
$response = $client->request('https://httpbin.org/post', 'qwerty', 'POST', [
129+
$response = $client->request("{$this->mockServerUrl}/post", 'qwerty', 'POST', [
128130
'user_agent' => 'Qwerty Client2',
129131
]);
130132

131133
isSame('Qwerty Client2', $response->getJSON()->find('headers.User-Agent'));
132134

133135
$request = $response->getRequest();
134-
isSame('https://httpbin.org/post', $request->getUri());
136+
isSame("{$this->mockServerUrl}/post", $request->getUri());
135137
isSame('qwerty', $request->getArgs());
136138
isSame('POST', $request->getMethod());
137139
isSame([
@@ -156,7 +158,7 @@ public function testGetRequestMergingOptions(): void
156158
'driver' => 'Guzzle',
157159
]);
158160

159-
$response = $client->request('https://httpbin.org/get?key=val', ['param' => 'value'], 'GET', [
161+
$response = $client->request("{$this->mockServerUrl}/get?key=val", ['param' => 'value'], 'GET', [
160162
'user_agent' => 'Custom Agent',
161163
'headers' => ['X-Custom-Header' => $randomValue],
162164
]);
@@ -175,7 +177,7 @@ public function testGetRequestMergingOptions(): void
175177
isSame((string)$randomValue, $client->getLastResponse()->getJSON()->find('headers.X-Custom-Header'));
176178

177179
$request = $client->getLastRequest();
178-
isSame('https://httpbin.org/get?key=val&param=value', $request->getUri());
180+
isSame("{$this->mockServerUrl}/get?key=val&param=value", $request->getUri());
179181
isSame(null, $request->getArgs());
180182
isSame('GET', $request->getMethod());
181183
isSame([
@@ -201,7 +203,7 @@ public function testCheckDefaultDriver(): void
201203
{
202204
$client = new HttpClient();
203205

204-
$response = $client->request('https://httpbin.org/user-agent');
206+
$response = $client->request("{$this->mockServerUrl}/user-agent");
205207
isSame('JBZoo/Http-Client (Guzzle)', $response->getJSON()->get('user-agent'));
206208
}
207209

@@ -212,28 +214,30 @@ public function testEventManager(): void
212214
$client = new HttpClient();
213215
$client->setEventManager($eManager);
214216

217+
$urlToTestGet = "{$this->mockServerUrl}/get";
218+
215219
$counter = 0;
216220
$eManager
217221
->once(
218222
'jbzoo.http.request.before',
219-
static function (HttpClient $client, Request $request) use (&$counter): void {
220-
isSame('https://httpbin.org/get', $client->getLastRequest()->getUri());
221-
isSame('https://httpbin.org/get', $request->getUri());
223+
static function (HttpClient $client, Request $request) use (&$counter, $urlToTestGet): void {
224+
isSame($urlToTestGet, $client->getLastRequest()->getUri());
225+
isSame($urlToTestGet, $request->getUri());
222226
$counter++;
223227
},
224228
)
225229
->once(
226230
'jbzoo.http.request.after',
227-
static function (HttpClient $client, Response $response, Request $request) use (&$counter): void {
228-
isSame('https://httpbin.org/get', $client->getLastRequest()->getUri());
229-
isSame('https://httpbin.org/get', $request->getUri());
230-
isSame('httpbin.org', $response->getJSON()->find('headers.Host'));
231+
static function (HttpClient $client, Response $response, Request $request) use (&$counter, $urlToTestGet): void {
232+
isSame($urlToTestGet, $client->getLastRequest()->getUri());
233+
isSame($urlToTestGet, $request->getUri());
234+
isContain('0.0.0.0', $response->getJSON()->find('headers.Host'));
231235
$counter++;
232236
},
233237
);
234238

235-
$response = $client->request('https://httpbin.org/get');
236-
isSame('httpbin.org', $response->getJSON()->find('headers.Host'));
239+
$response = $client->request($urlToTestGet);
240+
isSame('0.0.0.0:8087', $response->getJSON()->find('headers.Host'));
237241

238242
isSame(2, $counter);
239243
}
@@ -254,7 +258,7 @@ public function testEventManagerException(): void
254258
});
255259

256260
try {
257-
$client->request('http://httpbin.org/status/404');
261+
$client->request("{$this->mockServerUrl}/status/404");
258262
fail();
259263
} catch (\Exception $exception) {
260264
success();

0 commit comments

Comments
 (0)