php - Behat 可重用操作添加标题

标签 php symfony behat

我在 Symfony2 应用程序中使用 Behat。

我做了一个可重用的操作,在某些场景下添加 HTTP header

/**
* @Given I am authenticated as admin
*/
public function iAmAuthenticatedAsAdmin()
{
    $value = 'Bearer xxxxxxxxxxx';

    $response = new Response();
    $response->headers->set('Authorization', $value);
    $response->send();

    return $response;
 }

当我在场景中添加我已通过管理员身份验证步骤时,会调用此操作,但它不会添加我的 header 。像这样

Scenario: I find all my DNS zones
  Given I am authenticated as admin
  And I send a GET request to "/api/dns"
  Then the response code should be 200

如何使用可重用操作在场景中的请求步骤之前添加 HTTP header ? 这可能吗?

谢谢。

最佳答案

我已经找到了做到这一点的方法。

如果您使用WebApiExtension

只需像这样在上下文类中导入 WebApiContext

/**
 * @param BeforeScenarioScope $scope
 *
 * @BeforeScenario
 */
public function gatherContexts(BeforeScenarioScope $scope)
{
    $environment = $scope->getEnvironment();

    $this->webApiContext = $environment->getContext('Behat\WebApiExtension\Context\WebApiContext');
}

您现在只需使用iSetHeaderWithValue:

/**
 * @Given I am authenticated as admin
 */
public function iAmAuthenticatedAsAdmin()
{
    $name = 'Authorization';
    $value = 'Bearer xxxxxxxx';

    $this->webApiContext->iSetHeaderWithValue($name, $value);
}

关于php - Behat 可重用操作添加标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700252/

相关文章:

php - 如何在 php 上做 SoapClient

php - 为什么带有端口 80 的 MAMP (mac) 会导致禁止访问,而端口 8888 工作正常?

php - 无法打开输入文件 : app/console (Symfony 2)

php - 使用 Symfony2 和 Doctrine2 开发 Multi-Tenancy 应用程序的最佳实践

testing - 有没有办法在终端中显示所有自定义步骤定义的列表?

php - 计算输出不理想

PHP json_decode 一个 json_encoded 字符串

php - bundle 如何为 Doctrine ORM 提供新的列类型?

behat - 可以使用Behat排除标签吗?

php - Windows 上的 Behat - 截图