Skip to content

Commit db06fde

Browse files
committed
feat(chromium): add basic authentication
1 parent e8bc519 commit db06fde

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/Modules/ChromiumMultipartFormDataModule.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public function cookies(array $cookies): self
8888
return $this;
8989
}
9090

91+
/**
92+
* Specify the username and password used for Basic HTTP Authentication.
93+
*/
94+
public function basicAuthentication(string $username, string $password): self
95+
{
96+
$this->formValue('basicAuthenticationUsername', $username);
97+
$this->formValue('basicAuthenticationPassword', $password);
98+
99+
return $this;
100+
}
101+
91102
/**
92103
* Overrides the default 'User-Agent' HTTP header.
93104
*/
@@ -99,8 +110,8 @@ public function userAgent(string $userAgent): self
99110
}
100111

101112
/**
102-
* Sets extra HTTP headers that Chromium will send when loading the HTML
103-
* document.
113+
* Sets extra HTTP headers that Chromium will send for all frontend
114+
* requests.
104115
*
105116
* @param array<string,string> $headers
106117
*

tests/Modules/ChromiumPdfTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function (
3838
string|null $waitForExpression = null,
3939
string|null $emulatedMediaType = null,
4040
array $cookies = [],
41+
string|null $basicAuthenticationUsername = null,
42+
string|null $basicAuthenticationPassword = null,
4143
string|null $userAgent = null,
4244
array $extraHttpHeaders = [],
4345
array $failOnHttpStatusCodes = [],
@@ -70,6 +72,8 @@ function (
7072
$waitForExpression,
7173
$emulatedMediaType,
7274
$cookies,
75+
$basicAuthenticationUsername,
76+
$basicAuthenticationPassword,
7377
$userAgent,
7478
$extraHttpHeaders,
7579
$failOnHttpStatusCodes,
@@ -108,6 +112,8 @@ function (
108112
$waitForExpression,
109113
$emulatedMediaType,
110114
$cookies,
115+
$basicAuthenticationUsername,
116+
$basicAuthenticationPassword,
111117
$userAgent,
112118
$extraHttpHeaders,
113119
$failOnHttpStatusCodes,
@@ -146,6 +152,8 @@ function (
146152
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
147153
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
148154
],
155+
'username',
156+
'password',
149157
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
150158
[
151159
'My-Http-Header' => 'HTTP Header content',
@@ -193,6 +201,8 @@ function (
193201
string|null $waitForExpression = null,
194202
string|null $emulatedMediaType = null,
195203
array $cookies = [],
204+
string|null $basicAuthenticationUsername = null,
205+
string|null $basicAuthenticationPassword = null,
196206
string|null $userAgent = null,
197207
array $extraHttpHeaders = [],
198208
array $failOnHttpStatusCodes = [],
@@ -225,6 +235,8 @@ function (
225235
$waitForExpression,
226236
$emulatedMediaType,
227237
$cookies,
238+
$basicAuthenticationUsername,
239+
$basicAuthenticationPassword,
228240
$userAgent,
229241
$extraHttpHeaders,
230242
$failOnHttpStatusCodes,
@@ -265,6 +277,8 @@ function (
265277
$waitForExpression,
266278
$emulatedMediaType,
267279
$cookies,
280+
$basicAuthenticationUsername,
281+
$basicAuthenticationPassword,
268282
$userAgent,
269283
$extraHttpHeaders,
270284
$failOnHttpStatusCodes,
@@ -302,6 +316,8 @@ function (
302316
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
303317
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
304318
],
319+
'username',
320+
'password',
305321
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
306322
[
307323
'My-Http-Header' => 'Http Header content',
@@ -351,6 +367,8 @@ function (
351367
string|null $waitForExpression = null,
352368
string|null $emulatedMediaType = null,
353369
array $cookies = [],
370+
string|null $basicAuthenticationUsername = null,
371+
string|null $basicAuthenticationPassword = null,
354372
string|null $userAgent = null,
355373
array $extraHttpHeaders = [],
356374
array $failOnHttpStatusCodes = [],
@@ -383,6 +401,8 @@ function (
383401
$waitForExpression,
384402
$emulatedMediaType,
385403
$cookies,
404+
$basicAuthenticationUsername,
405+
$basicAuthenticationPassword,
386406
$userAgent,
387407
$extraHttpHeaders,
388408
$failOnHttpStatusCodes,
@@ -428,6 +448,8 @@ function (
428448
$waitForExpression,
429449
$emulatedMediaType,
430450
$cookies,
451+
$basicAuthenticationUsername,
452+
$basicAuthenticationPassword,
431453
$userAgent,
432454
$extraHttpHeaders,
433455
$failOnHttpStatusCodes,
@@ -474,6 +496,8 @@ function (
474496
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
475497
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
476498
],
499+
'username',
500+
'password',
477501
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
478502
[
479503
'My-Http-Header' => 'Http Header content',
@@ -519,6 +543,8 @@ function hydrateChromiumPdfFormData(
519543
string|null $waitForExpression = null,
520544
string|null $emulatedMediaType = null,
521545
array $cookies = [],
546+
string|null $basicAuthenticationUsername = null,
547+
string|null $basicAuthenticationPassword = null,
522548
string|null $userAgent = null,
523549
array $extraHttpHeaders = [],
524550
array $failOnHttpStatusCodes = [],
@@ -593,6 +619,10 @@ function hydrateChromiumPdfFormData(
593619
$chromium->cookies($cookies);
594620
}
595621

622+
if ($basicAuthenticationUsername !== null && $basicAuthenticationPassword !== null) {
623+
$chromium->basicAuthentication($basicAuthenticationUsername, $basicAuthenticationPassword);
624+
}
625+
596626
if ($userAgent !== null) {
597627
$chromium->userAgent($userAgent);
598628
}
@@ -660,6 +690,8 @@ function expectChromiumPdfOptions(
660690
string|null $waitForExpression,
661691
string|null $emulatedMediaType,
662692
array $cookies,
693+
string|null $basicAuthenticationUsername,
694+
string|null $basicAuthenticationPassword,
663695
string|null $userAgent,
664696
array $extraHttpHeaders,
665697
array $failOnHttpStatusCodes,
@@ -716,6 +748,10 @@ function expectChromiumPdfOptions(
716748
expect($body)->toContainFormValue('cookies', $json);
717749
}
718750

751+
expect($body)->unless($basicAuthenticationUsername === null && $basicAuthenticationPassword === null, fn ($body) => [
752+
$body->toContainFormValue('basicAuthenticationUsername', $basicAuthenticationUsername),
753+
$body->toContainFormValue('basicAuthenticationPassword', $basicAuthenticationPassword),
754+
]);
719755
expect($body)->unless($userAgent === null, fn ($body) => $body->toContainFormValue('userAgent', $userAgent));
720756

721757
if (count($extraHttpHeaders) > 0) {

tests/Modules/ChromiumScreenshotTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ function (
2929
string|null $waitForExpression = null,
3030
string|null $emulatedMediaType = null,
3131
array $cookies = [],
32+
string|null $basicAuthenticationUsername = null,
33+
string|null $basicAuthenticationPassword = null,
3234
string|null $userAgent = null,
3335
array $extraHttpHeaders = [],
3436
array $failOnHttpStatusCodes = [],
@@ -50,6 +52,8 @@ function (
5052
$waitForExpression,
5153
$emulatedMediaType,
5254
$cookies,
55+
$basicAuthenticationUsername,
56+
$basicAuthenticationPassword,
5357
$userAgent,
5458
$extraHttpHeaders,
5559
$failOnHttpStatusCodes,
@@ -77,6 +81,8 @@ function (
7781
$waitForExpression,
7882
$emulatedMediaType,
7983
$cookies,
84+
$basicAuthenticationUsername,
85+
$basicAuthenticationPassword,
8086
$userAgent,
8187
$extraHttpHeaders,
8288
$failOnHttpStatusCodes,
@@ -103,6 +109,8 @@ function (
103109
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
104110
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
105111
],
112+
'username',
113+
'password',
106114
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
107115
[
108116
'My-Http-Header' => 'HTTP Header content',
@@ -138,6 +146,8 @@ function (
138146
string|null $waitForExpression = null,
139147
string|null $emulatedMediaType = null,
140148
array $cookies = [],
149+
string|null $basicAuthenticationUsername = null,
150+
string|null $basicAuthenticationPassword = null,
141151
string|null $userAgent = null,
142152
array $extraHttpHeaders = [],
143153
array $failOnHttpStatusCodes = [],
@@ -159,6 +169,8 @@ function (
159169
$waitForExpression,
160170
$emulatedMediaType,
161171
$cookies,
172+
$basicAuthenticationUsername,
173+
$basicAuthenticationPassword,
162174
$userAgent,
163175
$extraHttpHeaders,
164176
$failOnHttpStatusCodes,
@@ -188,6 +200,8 @@ function (
188200
$waitForExpression,
189201
$emulatedMediaType,
190202
$cookies,
203+
$basicAuthenticationUsername,
204+
$basicAuthenticationPassword,
191205
$userAgent,
192206
$extraHttpHeaders,
193207
$failOnHttpStatusCodes,
@@ -214,6 +228,8 @@ function (
214228
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
215229
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
216230
],
231+
'username',
232+
'password',
217233
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
218234
[
219235
'My-Http-Header' => 'Http Header content',
@@ -251,6 +267,8 @@ function (
251267
string|null $waitForExpression = null,
252268
string|null $emulatedMediaType = null,
253269
array $cookies = [],
270+
string|null $basicAuthenticationUsername = null,
271+
string|null $basicAuthenticationPassword = null,
254272
string|null $userAgent = null,
255273
array $extraHttpHeaders = [],
256274
array $failOnHttpStatusCodes = [],
@@ -272,6 +290,8 @@ function (
272290
$waitForExpression,
273291
$emulatedMediaType,
274292
$cookies,
293+
$basicAuthenticationUsername,
294+
$basicAuthenticationPassword,
275295
$userAgent,
276296
$extraHttpHeaders,
277297
$failOnHttpStatusCodes,
@@ -306,6 +326,8 @@ function (
306326
$waitForExpression,
307327
$emulatedMediaType,
308328
$cookies,
329+
$basicAuthenticationUsername,
330+
$basicAuthenticationPassword,
309331
$userAgent,
310332
$extraHttpHeaders,
311333
$failOnHttpStatusCodes,
@@ -341,6 +363,8 @@ function (
341363
new ChromiumCookie('yummy_cookie', 'choco', 'theyummycookie.com'),
342364
new ChromiumCookie('vanilla_cookie', 'vanilla', 'theyummycookie.com', '/', true, true, 'Lax'),
343365
],
366+
'username',
367+
'password',
344368
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)',
345369
[
346370
'My-Http-Header' => 'Http Header content',
@@ -374,6 +398,8 @@ function hydrateChromiumScreenshotFormData(
374398
string|null $waitForExpression = null,
375399
string|null $emulatedMediaType = null,
376400
array $cookies = [],
401+
string|null $basicAuthenticationUsername = null,
402+
string|null $basicAuthenticationPassword = null,
377403
string|null $userAgent = null,
378404
array $extraHttpHeaders = [],
379405
array $failOnHttpStatusCodes = [],
@@ -437,6 +463,10 @@ function hydrateChromiumScreenshotFormData(
437463
$chromium->cookies($cookies);
438464
}
439465

466+
if ($basicAuthenticationUsername !== null && $basicAuthenticationPassword !== null) {
467+
$chromium->basicAuthentication($basicAuthenticationUsername, $basicAuthenticationPassword);
468+
}
469+
440470
if ($userAgent !== null) {
441471
$chromium->userAgent($userAgent);
442472
}
@@ -483,6 +513,8 @@ function expectChromiumScreenshotOptions(
483513
string|null $waitForExpression,
484514
string|null $emulatedMediaType,
485515
array $cookies,
516+
string|null $basicAuthenticationUsername,
517+
string|null $basicAuthenticationPassword,
486518
string|null $userAgent,
487519
array $extraHttpHeaders,
488520
array $failOnHttpStatusCodes,
@@ -510,6 +542,10 @@ function expectChromiumScreenshotOptions(
510542
expect($body)->toContainFormValue('cookies', $json);
511543
}
512544

545+
expect($body)->unless($basicAuthenticationUsername === null && $basicAuthenticationPassword === null, fn ($body) => [
546+
$body->toContainFormValue('basicAuthenticationUsername', $basicAuthenticationUsername),
547+
$body->toContainFormValue('basicAuthenticationPassword', $basicAuthenticationPassword),
548+
]);
513549
expect($body)->unless($userAgent === null, fn ($body) => $body->toContainFormValue('userAgent', $userAgent));
514550

515551
if (count($extraHttpHeaders) > 0) {

0 commit comments

Comments
 (0)