title: How to do request spec for JWT authenticate app using RSpec
body: <p>I have a Rails 5 API only app and using knock to do JWT authenticate.</p>
  
  <p>After complete the model and model spec, I start to do the request spec.</p>
  
  <p>But I have no idea how to complete the authentication inside the request spec in the right way,</p>
  
  <p>My users controller,</p>
  
  <pre><code>module V1
    class UsersController &lt; ApplicationController
      before_action :authenticate_user, except: [:create]
    end
  end
  </code></pre>
  
  <p>Application controller,</p>
  
  <pre><code>class ApplicationController &lt; ActionController::API
    include Knock::Authenticable
    include ActionController::Serialization
  end
  </code></pre>
  
  <p>My stupidest solution (call the get token request to get the JWT before the rest request),</p>
  
  <pre><code>context 'when the request contains an authentication header' do
    it 'should return the user info' do
      user  = create(:user)
      post '/user_token', params: {"auth": {"email": user.email, "password": user.password }}
      body = response.body
      puts body # {"jwt":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0ODgxMDgxMDYsInN1YiI6MX0.GDBHPzbivclJfwSTswXhDkV0TCFCybJFDrjBnLIfN3Q"}
      # use the retrieved JWT for future requests
    end
  end
  </code></pre>
  
  <p>Any advice is appreciated.</p>