First of all I've read this related question and this other approach (blog post).
This is my behat.yml
:
default:
suites:
users:
contexts:
- DoctrineFixturesContext
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
- Sanpi\Behatch\Context\RestContext
- Sanpi\Behatch\Context\JsonContext
paths: [%paths.base%/features/users]
extensions:
Behat\Symfony2Extension:
kernel:
env: "test"
debug: "true"
Behat\MinkExtension:
base_url: ''
sessions:
default:
symfony2: ~
Sanpi\Behatch\Extension: ~
Now what I want is to reproduce the behavior of a user, which is:
- User logs in once
- Requests have then an
Authorization
header with the token
The easiest way I've found is to simply mimic the user behavior:
class FeatureContext implements Context, SnippetAcceptingContext
{
private $request;
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* @BeforeScenario @Login
*/
public function login()
{
$d = $this->request->send('POST', '/login_check', ['_username' => 'test', '_password' => 'test']);
$data = json_decode($d->getContent());
$this->request->setHttpHeader('Authorization', 'Bearer '.$data->token);
}
}
The user is previously loaded through fixtures. This works well except that the Authorization
header is not kept through the next steps:
Feature: Get my availables menus
@Login
Scenario: Get my menus
When I send a "GET" request to "/api/menus"
Then the response status code should be 200
The response is 401.
Why is the Authorization header not kept through the scenario? How may I achieve this simple task?