title: What is the proper way to grab Authorization header from controller request object?
body: <p>I have two RSpec tests, a controller spec and a request spec, where I am making a GET request to the index action of the same controller. In both specs I am sending an Authorization header that contains an Oauth2 bearer token.</p>
  
  <p>The problem I'm having is that depending on the type of spec, the header is stored on a different property of the request object. In the case of the request spec, it is available at <code>request.env["Authorization"]</code> and in the case of the controller spec, it is available at <code>request.session["Authorization"]</code>.</p>
  
  <p><strong>Why is "Authorization" stored in different places for different types of specs? Is there somewhere I can find it for both specs?</strong></p>
  
  <p>This <code>bearer_token</code> method is in the parent controller class where I'm grabbing the token from the header:</p>
  
  <p>Works with <code>env</code> in the request specs:</p>
  
  <pre><code>def bearer_token
    pattern = /^Bearer /
    header  = request.env["Authorization"] # &lt;= env
    header.gsub(pattern, '') if header &amp;&amp; header.match(pattern)
  end
  </code></pre>
  
  <p>Works with <code>session</code> in the controller specs:</p>
  
  <pre><code>def bearer_token
    pattern = /^Bearer /
    header  = request.session["Authorization"] # &lt;= session
    header.gsub(pattern, '') if header &amp;&amp; header.match(pattern)
  end
  </code></pre>
  
  <p>Here is my request spec:</p>
  
  <pre><code>describe '' do
    let(:user) { Fabricate(:user) }
  
    describe 'accessing content with valid token' do
      let(:token) { OauthToken.create(user: user) }
      let(:auth_headers) { {
        'Authorization' =&gt; "Bearer #{token.access_token}",
        'HTTPS' =&gt; 'on'
      } }
      before { get api_v2_cats_path, {}, auth_headers }
      specify { response.status.should == 200 }
    end
  end
  </code></pre>
  
  <p>Here is my controller spec</p>
  
  <pre><code>describe Api::V2::CatsController do
    let(:user) { Fabricate(:user) }
  
    describe ".index" do
      let(:token) { OauthToken.create(user: user) }
      let(:auth_headers) { {
        'Authorization' =&gt; "Bearer #{token.access_token}",
        'HTTPS' =&gt; 'on'
      } }
  
      it "should be valid" do
        get :index, { format: :json, page_size: 1 }, auth_headers
        @json = JSON.parse(response.body)
        @json.should_not be_nil
      end
    end
  end
  </code></pre>