@@ -29,7 +29,7 @@ public function testFetch(
29
29
30
30
try {
31
31
$ client = new Client ();
32
- foreach ($ headers as $ key => $ value ) {
32
+ foreach ($ headers as $ key => $ value ) {
33
33
$ client ->addHeader ($ key , $ value );
34
34
}
35
35
@@ -46,11 +46,11 @@ public function testFetch(
46
46
if ($ resp ->getStatusCode () === 200 ) { // If the response is OK
47
47
$ respData = $ resp ->json (); // Convert body to array
48
48
$ this ->assertEquals ($ respData ['method ' ], $ method ); // Assert that the method is equal to the response's method
49
- if ($ method != Client::METHOD_GET ) {
50
- if (empty ($ body )) { // if body is empty then response body should be an empty string
49
+ if ($ method != Client::METHOD_GET ) {
50
+ if (empty ($ body )) { // if body is empty then response body should be an empty string
51
51
$ this ->assertEquals ($ respData ['body ' ], '' );
52
52
} else {
53
- if ($ headers ['content-type ' ] != "application/x-www-form-urlencoded " ) {
53
+ if ($ headers ['content-type ' ] != "application/x-www-form-urlencoded " ) {
54
54
$ this ->assertEquals ( // Assert that the body is equal to the response's body
55
55
$ respData ['body ' ],
56
56
json_encode ($ body ) // Converting the body to JSON string
@@ -65,15 +65,15 @@ public function testFetch(
65
65
); // Assert that the args are equal to the response's args
66
66
$ respHeaders = json_decode ($ respData ['headers ' ], true ); // Converting the headers to array
67
67
$ host = $ respHeaders ['Host ' ];
68
- if (array_key_exists ('Content-Type ' , $ respHeaders )) {
68
+ if (array_key_exists ('Content-Type ' , $ respHeaders )) {
69
69
$ contentType = $ respHeaders ['Content-Type ' ];
70
70
} else {
71
71
$ contentType = $ respHeaders ['content-type ' ];
72
72
}
73
73
$ contentType = explode ('; ' , $ contentType )[0 ];
74
74
$ this ->assertEquals ($ host , $ url ); // Assert that the host is equal to the response's host
75
- if (empty ($ headers )) {
76
- if (empty ($ body )) {
75
+ if (empty ($ headers )) {
76
+ if (empty ($ body )) {
77
77
$ this ->assertEquals ($ contentType , 'application/x-www-form-urlencoded ' );
78
78
} else {
79
79
$ this ->assertEquals ($ contentType , 'application/json ' );
@@ -113,7 +113,7 @@ public function testSendFile(
113
113
}
114
114
if ($ resp ->getStatusCode () === 200 ) { // If the response is OK
115
115
$ respData = $ resp ->json (); // Convert body to array
116
- if (isset ($ respData ['method ' ])) {
116
+ if (isset ($ respData ['method ' ])) {
117
117
$ this ->assertEquals ($ respData ['method ' ], Client::METHOD_POST );
118
118
} // Assert that the method is equal to the response's method
119
119
$ this ->assertEquals ($ respData ['url ' ], 'localhost:8000 ' ); // Assert that the url is equal to the response's url
@@ -151,7 +151,7 @@ public function testGetFile(
151
151
try {
152
152
$ client = new Client ();
153
153
$ resp = $ client ->fetch (
154
- url: 'localhost:8000/ ' . $ type ,
154
+ url: 'localhost:8000/ ' . $ type ,
155
155
method: Client::METHOD_GET ,
156
156
body: [],
157
157
query: []
@@ -163,7 +163,7 @@ public function testGetFile(
163
163
if ($ resp ->getStatusCode () === 200 ) { // If the response is OK
164
164
$ data = fopen ($ path , 'rb ' );
165
165
$ size = filesize ($ path );
166
- if ($ data && $ size ) {
166
+ if ($ data && $ size ) {
167
167
$ contents = fread ($ data , $ size );
168
168
fclose ($ data );
169
169
$ this ->assertEquals ($ resp ->getBody (), $ contents ); // Assert that the body is equal to the expected file contents
@@ -200,6 +200,77 @@ public function testRedirect(): void
200
200
echo "Please configure your PHP inbuilt SERVER " ;
201
201
}
202
202
}
203
+
204
+ /**
205
+ * Test setting and getting the timeout.
206
+ * @return void
207
+ */
208
+ public function testSetGetTimeout (): void
209
+ {
210
+ $ client = new Client ();
211
+ $ timeout = 10 ;
212
+
213
+ $ client ->setTimeout ($ timeout );
214
+
215
+ $ this ->assertEquals ($ timeout , $ client ->getTimeout ());
216
+ }
217
+
218
+ /**
219
+ * Test setting and getting the allowRedirects flag.
220
+ * @return void
221
+ */
222
+ public function testSetGetAllowRedirects (): void
223
+ {
224
+ $ client = new Client ();
225
+ $ allowRedirects = true ;
226
+
227
+ $ client ->setAllowRedirects ($ allowRedirects );
228
+
229
+ $ this ->assertEquals ($ allowRedirects , $ client ->getAllowRedirects ());
230
+ }
231
+
232
+ /**
233
+ * Test setting and getting the maxRedirects.
234
+ * @return void
235
+ */
236
+ public function testSetGetMaxRedirects (): void
237
+ {
238
+ $ client = new Client ();
239
+ $ maxRedirects = 5 ;
240
+
241
+ $ client ->setMaxRedirects ($ maxRedirects );
242
+
243
+ $ this ->assertEquals ($ maxRedirects , $ client ->getMaxRedirects ());
244
+ }
245
+
246
+ /**
247
+ * Test setting and getting the connectTimeout.
248
+ * @return void
249
+ */
250
+ public function testSetGetConnectTimeout (): void
251
+ {
252
+ $ client = new Client ();
253
+ $ connectTimeout = 5 ;
254
+
255
+ $ client ->setConnectTimeout ($ connectTimeout );
256
+
257
+ $ this ->assertEquals ($ connectTimeout , $ client ->getConnectTimeout ());
258
+ }
259
+
260
+ /**
261
+ * Test setting and getting the userAgent.
262
+ * @return void
263
+ */
264
+ public function testSetGetUserAgent (): void
265
+ {
266
+ $ client = new Client ();
267
+ $ userAgent = "MyCustomUserAgent/1.0 " ;
268
+
269
+ $ client ->setUserAgent ($ userAgent );
270
+
271
+ $ this ->assertEquals ($ userAgent , $ client ->getUserAgent ());
272
+ }
273
+
203
274
/**
204
275
* Data provider for testFetch
205
276
* @return array<string, array<mixed>>
@@ -258,12 +329,12 @@ public function sendFileDataSet(): array
258
329
{
259
330
return [
260
331
'imageFile ' => [
261
- __DIR__ . '/resources/logo.png ' ,
332
+ __DIR__ . '/resources/logo.png ' ,
262
333
'image/png ' ,
263
334
'logo.png '
264
335
],
265
336
'textFile ' => [
266
- __DIR__ . '/resources/test.txt ' ,
337
+ __DIR__ . '/resources/test.txt ' ,
267
338
'text/plain ' ,
268
339
'text.txt '
269
340
],
@@ -277,11 +348,11 @@ public function getFileDataset(): array
277
348
{
278
349
return [
279
350
'imageFile ' => [
280
- __DIR__ . '/resources/logo.png ' ,
351
+ __DIR__ . '/resources/logo.png ' ,
281
352
'image '
282
353
],
283
354
'textFile ' => [
284
- __DIR__ . '/resources/test.txt ' ,
355
+ __DIR__ . '/resources/test.txt ' ,
285
356
'text '
286
357
],
287
358
];
0 commit comments