Skip to content

Commit e21d9f2

Browse files
authored
Using local Mock Server for tests (#19)
1 parent 9b72195 commit e21d9f2

File tree

6 files changed

+77
-66
lines changed

6 files changed

+77
-66
lines changed

.github/workflows/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ jobs:
5454
- name: Build the Project
5555
run: make update --no-print-directory
5656

57+
- name: Start HTTP Mock Server
58+
run: make start-mock-server --no-print-directory
59+
5760
- name: 🧪 PHPUnit Tests
5861
run: make test --no-print-directory
5962

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ update: ##@Project Install/Update all 3rd party dependencies
2424
test-all: ##@Project Run all project tests at once
2525
@make test
2626
@make codestyle
27+
28+
29+
start-mock-server: ##@Project Start mock server. See: http://httpbin.org/
30+
@echo "Start mock server"
31+
@docker run -d --name httpbin -p 8087:80 kennethreitz/httpbin

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
"require-dev" : {
3939
"jbzoo/toolbox-dev" : "7.x-dev",
40-
"rmccue/requests" : ">=2.0.5",
40+
"rmccue/requests" : ">=2.0.7",
4141
"guzzlehttp/guzzle" : ">=7.5.0"
4242
},
4343

src/Driver/Rmccue.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,10 @@ public function request(Request $request): Response
3131
{
3232
$options = $request->getOptions();
3333

34-
/**
35-
* @psalm-suppress PossiblyInvalidArgument
36-
* @phan-suppress PhanPartialTypeMismatchArgument
37-
*/
3834
$httpResult = Requests::request(
3935
$request->getUri(),
4036
$request->getHeaders(),
41-
$request->getArgs(), // @phpstan-ignore-line @phan-suppress-current-line PhanPartialTypeMismatchArgument
37+
(array)$request->getArgs(),
4238
$request->getMethod(),
4339
self::getDriverOptions($options),
4440
);

tests/AbstractDriverTest.php

+59-60
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,20 @@
1818

1919
use JBZoo\HttpClient\HttpClient;
2020
use JBZoo\HttpClient\Options;
21-
use JBZoo\Utils\Env;
2221
use JBZoo\Utils\Url;
2322
use JBZoo\Utils\Xml;
2423

2524
abstract class AbstractDriverTest extends PHPUnit
2625
{
27-
protected string $driver = 'Auto';
28-
29-
protected array $methods = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'];
30-
31-
protected function setUp(): void
32-
{
33-
parent::setUp();
34-
35-
if (Env::bool('GITHUB_ACTIONS')) {
36-
\sleep(\random_int(0, 2));
37-
}
38-
}
26+
protected string $driver = 'Auto';
27+
protected array $methods = [
28+
'GET',
29+
'POST',
30+
'PATCH',
31+
'PUT',
32+
'DELETE', // must be the last in the list
33+
];
34+
protected string $httpBinHost = 'http://0.0.0.0:8087';
3935

4036
public function testSimple(): void
4137
{
@@ -50,32 +46,36 @@ public function testSimple(): void
5046

5147
public function testBinaryData(): void
5248
{
53-
$result = $this->getClient()->request('https://httpbin.org/image/png');
49+
$result = $this->getClient()->request("{$this->httpBinHost}/image/png");
5450

5551
isSame(200, $result->getCode());
5652
isSame('image/png', $result->getHeader('CONTENT-TYPE'));
5753
isContain('PNG', $result->getBody());
5854
}
5955

60-
public function testPOSTPayload(): void
56+
public function testPostPayload(): void
6157
{
58+
if ($this->driver === 'Rmccue') {
59+
skip('Curl driver does not support post payload');
60+
}
61+
6262
$uniq = \uniqid('', true);
63-
$payload = \json_encode(['key' => $uniq]);
63+
$payload = \json_encode(['key' => $uniq], \JSON_THROW_ON_ERROR);
6464

65-
$url = 'http://mockbin.org/request?key=value';
65+
$url = "{$this->httpBinHost}/anything?key=value";
6666
$result = $this->getClient(['exceptions' => true])->request($url, $payload, 'post');
6767
$body = $result->getJSON();
6868

69-
isSame($payload, $body->find('postData.text'));
70-
isSame('value', $body->find('queryString.key'));
71-
isSame('POST', $body->find('method'));
69+
isSame($payload, $body->find('data'));
70+
isSame('value', $body->find('args.key'));
7271
}
7372

7473
public function testAllMethods(): void
7574
{
7675
foreach ($this->methods as $method) {
77-
$uniq = \uniqid('', true);
78-
$url = "http://mockbin.org/request?method={$method}&qwerty=remove_me";
76+
$uniq = \uniqid('', true);
77+
$url = "{$this->httpBinHost}/anything?method={$method}&qwerty=remove_me";
78+
7979
$args = ['qwerty' => $uniq];
8080
$message = 'Method: ' . $method;
8181

@@ -88,25 +88,26 @@ public function testAllMethods(): void
8888

8989
isSame(200, $result->getCode(), $message);
9090
isContain('application/json', $result->getHeader('Content-Type'), false, $message);
91-
isSame($method, $body->find('queryString.method'), $message);
91+
isSame($method, $body->find('args.method'), $message);
9292
isSame($method, $body->find('method'), $message);
9393

9494
if ($method === 'GET') {
9595
$this->isSameUrl(Url::addArg($args, $url), $body->find('url'), $message);
96-
isSame($uniq, $body->find('queryString.qwerty'), $message);
96+
isSame($uniq, $body->find('args.qwerty'), $message);
9797
} else {
9898
$this->isContainUrl($url, $body->find('url'), $message);
9999
if ($this->driver === 'Rmccue' && $method === 'DELETE') {
100100
skip('DELETE is not supported with Rmccue/Requests correctly');
101101
}
102-
isSame($uniq, $body->find('postData.params.qwerty'), $message);
102+
103+
isSame($uniq, $body->find('form.qwerty'), $message);
103104
}
104105
}
105106
}
106107

107108
public function testAuth(): void
108109
{
109-
$url = 'http://httpbin.org/basic-auth/user/passwd';
110+
$url = "{$this->httpBinHost}/basic-auth/user/passwd";
110111
$result = $this->getClient([
111112
'auth' => ['user', 'passwd'],
112113
])->request($url);
@@ -120,7 +121,7 @@ public function testGetQueryString(): void
120121
{
121122
$uniq = \uniqid('', true);
122123

123-
$siteUrl = 'https://httpbin.org/get?key=value';
124+
$siteUrl = "{$this->httpBinHost}/get?key=value";
124125
$args = ['qwerty' => $uniq];
125126
$url = Url::addArg($args, $siteUrl);
126127
$result = $this->getClient()->request($url, $args);
@@ -136,7 +137,7 @@ public function testGetQueryString(): void
136137

137138
public function testUserAgent(): void
138139
{
139-
$result = $this->getClient()->request('https://httpbin.org/user-agent');
140+
$result = $this->getClient()->request("{$this->httpBinHost}/user-agent");
140141
$body = $result->getJSON();
141142

142143
isSame(200, $result->code);
@@ -147,22 +148,22 @@ public function testUserAgent(): void
147148
public function testPost(): void
148149
{
149150
$uniq = \uniqid('', true);
150-
$url = 'https://httpbin.org/post?key=value';
151+
$url = "{$this->httpBinHost}/post?key=value";
151152
$args = ['qwerty' => $uniq];
152153

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

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

163164
public function testStatus404(): void
164165
{
165-
$result = $this->getClient()->request('http://httpbin.org/status/404');
166+
$result = $this->getClient()->request("{$this->httpBinHost}/status/404");
166167

167168
isSame(404, $result->code);
168169
}
@@ -182,12 +183,12 @@ public function testStatus404Exceptions(): void
182183

183184
$this->getClient([
184185
'exceptions' => true,
185-
])->request('http://httpbin.org/status/404');
186+
])->request("{$this->httpBinHost}/status/404");
186187
}
187188

188189
public function testStatus500(): void
189190
{
190-
$result = $this->getClient()->request('http://httpbin.org/status/500');
191+
$result = $this->getClient()->request("{$this->httpBinHost}/status/500");
191192
isTrue($result->code >= 500);
192193
}
193194

@@ -197,12 +198,12 @@ public function testStatus500Exceptions(): void
197198

198199
$this->getClient([
199200
'exceptions' => true,
200-
])->request('http://httpbin.org/status/500');
201+
])->request("{$this->httpBinHost}/status/500");
201202
}
202203

203204
public function testRedirect(): void
204205
{
205-
$url = Url::addArg(['url' => 'https://google.com'], 'https://httpbin.org/redirect-to');
206+
$url = Url::addArg(['url' => 'https://google.com'], "{$this->httpBinHost}/redirect-to");
206207

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

@@ -213,7 +214,7 @@ public function testRedirect(): void
213214

214215
public function testHeaders(): void
215216
{
216-
$url = 'http://httpbin.org/headers';
217+
$url = "{$this->httpBinHost}/headers";
217218

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

229230
public function testGzip(): void
230231
{
231-
$url = 'http://httpbin.org/gzip';
232+
$url = "{$this->httpBinHost}/gzip";
232233

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

@@ -238,12 +239,12 @@ public function testGzip(): void
238239

239240
public function testMultiRedirects(): void
240241
{
241-
$url = 'http://httpbin.org/absolute-redirect/2';
242+
$url = "{$this->httpBinHost}/absolute-redirect/2";
242243
$result = $this->getClient()->request($url);
243244
$body = $result->getJSON();
244245

245246
isSame(200, $result->code);
246-
$this->isSameUrl('http://httpbin.org/get', $body->get('url'));
247+
$this->isSameUrl("{$this->httpBinHost}/get", $body->get('url'));
247248
}
248249

249250
public function testDelayError(): void
@@ -253,15 +254,15 @@ public function testDelayError(): void
253254
$this->getClient([
254255
'timeout' => 2,
255256
'exceptions' => true,
256-
])->request('http://httpbin.org/delay/5');
257+
])->request("{$this->httpBinHost}/delay/5");
257258
}
258259

259260
public function testDelayErrorExceptionsDisable(): void
260261
{
261262
$result = $this->getClient([
262263
'timeout' => 2,
263264
'exceptions' => false,
264-
])->request('http://httpbin.org/delay/5');
265+
])->request("{$this->httpBinHost}/delay/5");
265266

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

271272
public function testDelay(): void
272273
{
273-
$url = 'https://httpbin.org/delay/5';
274+
$url = "{$this->httpBinHost}/delay/5";
274275
$result = $this->getClient()->request($url);
275276
$body = $result->getJSON();
276277

@@ -280,22 +281,20 @@ public function testDelay(): void
280281

281282
public function testSSL(): void
282283
{
283-
$url = 'https://www.google.com';
284-
$result = $this->getClient(['verify' => false])->request($url);
284+
$url = 'https://www.google.com';
285285

286+
$result = $this->getClient(['verify' => false])->request($url);
286287
isSame(200, $result->code);
287288
isContain('google', $result->body);
288289

289-
$url = 'https://www.google.com';
290290
$result = $this->getClient(['verify' => true])->request($url);
291-
292291
isSame(200, $result->code);
293292
isContain('google', $result->body);
294293
}
295294

296295
public function testXmlAsResponse(): void
297296
{
298-
$result = $this->getClient()->request('https://httpbin.org/xml');
297+
$result = $this->getClient()->request("{$this->httpBinHost}/xml");
299298

300299
isSame(200, $result->code);
301300
isSame('application/xml', $result->getHeader('Content-Type'));
@@ -444,10 +443,10 @@ public function testXmlAsResponse(): void
444443
public function testMultiRequest(): void
445444
{
446445
$responseList = $this->getClient(['user_agent' => 'Qwerty Agent v123'])->multiRequest([
447-
'request_0' => ['http://mockbin.org/request?qwerty=123456'],
448-
'request_1' => ['http://mockbin.org/request', ['key' => 'value']],
446+
'request_0' => ["{$this->httpBinHost}/anything?qwerty=123456"],
447+
'request_1' => ["{$this->httpBinHost}/anything", ['key' => 'value']],
449448
'request_2' => [
450-
'http://mockbin.org/request',
449+
"{$this->httpBinHost}/anything",
451450
['key' => 'value'],
452451
'post',
453452
[
@@ -461,38 +460,38 @@ public function testMultiRequest(): void
461460

462461
// Response - 0
463462
$request0 = $responseList['request_0']->getRequest();
464-
isSame('http://mockbin.org/request?qwerty=123456', $request0->getUri());
463+
isSame("{$this->httpBinHost}/anything?qwerty=123456", $request0->getUri());
465464
isSame('Qwerty Agent v123', $request0->getOptions()->getUserAgent());
466465
isSame('GET', $request0->getMethod());
467466

468467
$jsonBody0 = $responseList['request_0']->getJSON();
469-
isSame('http://mockbin.org/request?qwerty=123456', $jsonBody0->find('url'));
468+
isSame("{$this->httpBinHost}/anything?qwerty=123456", $jsonBody0->find('url'));
470469
isSame('GET', $jsonBody0->find('method'));
471-
isSame('123456', $jsonBody0->find('queryString.qwerty'));
470+
isSame('123456', $jsonBody0->find('args.qwerty'));
472471

473472
// Response - 1
474473
$request1 = $responseList['request_1']->getRequest();
475-
isSame('http://mockbin.org/request?key=value', $request1->getUri());
474+
isSame("{$this->httpBinHost}/anything?key=value", $request1->getUri());
476475
isSame('Qwerty Agent v123', $request1->getOptions()->getUserAgent());
477476
isSame('GET', $request1->getMethod());
478477

479478
$jsonBody1 = $responseList['request_1']->getJSON();
480-
isSame('http://mockbin.org/request?key=value', $jsonBody1->find('url'));
479+
isSame("{$this->httpBinHost}/anything?key=value", $jsonBody1->find('url'));
481480
isSame('GET', $jsonBody1->find('method'));
482-
isSame('value', $jsonBody1->find('queryString.key'));
481+
isSame('value', $jsonBody1->find('args.key'));
483482

484483
// Response - 2
485484
$request2 = $responseList['request_2']->getRequest();
486-
isSame('http://mockbin.org/request', $request2->getUri());
485+
isSame("{$this->httpBinHost}/anything", $request2->getUri());
487486
isSame('Qwerty Agent v456', $request2->getOptions()->getUserAgent());
488487
isSame('123', $request2->getHeaders()['x-custom-header']);
489488
isSame('POST', $request2->getMethod());
490489

491490
$jsonBody2 = $responseList['request_2']->getJSON();
492-
isSame('123', $jsonBody2->find('headers.x-custom-header'));
493-
isSame('http://mockbin.org/request', $jsonBody2->find('url'));
491+
isSame('123', $jsonBody2->find('headers.X-Custom-Header'));
492+
isSame("{$this->httpBinHost}/anything", $jsonBody2->find('url'));
494493
isSame('POST', $jsonBody2->find('method'));
495-
isSame('value', $jsonBody2->find('postData.params.key'));
494+
isSame('value', $jsonBody2->find('form.key'));
496495
}
497496

498497
protected function getClient(array $options = []): HttpClient

tests/HttpClientPackageTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@
1919
final class HttpClientPackageTest extends \JBZoo\Codestyle\PHPUnit\AbstractPackageTest
2020
{
2121
protected string $packageName = 'Http-Client';
22+
23+
protected static function stepBeforeTests(): ?array
24+
{
25+
return [
26+
'name' => 'Start HTTP Mock Server',
27+
'run' => 'make start-mock-server --no-print-directory',
28+
];
29+
}
2230
}

0 commit comments

Comments
 (0)