Behat 可重用操作添加 header
Behat reusable actions add header
我在 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;
}
当我在我的场景中添加 I am authenticated as admin
步骤但它没有添加我的 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?
可能吗?
谢谢。
你为什么不把它简单地存储在会话中(提示:会话应该在每个场景中销毁;看看BeforeScenario)并在你需要的时候使用它?
因为我猜它已经添加了,但是在下一次请求时,它消失了,因为生成了一对全新的 request/response(如果我正确理解您的需求)
我已经找到方法了。
如果您正在使用 WebApiExtension
只需像这样在您的上下文中导入 WebApiContext class
/**
* @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);
}
我在 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;
}
当我在我的场景中添加 I am authenticated as admin
步骤但它没有添加我的 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? 可能吗?
谢谢。
你为什么不把它简单地存储在会话中(提示:会话应该在每个场景中销毁;看看BeforeScenario)并在你需要的时候使用它?
因为我猜它已经添加了,但是在下一次请求时,它消失了,因为生成了一对全新的 request/response(如果我正确理解您的需求)
我已经找到方法了。
如果您正在使用 WebApiExtension
只需像这样在您的上下文中导入 WebApiContext class
/**
* @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);
}