Skip to content

Commit 9c80605

Browse files
NaktibaldaDavertMik
authored andcommitted
[PhpBrowser] User-Agent can be set in config (#4582)
Headers option is processed by the module instead of passing it to constructor of Guzzle Client
1 parent c0d4d32 commit 9c80605

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

src/Codeception/Module/PhpBrowser.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@
2323
* * Maintainer: **davert**
2424
* * Stability: **stable**
2525
* * Contact: codeception@codeception.com
26-
* * Works with [Guzzle](http://guzzlephp.org/)
2726
*
28-
* *Please review the code of non-stable modules and provide patches if you have issues.*
2927
*
3028
* ## Configuration
3129
*
3230
* * url *required* - start url of your app
31+
* * headers - default headers are set before each test.
3332
* * handler (default: curl) - Guzzle handler to use. By default curl is used, also possible to pass `stream`, or any valid class name as [Handler](http://docs.guzzlephp.org/en/latest/handlers-and-middleware.html#handlers).
3433
* * middleware - Guzzle middlewares to add. An array of valid callables is required.
3534
* * curl - curl options
36-
* * headers - ...
3735
* * cookies - ...
3836
* * auth - ...
3937
* * verify - ...
@@ -83,6 +81,7 @@ class PhpBrowser extends InnerBrowser implements Remote, MultiSession, RequiresP
8381
protected $requiredFields = ['url'];
8482

8583
protected $config = [
84+
'headers' => [],
8685
'verify' => false,
8786
'expect' => false,
8887
'timeout' => 30,
@@ -98,7 +97,6 @@ class PhpBrowser extends InnerBrowser implements Remote, MultiSession, RequiresP
9897
];
9998

10099
protected $guzzleConfigFields = [
101-
'headers',
102100
'auth',
103101
'proxy',
104102
'verify',
@@ -244,6 +242,7 @@ public function _prepareSession()
244242
}
245243
}
246244

245+
$this->headers = $this->config['headers'];
247246
$this->setCookiesFromOptions();
248247

249248
if ($this->isGuzzlePsr7) {

tests/unit/Codeception/Module/PhpBrowserTest.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,6 @@ public function testSubmitFormWithQueries()
315315
$this->module->seeCurrentUrlEquals('/form/example3?validate=yes');
316316
}
317317

318-
public function testHeadersByConfig()
319-
{
320-
$this->module->_setConfig(['headers' => ['xxx' => 'yyyy']]);
321-
$this->module->_initialize();
322-
$this->module->amOnPage('/form1');
323-
324-
if (method_exists($this->module->guzzle, 'getConfig')) {
325-
$headers = $this->module->guzzle->getConfig('headers');
326-
} else {
327-
$headers = $this->module->guzzle->getDefaultOption('headers');
328-
}
329-
$this->assertArrayHasKey('xxx', $headers);
330-
}
331-
332318
public function testHeadersBySetHeader()
333319
{
334320
$this->module->setHeader('xxx', 'yyyy');
@@ -687,4 +673,14 @@ public function testSecondAmOnUrlWithEmptyPath()
687673
$this->module->amOnUrl('http://localhost:8000');
688674
$this->module->dontSee('Lots of valuable data here');
689675
}
676+
677+
public function testSetUserAgentUsingConfig()
678+
{
679+
$this->module->_setConfig(['headers' => ['User-Agent' => 'Codeception User Agent Test 1.0']]);
680+
$this->module->_initialize();
681+
682+
$this->module->amOnPage('/user-agent');
683+
$response = $this->module->grabPageSource();
684+
$this->assertEquals('Codeception User Agent Test 1.0', $response, 'Incorrect user agent');
685+
}
690686
}

0 commit comments

Comments
 (0)