Skip to content

Commit 82f5ab2

Browse files
[1.7] Fix extra http headers (#413)
Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
1 parent 113850c commit 82f5ab2

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

src/Browser.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ public function __construct(Connection $connection)
8282

8383
// enable target discovery
8484
$connection->sendMessageSync(new Message('Target.setDiscoverTargets', ['discover' => true]));
85-
86-
// set up http headers
87-
$headers = $connection->getConnectionHttpHeaders();
88-
$connection->sendMessageSync(new Message('Network.setExtraHTTPHeaders', $headers));
8985
}
9086

9187
/**
@@ -188,6 +184,8 @@ public function getTargets()
188184
/**
189185
* @param string $targetId
190186
*
187+
* @throws CommunicationException
188+
*
191189
* @return Page|null
192190
*/
193191
public function getPage($targetId)
@@ -225,6 +223,13 @@ public function getPage($targetId)
225223
// Page.setLifecycleEventsEnabled
226224
$page->getSession()->sendMessageSync(new Message('Page.setLifecycleEventsEnabled', ['enabled' => true]));
227225

226+
// set up http headers
227+
$headers = $this->connection->getConnectionHttpHeaders();
228+
229+
if (\count($headers) > 0) {
230+
$page->setExtraHTTPHeaders($headers);
231+
}
232+
228233
// add prescript
229234
if ($this->pagePreScript) {
230235
$page->addPreScript($this->pagePreScript);

src/Page.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,27 @@ public function setDownloadPath(string $path): void
176176
* @see https://chromedevtools.github.io/devtools-protocol/1-2/Network/#method-setExtraHTTPHeaders
177177
*
178178
* @param array<string, string> $headers
179+
*
180+
* @throws CommunicationException
179181
*/
180182
public function setExtraHTTPHeaders(array $headers = []): void
181183
{
182-
$this->getSession()->sendMessage(new Message(
184+
$response = $this->getSession()->sendMessage(new Message(
183185
'Network.setExtraHTTPHeaders',
184-
$headers
185-
));
186+
['headers' => $headers]
187+
))->waitForResponse();
188+
189+
if (false === $response->isSuccessful()) {
190+
throw new CommunicationException($response->getErrorMessage());
191+
}
186192
}
187193

188194
/**
189195
* @param string $url
190196
* @param array $options
191197
* - strict: make waitForNAvigation to fail if a new navigation is initiated. Default: false
192198
*
193-
* @throws Exception\CommunicationException
199+
* @throws CommunicationException
194200
*
195201
* @return PageNavigation
196202
*/
@@ -213,7 +219,7 @@ public function navigate(string $url, array $options = [])
213219
*
214220
* @param string $expression
215221
*
216-
* @throws Exception\CommunicationException
222+
* @throws CommunicationException
217223
*
218224
* @return PageEvaluation
219225
*/
@@ -849,7 +855,7 @@ public function getCurrentUrl()
849855
/**
850856
* Sets the raw html of the current page.
851857
*
852-
* @throws Exception\CommunicationException
858+
* @throws CommunicationException
853859
*/
854860
public function setHtml(string $html, int $timeout = 3000): void
855861
{
@@ -869,7 +875,7 @@ public function setHtml(string $html, int $timeout = 3000): void
869875
/**
870876
* Gets the raw html of the current page.
871877
*
872-
* @throws Exception\CommunicationException
878+
* @throws CommunicationException
873879
*/
874880
public function getHtml(?int $timeout = null): string
875881
{

tests/BrowserFactoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function testAddHeaders(): void
6565

6666
$factory->addHeader('header_name', 'header_value');
6767
$factory->addHeaders(['header_name2' => 'header_value2']);
68+
$factory->createBrowser()->createPage();
6869

6970
$expected = [
7071
'header_name' => 'header_value',
@@ -84,24 +85,28 @@ public function testOptions(): void
8485

8586
$factory->addHeaders($headers);
8687
$factory->addOptions($options);
88+
$factory->createBrowser()->createPage();
8789

8890
$expected = \array_merge(['headers' => $headers], $options);
8991

9092
$this->assertSame($expected, $factory->getOptions());
9193

9294
// test overwriting
9395
$factory->addOptions($modifiedOptions);
96+
$factory->createBrowser()->createPage();
9497

9598
$expected['userAgent'] = 'foo bar';
9699

97100
$this->assertSame($expected, $factory->getOptions());
98101

99102
// test removing options
100103
$factory->setOptions($modifiedOptions);
104+
$factory->createBrowser()->createPage();
101105

102106
$this->assertSame($modifiedOptions, $factory->getOptions());
103107

104108
$factory->setOptions([]);
109+
$factory->createBrowser()->createPage();
105110

106111
$this->assertSame([], $factory->getOptions());
107112
}

tests/PageTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,14 @@ public function testWaitUntilContainsElement(): void
439439

440440
$this->assertStringContainsString('<div data-name="el"></div>', $page->getHtml());
441441
}
442+
443+
public function testSetExtraHTTPHeaders(): void
444+
{
445+
$factory = new BrowserFactory();
446+
447+
$page = $factory->createBrowser()->createPage();
448+
$page->setExtraHTTPHeaders(['test' => 'test']);
449+
450+
$this->expectNotToPerformAssertions();
451+
}
442452
}

0 commit comments

Comments
 (0)