Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set user agent in codeception cept

I have some features / limitations in project for search bot crawlers and I would like to test them using codeception and I found there is an option to set headers using setHeader method. In my case it doesn't work though. I'm getting [RuntimeException] Call to undefined method FunctionalTester::setHeader error.

Code bellow:

<?php
/** @var \Codeception\Scenario $scenario */
$I = new FunctionalTester($scenario);

$I->setHeader('User-Agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');

FileDetailPage::of($I)->amOnFileDetail(96972088, 'raped.zip');

According to Authorization header not making it through in Codeception API testing I wonder if I have to set user agent header for every request. Isn't there method or configuration option to change User-Agent permanently?

like image 401
Northys Avatar asked Feb 22 '26 21:02

Northys


2 Answers

You might be confusing Functional tests with Acceptance tests. Only Acceptance tests use PhpBrowser. Functional tests run the PHP code directly without making HTTP requests.

But to try to answer the question of setting headers globally...

Based on my understanding of the doc page here, I think you're supposed to be able to set the user agent in your config file, like this:

class_name: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://ames.dev/
            headers:
                User-Agent: Codeception Custom User Agent
        - \Helper\Acceptance

But I haven't got this fully working yet. Please assist by editing my post if you'd like to correct it.

like image 106
2 revsSimon East Avatar answered Feb 24 '26 09:02

2 revsSimon East


In my case, I made it work like this:

class_name: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://xxxx.xx
            headers:
                header-testing:
                    value: someValue
        - \Helper\Acceptance

This makes array value in the $_SERVER variable:

HTTP_HEADER_TESTING => "someValue"
like image 22
JaK Avatar answered Feb 24 '26 10:02

JaK