title: How to test or bypass Basic Auth in Rails 5 testing of controllers
body: <p>I would like to test controllers in Rails 5 for which Basic Authentication is activated.
  The way explained in the <a href="https://guides.rubyonrails.org/testing.html#what-to-include-in-your-functional-tests" rel="noreferrer">current official instruction</a> (as of 2018-10-14) does not work for some reason.
  The Q&amp;A "<a href="https://stackoverflow.com/questions/1165478/testing-http-basic-auth-in-rails-2-2">Testing HTTP Basic Auth in Rails 2.2+</a>" seems too old for Rails 5 (at least for the default).</p>
  
  <p>Here is a simplified example to reproduce the case.<br>
  I made an Article model and related resources by scaffold from a fresh install of Rails (latest stable version 5.2.1):</p>
  
  <pre><code>bin/rails g scaffold Article title:string content:text
  </code></pre>
  
  <p>and added the basic auth function to the controller, following the <a href="https://guides.rubyonrails.org/action_controller_overview.html#http-basic-authentication" rel="noreferrer">official guide</a>; then the ArticlesController is like this, which certainly works:</p>
  
  <pre><code># /app/controllers/articles_controller.rb
  class ArticlesController &lt; ApplicationController
    http_basic_authenticate_with name: "user1", password: "pass"
    before_action :set_article, only: [:show, :edit, :update, :destroy]
  
    def index
      @articles = Article.all
    end
  end
  </code></pre>
  
  <p>The <a href="https://guides.rubyonrails.org/testing.html#what-to-include-in-your-functional-tests" rel="noreferrer">official instruction</a> explains the way to test basic authentication; you add
  <code>request.headers['Authorization']</code> in the <code>setup</code> block in the test file of the controller, which I did:</p>
  
  <pre><code># /test/controllers/articles_controller_test.rb
  require 'test_helper'
  
  class ArticlesControllerTest &lt; ActionDispatch::IntegrationTest
    setup do
      request.headers['Authorization'] =
        ActionController::HttpAuthentication::Basic.encode_credentials("user1", "pass")
      @article = articles(:one)
    end
  
    test "should get index" do
      get articles_url
      assert_response :success
    end
  end
  </code></pre>
  
  <p>However, <code>bin/rails test</code> fails as follows:</p>
  
  <pre><code># Running:
  
  E
  
  Error:
  ArticlesControllerTest#test_should_get_index:
  NoMethodError: undefined method `headers' for nil:NilClass
      test/controllers/articles_controller_test.rb:5:in `block in &lt;class:ArticlesControllerTest&gt;'
  
  bin/rails test test/controllers/articles_controller_test.rb:10
  </code></pre>
  
  <p>Clearly, <code>request</code> method returns nil, and hence <code>request.headers['Authorization']</code> fails.  It is the same if the statement is placed at the top of 'testing-index' block instead.</p>
  
  <p>I have found <code>request</code> returns a proper value <strong>after</strong> <code>get articles_url</code> is run, but it is too late by that time; I mean, Authentication has already failed by that time (obviously).  With some googling, it seems some people use <code>@request</code> and <code>@response</code> for the purpose instead, but I have also found the they are exactly in the same situation as <code>request</code> (expectedly?), that is, they are nil before <code>get</code>.</p>
  
  <p>What is the way to bypass or test Basic Auth in testing of controllers or integration tests in Rails 5?</p>
  
  <p><strong>EDIT:</strong><br>
  "The <a href="https://guides.rubyonrails.org/testing.html#what-to-include-in-your-functional-tests" rel="noreferrer"><strong>current</strong> official instruction</a> (as of 2018-10-14)" is apparently wrong.  See <a href="https://stackoverflow.com/questions/52803228/how-to-test-or-bypass-basic-auth-in-rails-5-testing-of-controllers/52803443#52803443">the answer</a>.</p>