在 codeception 中设置用户代理

Set user agent in codeception cept

我在搜索机器人爬虫项目中有一些功能/限制,我想使用代码接受来测试它们,我发现有一个选项可以使用 setHeader 方法设置 headers。就我而言,它不起作用。我收到 [RuntimeException] Call to undefined method FunctionalTester::setHeader 错误。

代码如下:

<?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');

根据 ,我想知道是否必须为每个请求设置用户代理 header。没有方法或配置选项可以永久更改 User-Agent 吗?

您可能会混淆功能测试验收测试。只有验收测试使用 PhpBrowser。功能测试 运行 直接 PHP 代码,无需发出 HTTP 请求。

但是要尝试回答全局设置 headers 的问题...

根据我对 doc page here 的理解,我 认为 您应该能够在配置文件中设置用户代理,如下所示:

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

但我还没有完全使用它。如果您想更正它,请协助编辑我的 post。

就我而言,我是这样工作的:

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

这使得 $_SERVER 变量中的数组值:

HTTP_HEADER_TESTING => "someValue"