我在 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/