4
4
5
5
namespace Codeception \Lib \Connector ;
6
6
7
- use Aws \Credentials \Credentials ;
8
- use Aws \Signature \SignatureV4 ;
7
+ use Aws \Credentials \Credentials as AwsCredentials ;
8
+ use Aws \Signature \SignatureV4 as AwsSignatureV4 ;
9
9
use Codeception \Util \Uri ;
10
10
use GuzzleHttp \Client as GuzzleClient ;
11
- use GuzzleHttp \Cookie \CookieJar ;
11
+ use GuzzleHttp \Cookie \CookieJar as GuzzleCookieJar ;
12
12
use GuzzleHttp \Cookie \SetCookie ;
13
13
use GuzzleHttp \Exception \RequestException ;
14
14
use GuzzleHttp \Handler \CurlHandler ;
15
15
use GuzzleHttp \Handler \StreamHandler ;
16
- use GuzzleHttp \HandlerStack ;
16
+ use GuzzleHttp \HandlerStack as GuzzleHandlerStack ;
17
17
use GuzzleHttp \Psr7 \Request as Psr7Request ;
18
18
use GuzzleHttp \Psr7 \Response as Psr7Response ;
19
19
use GuzzleHttp \Psr7 \Uri as Psr7Uri ;
23
23
24
24
class Guzzle extends AbstractBrowser
25
25
{
26
- /**
27
- * @var array
28
- */
29
- protected $ requestOptions = [
26
+ protected array $ requestOptions = [
30
27
'allow_redirects ' => false ,
31
28
'headers ' => [],
32
29
];
33
30
34
- /**
35
- * @var int
36
- */
37
- protected $ refreshMaxInterval = 0 ;
31
+ protected int $ refreshMaxInterval = 0 ;
38
32
39
- /**
40
- * @var \Aws\Credentials\Credentials|null
41
- */
42
- protected $ awsCredentials ;
33
+ protected ?AwsCredentials $ awsCredentials = null ;
43
34
44
- /**
45
- * @var \Aws\Signature\SignatureV4|null
46
- */
47
- protected $ awsSignature ;
35
+ protected ?AwsSignatureV4 $ awsSignature = null ;
48
36
49
- /**
50
- * @var GuzzleClient
51
- */
52
- protected $ client ;
37
+ protected ?GuzzleClient $ client = null ;
53
38
54
39
/**
55
40
* Sets the maximum allowable timeout interval for a meta tag refresh to
@@ -108,13 +93,12 @@ public function setAuth(string $username, string $password, string $type = 'basi
108
93
unset($ this ->requestOptions ['auth ' ]);
109
94
return ;
110
95
}
96
+
111
97
$ this ->requestOptions ['auth ' ] = [$ username , $ password , $ type ];
112
98
}
113
99
114
100
/**
115
101
* Taken from Mink\BrowserKitDriver
116
- *
117
- * @return BrowserKitResponse
118
102
*/
119
103
protected function createResponse (Psr7Response $ psr7Response ): BrowserKitResponse
120
104
{
@@ -126,6 +110,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
126
110
if (isset ($ headers ['Content-Type ' ])) {
127
111
$ contentType = reset ($ headers ['Content-Type ' ]);
128
112
}
113
+
129
114
if (!$ contentType ) {
130
115
$ contentType = 'text/html ' ;
131
116
}
@@ -134,6 +119,7 @@ protected function createResponse(Psr7Response $psr7Response): BrowserKitRespons
134
119
if (preg_match ('#<meta[^>]+charset *= *[" \']?([a-zA-Z\-0-9]+)#i ' , $ body , $ matches )) {
135
120
$ contentType .= ';charset= ' . $ matches [1 ];
136
121
}
122
+
137
123
$ headers ['Content-Type ' ] = [$ contentType ];
138
124
}
139
125
@@ -182,17 +168,19 @@ protected function getAbsoluteUri($uri)
182
168
183
169
return Uri::appendPath ((string )$ baseUri , $ uri );
184
170
}
171
+
185
172
// relative url
186
173
if (!$ this ->getHistory ()->isEmpty ()) {
187
174
return Uri::mergeUrls ((string )$ this ->getHistory ()->current ()->getUri (), $ uri );
188
175
}
189
176
}
177
+
190
178
return Uri::mergeUrls ((string )$ baseUri , $ uri );
191
179
}
192
180
193
181
protected function doRequest ($ request )
194
182
{
195
- /** @var $request BrowserKitRequest **/
183
+ /** @var $request BrowserKitRequest **/
196
184
$ guzzleRequest = new Psr7Request (
197
185
$ request ->getMethod (),
198
186
$ request ->getUri (),
@@ -217,15 +205,20 @@ protected function doRequest($request)
217
205
} else {
218
206
$ response = $ this ->client ->send ($ guzzleRequest , $ options );
219
207
}
220
- } catch (RequestException $ e ) {
221
- if (!$ e ->hasResponse ()) {
222
- throw $ e ;
208
+ } catch (RequestException $ exception ) {
209
+ if (!$ exception ->hasResponse ()) {
210
+ throw $ exception ;
223
211
}
224
- $ response = $ e ->getResponse ();
212
+
213
+ $ response = $ exception ->getResponse ();
225
214
}
215
+
226
216
return $ this ->createResponse ($ response );
227
217
}
228
218
219
+ /**
220
+ * @return array<string, mixed>
221
+ */
229
222
protected function extractHeaders (BrowserKitRequest $ request ): array
230
223
{
231
224
$ headers = [];
@@ -240,6 +233,7 @@ protected function extractHeaders(BrowserKitRequest $request): array
240
233
$ headers [$ header ] = $ val ;
241
234
}
242
235
}
236
+
243
237
return $ headers ;
244
238
}
245
239
@@ -255,9 +249,11 @@ protected function extractFormData(BrowserKitRequest $browserKitRequest): ?array
255
249
if (isset ($ headers ['HTTP_CONTENT_TYPE ' ]) && $ headers ['HTTP_CONTENT_TYPE ' ] !== 'application/x-www-form-urlencoded ' ) {
256
250
return null ;
257
251
}
252
+
258
253
if ($ browserKitRequest ->getContent () !== null ) {
259
254
return null ;
260
255
}
256
+
261
257
return $ browserKitRequest ->getParameters ();
262
258
}
263
259
@@ -275,6 +271,7 @@ protected function extractMultipartFormData(BrowserKitRequest $browserKitRequest
275
271
foreach ($ browserKitRequest ->getParameters () as $ k => $ parameter ) {
276
272
$ parts = $ this ->formatMultipart ($ parts , $ k , $ parameter );
277
273
}
274
+
278
275
return $ parts ;
279
276
}
280
277
@@ -284,8 +281,10 @@ protected function formatMultipart($parts, $key, $value)
284
281
foreach ($ value as $ subKey => $ subValue ) {
285
282
$ parts = array_merge ($ this ->formatMultipart ([], $ key .sprintf ('[%s] ' , $ subKey ), $ subValue ), $ parts );
286
283
}
284
+
287
285
return $ parts ;
288
286
}
287
+
289
288
$ parts [] = ['name ' => $ key , 'contents ' => (string ) $ value ];
290
289
return $ parts ;
291
290
}
@@ -313,6 +312,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
313
312
'content-type ' => $ info ['type ' ]
314
313
];
315
314
}
315
+
316
316
$ files [] = $ file ;
317
317
}
318
318
} else {
@@ -329,7 +329,7 @@ protected function mapFiles($requestFiles, $arrayName = ''): array
329
329
return $ files ;
330
330
}
331
331
332
- protected function extractCookies ($ host ): \ GuzzleHttp \ Cookie \ CookieJar
332
+ protected function extractCookies ($ host ): GuzzleCookieJar
333
333
{
334
334
$ jar = [];
335
335
$ cookies = $ this ->getCookieJar ()->all ();
@@ -338,34 +338,41 @@ protected function extractCookies($host): \GuzzleHttp\Cookie\CookieJar
338
338
if (!$ setCookie ->getDomain ()) {
339
339
$ setCookie ->setDomain ($ host );
340
340
}
341
+
341
342
$ jar [] = $ setCookie ;
342
343
}
343
- return new CookieJar (false , $ jar );
344
+
345
+ return new GuzzleCookieJar (false , $ jar );
344
346
}
345
347
346
- public static function createHandler ($ handler ): \ GuzzleHttp \ HandlerStack
348
+ public static function createHandler ($ handler ): GuzzleHandlerStack
347
349
{
348
- if ($ handler instanceof HandlerStack ) {
350
+ if ($ handler instanceof GuzzleHandlerStack ) {
349
351
return $ handler ;
350
352
}
353
+
351
354
if ($ handler === 'curl ' ) {
352
- return HandlerStack ::create (new CurlHandler ());
355
+ return GuzzleHandlerStack ::create (new CurlHandler ());
353
356
}
357
+
354
358
if ($ handler === 'stream ' ) {
355
- return HandlerStack ::create (new StreamHandler ());
359
+ return GuzzleHandlerStack ::create (new StreamHandler ());
356
360
}
361
+
357
362
if (is_string ($ handler ) && class_exists ($ handler )) {
358
- return HandlerStack ::create (new $ handler );
363
+ return GuzzleHandlerStack ::create (new $ handler );
359
364
}
365
+
360
366
if (is_callable ($ handler )) {
361
- return HandlerStack ::create ($ handler );
367
+ return GuzzleHandlerStack ::create ($ handler );
362
368
}
363
- return HandlerStack::create ();
369
+
370
+ return GuzzleHandlerStack::create ();
364
371
}
365
372
366
373
public function setAwsAuth ($ config ): void
367
374
{
368
- $ this ->awsCredentials = new Credentials ($ config ['key ' ], $ config ['secret ' ]);
369
- $ this ->awsSignature = new SignatureV4 ($ config ['service ' ], $ config ['region ' ]);
375
+ $ this ->awsCredentials = new AwsCredentials ($ config ['key ' ], $ config ['secret ' ]);
376
+ $ this ->awsSignature = new AwsSignatureV4 ($ config ['service ' ], $ config ['region ' ]);
370
377
}
371
378
}
0 commit comments