title: Login before scenario with Behat and Symfony2
body: <p>First of all I've read <a href="https://stackoverflow.com/questions/8108001/behat-authenticate-symfony2-user">this related question</a> and <a href="http://www.robinvdvleuten.nl/blog/handle-authenticated-users-in-behat-mink/" rel="nofollow noreferrer">this other approach (blog post)</a>. </p>
  
  <p>This is my <code>behat.yml</code>:</p>
  
  <pre><code>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: ~
  </code></pre>
  
  <p>Now what I want is to reproduce the behavior of a user, which is:</p>
  
  <ul>
  <li>User logs in <strong>once</strong></li>
  <li>Requests have then an <code>Authorization</code> header with the token</li>
  </ul>
  
  <p>The easiest way I've found is to simply mimic the user behavior:</p>
  
  <pre><code>class FeatureContext implements Context, SnippetAcceptingContext
  {
      private $request;
  
      public function __construct(Request $request)
      {
          $this-&gt;request = $request; 
      }
  
      /**
       * @BeforeScenario @Login
       */
      public function login() 
      {
          $d = $this-&gt;request-&gt;send('POST', '/login_check', ['_username' =&gt; 'test', '_password' =&gt; 'test']);
          $data = json_decode($d-&gt;getContent());
          $this-&gt;request-&gt;setHttpHeader('Authorization', 'Bearer '.$data-&gt;token);
      }
  }
  </code></pre>
  
  <p>The user is previously loaded through fixtures. This works well except that the <code>Authorization</code> header is not kept through the next steps:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The response is 401. </p>
  
  <p>Why is the Authorization header not kept through the scenario? How may I achieve this simple task?</p>