18
18
19
19
use JBZoo \HttpClient \HttpClient ;
20
20
use JBZoo \HttpClient \Options ;
21
- use JBZoo \Utils \Env ;
22
21
use JBZoo \Utils \Url ;
23
22
use JBZoo \Utils \Xml ;
24
23
25
24
abstract class AbstractDriverTest extends PHPUnit
26
25
{
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 ' ;
39
35
40
36
public function testSimple (): void
41
37
{
@@ -50,32 +46,36 @@ public function testSimple(): void
50
46
51
47
public function testBinaryData (): void
52
48
{
53
- $ result = $ this ->getClient ()->request (' https://httpbin.org/ image/png' );
49
+ $ result = $ this ->getClient ()->request ("{ $ this -> httpBinHost } / image/png" );
54
50
55
51
isSame (200 , $ result ->getCode ());
56
52
isSame ('image/png ' , $ result ->getHeader ('CONTENT-TYPE ' ));
57
53
isContain ('PNG ' , $ result ->getBody ());
58
54
}
59
55
60
- public function testPOSTPayload (): void
56
+ public function testPostPayload (): void
61
57
{
58
+ if ($ this ->driver === 'Rmccue ' ) {
59
+ skip ('Curl driver does not support post payload ' );
60
+ }
61
+
62
62
$ uniq = \uniqid ('' , true );
63
- $ payload = \json_encode (['key ' => $ uniq ]);
63
+ $ payload = \json_encode (['key ' => $ uniq ], \ JSON_THROW_ON_ERROR );
64
64
65
- $ url = ' http://mockbin.org/request ?key=value' ;
65
+ $ url = "{ $ this -> httpBinHost } /anything ?key=value" ;
66
66
$ result = $ this ->getClient (['exceptions ' => true ])->request ($ url , $ payload , 'post ' );
67
67
$ body = $ result ->getJSON ();
68
68
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 ' ));
72
71
}
73
72
74
73
public function testAllMethods (): void
75
74
{
76
75
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
+
79
79
$ args = ['qwerty ' => $ uniq ];
80
80
$ message = 'Method: ' . $ method ;
81
81
@@ -88,25 +88,26 @@ public function testAllMethods(): void
88
88
89
89
isSame (200 , $ result ->getCode (), $ message );
90
90
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 );
92
92
isSame ($ method , $ body ->find ('method ' ), $ message );
93
93
94
94
if ($ method === 'GET ' ) {
95
95
$ 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 );
97
97
} else {
98
98
$ this ->isContainUrl ($ url , $ body ->find ('url ' ), $ message );
99
99
if ($ this ->driver === 'Rmccue ' && $ method === 'DELETE ' ) {
100
100
skip ('DELETE is not supported with Rmccue/Requests correctly ' );
101
101
}
102
- isSame ($ uniq , $ body ->find ('postData.params.qwerty ' ), $ message );
102
+
103
+ isSame ($ uniq , $ body ->find ('form.qwerty ' ), $ message );
103
104
}
104
105
}
105
106
}
106
107
107
108
public function testAuth (): void
108
109
{
109
- $ url = ' http://httpbin.org/ basic-auth/user/passwd' ;
110
+ $ url = "{ $ this -> httpBinHost } / basic-auth/user/passwd" ;
110
111
$ result = $ this ->getClient ([
111
112
'auth ' => ['user ' , 'passwd ' ],
112
113
])->request ($ url );
@@ -120,7 +121,7 @@ public function testGetQueryString(): void
120
121
{
121
122
$ uniq = \uniqid ('' , true );
122
123
123
- $ siteUrl = ' https://httpbin.org/ get?key=value' ;
124
+ $ siteUrl = "{ $ this -> httpBinHost } / get?key=value" ;
124
125
$ args = ['qwerty ' => $ uniq ];
125
126
$ url = Url::addArg ($ args , $ siteUrl );
126
127
$ result = $ this ->getClient ()->request ($ url , $ args );
@@ -136,7 +137,7 @@ public function testGetQueryString(): void
136
137
137
138
public function testUserAgent (): void
138
139
{
139
- $ result = $ this ->getClient ()->request (' https://httpbin.org/ user-agent' );
140
+ $ result = $ this ->getClient ()->request ("{ $ this -> httpBinHost } / user-agent" );
140
141
$ body = $ result ->getJSON ();
141
142
142
143
isSame (200 , $ result ->code );
@@ -147,22 +148,22 @@ public function testUserAgent(): void
147
148
public function testPost (): void
148
149
{
149
150
$ uniq = \uniqid ('' , true );
150
- $ url = ' https://httpbin.org/ post?key=value' ;
151
+ $ url = "{ $ this -> httpBinHost } / post?key=value" ;
151
152
$ args = ['qwerty ' => $ uniq ];
152
153
153
154
$ result = $ this ->getClient ()->request ($ url , $ args , 'post ' );
154
155
$ body = $ result ->getJSON ();
155
156
156
157
isSame (200 , $ result ->code );
157
158
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 ' ));
159
160
isSame ($ uniq , $ body ->find ('form.qwerty ' ));
160
161
isSame ('value ' , $ body ->find ('args.key ' ));
161
162
}
162
163
163
164
public function testStatus404 (): void
164
165
{
165
- $ result = $ this ->getClient ()->request (' http://httpbin.org/ status/404' );
166
+ $ result = $ this ->getClient ()->request ("{ $ this -> httpBinHost } / status/404" );
166
167
167
168
isSame (404 , $ result ->code );
168
169
}
@@ -182,12 +183,12 @@ public function testStatus404Exceptions(): void
182
183
183
184
$ this ->getClient ([
184
185
'exceptions ' => true ,
185
- ])->request (' http://httpbin.org/ status/404' );
186
+ ])->request ("{ $ this -> httpBinHost } / status/404" );
186
187
}
187
188
188
189
public function testStatus500 (): void
189
190
{
190
- $ result = $ this ->getClient ()->request (' http://httpbin.org/ status/500' );
191
+ $ result = $ this ->getClient ()->request ("{ $ this -> httpBinHost } / status/500" );
191
192
isTrue ($ result ->code >= 500 );
192
193
}
193
194
@@ -197,12 +198,12 @@ public function testStatus500Exceptions(): void
197
198
198
199
$ this ->getClient ([
199
200
'exceptions ' => true ,
200
- ])->request (' http://httpbin.org/ status/500' );
201
+ ])->request ("{ $ this -> httpBinHost } / status/500" );
201
202
}
202
203
203
204
public function testRedirect (): void
204
205
{
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" );
206
207
207
208
$ result = $ this ->getClient ()->request ($ url );
208
209
@@ -213,7 +214,7 @@ public function testRedirect(): void
213
214
214
215
public function testHeaders (): void
215
216
{
216
- $ url = ' http://httpbin.org/ headers' ;
217
+ $ url = "{ $ this -> httpBinHost } / headers" ;
217
218
218
219
$ uniq = \uniqid ('' , true );
219
220
$ result = $ this ->getClient ([
@@ -228,7 +229,7 @@ public function testHeaders(): void
228
229
229
230
public function testGzip (): void
230
231
{
231
- $ url = ' http://httpbin.org/ gzip' ;
232
+ $ url = "{ $ this -> httpBinHost } / gzip" ;
232
233
233
234
$ result = $ this ->getClient ()->request ($ url );
234
235
@@ -238,12 +239,12 @@ public function testGzip(): void
238
239
239
240
public function testMultiRedirects (): void
240
241
{
241
- $ url = ' http://httpbin.org/ absolute-redirect/2' ;
242
+ $ url = "{ $ this -> httpBinHost } / absolute-redirect/2" ;
242
243
$ result = $ this ->getClient ()->request ($ url );
243
244
$ body = $ result ->getJSON ();
244
245
245
246
isSame (200 , $ result ->code );
246
- $ this ->isSameUrl (' http://httpbin.org/ get' , $ body ->get ('url ' ));
247
+ $ this ->isSameUrl ("{ $ this -> httpBinHost } / get" , $ body ->get ('url ' ));
247
248
}
248
249
249
250
public function testDelayError (): void
@@ -253,15 +254,15 @@ public function testDelayError(): void
253
254
$ this ->getClient ([
254
255
'timeout ' => 2 ,
255
256
'exceptions ' => true ,
256
- ])->request (' http://httpbin.org/ delay/5' );
257
+ ])->request ("{ $ this -> httpBinHost } / delay/5" );
257
258
}
258
259
259
260
public function testDelayErrorExceptionsDisable (): void
260
261
{
261
262
$ result = $ this ->getClient ([
262
263
'timeout ' => 2 ,
263
264
'exceptions ' => false ,
264
- ])->request (' http://httpbin.org/ delay/5' );
265
+ ])->request ("{ $ this -> httpBinHost } / delay/5" );
265
266
266
267
isSame (0 , $ result ->getCode ());
267
268
isSame ([], $ result ->getHeaders ());
@@ -270,7 +271,7 @@ public function testDelayErrorExceptionsDisable(): void
270
271
271
272
public function testDelay (): void
272
273
{
273
- $ url = ' https://httpbin.org/ delay/5' ;
274
+ $ url = "{ $ this -> httpBinHost } / delay/5" ;
274
275
$ result = $ this ->getClient ()->request ($ url );
275
276
$ body = $ result ->getJSON ();
276
277
@@ -280,22 +281,20 @@ public function testDelay(): void
280
281
281
282
public function testSSL (): void
282
283
{
283
- $ url = 'https://www.google.com ' ;
284
- $ result = $ this ->getClient (['verify ' => false ])->request ($ url );
284
+ $ url = 'https://www.google.com ' ;
285
285
286
+ $ result = $ this ->getClient (['verify ' => false ])->request ($ url );
286
287
isSame (200 , $ result ->code );
287
288
isContain ('google ' , $ result ->body );
288
289
289
- $ url = 'https://www.google.com ' ;
290
290
$ result = $ this ->getClient (['verify ' => true ])->request ($ url );
291
-
292
291
isSame (200 , $ result ->code );
293
292
isContain ('google ' , $ result ->body );
294
293
}
295
294
296
295
public function testXmlAsResponse (): void
297
296
{
298
- $ result = $ this ->getClient ()->request (' https://httpbin.org/ xml' );
297
+ $ result = $ this ->getClient ()->request ("{ $ this -> httpBinHost } / xml" );
299
298
300
299
isSame (200 , $ result ->code );
301
300
isSame ('application/xml ' , $ result ->getHeader ('Content-Type ' ));
@@ -444,10 +443,10 @@ public function testXmlAsResponse(): void
444
443
public function testMultiRequest (): void
445
444
{
446
445
$ 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 ' ]],
449
448
'request_2 ' => [
450
- ' http://mockbin.org/request ' ,
449
+ "{ $ this -> httpBinHost } /anything " ,
451
450
['key ' => 'value ' ],
452
451
'post ' ,
453
452
[
@@ -461,38 +460,38 @@ public function testMultiRequest(): void
461
460
462
461
// Response - 0
463
462
$ request0 = $ responseList ['request_0 ' ]->getRequest ();
464
- isSame (' http://mockbin.org/request ?qwerty=123456' , $ request0 ->getUri ());
463
+ isSame ("{ $ this -> httpBinHost } /anything ?qwerty=123456" , $ request0 ->getUri ());
465
464
isSame ('Qwerty Agent v123 ' , $ request0 ->getOptions ()->getUserAgent ());
466
465
isSame ('GET ' , $ request0 ->getMethod ());
467
466
468
467
$ 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 ' ));
470
469
isSame ('GET ' , $ jsonBody0 ->find ('method ' ));
471
- isSame ('123456 ' , $ jsonBody0 ->find ('queryString .qwerty ' ));
470
+ isSame ('123456 ' , $ jsonBody0 ->find ('args .qwerty ' ));
472
471
473
472
// Response - 1
474
473
$ request1 = $ responseList ['request_1 ' ]->getRequest ();
475
- isSame (' http://mockbin.org/request ?key=value' , $ request1 ->getUri ());
474
+ isSame ("{ $ this -> httpBinHost } /anything ?key=value" , $ request1 ->getUri ());
476
475
isSame ('Qwerty Agent v123 ' , $ request1 ->getOptions ()->getUserAgent ());
477
476
isSame ('GET ' , $ request1 ->getMethod ());
478
477
479
478
$ 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 ' ));
481
480
isSame ('GET ' , $ jsonBody1 ->find ('method ' ));
482
- isSame ('value ' , $ jsonBody1 ->find ('queryString .key ' ));
481
+ isSame ('value ' , $ jsonBody1 ->find ('args .key ' ));
483
482
484
483
// Response - 2
485
484
$ request2 = $ responseList ['request_2 ' ]->getRequest ();
486
- isSame (' http://mockbin.org/request ' , $ request2 ->getUri ());
485
+ isSame ("{ $ this -> httpBinHost } /anything " , $ request2 ->getUri ());
487
486
isSame ('Qwerty Agent v456 ' , $ request2 ->getOptions ()->getUserAgent ());
488
487
isSame ('123 ' , $ request2 ->getHeaders ()['x-custom-header ' ]);
489
488
isSame ('POST ' , $ request2 ->getMethod ());
490
489
491
490
$ 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 ' ));
494
493
isSame ('POST ' , $ jsonBody2 ->find ('method ' ));
495
- isSame ('value ' , $ jsonBody2 ->find ('postData.params .key ' ));
494
+ isSame ('value ' , $ jsonBody2 ->find ('form .key ' ));
496
495
}
497
496
498
497
protected function getClient (array $ options = []): HttpClient
0 commit comments