title: How to fake HTTP_ACCEPT_LANGUAGE in RSpec controller tests?
body: <p>I recently added some new code to a <code>before_action</code> in my <code>ApplicationController</code>:</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
  
    before_action :set_locale
  
    def set_locale
      I18n.locale = (session[:locale] || params[:locale] || extract_locale_from_accept_language_header).to_s.downcase.presence || I18n.default_locale
    end
  
    private
  
    def extract_locale_from_accept_language_header
      request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
    end
  
  end
  </code></pre>
  
  <p>The problem is that the <code>extract_locale_from_accept_language_header</code> function disrupts all my controller specs (i.e. they all fail now). It seems that RSpec can't detect any <code>HTTP_ACCEPT_LANGUAGE</code>.</p>
  
  <p>Is there a way to fake this behaviour for all my controller specs?</p>
  
  <p>The following <em>does</em> work but is a bit ugly since I would have to add the line <code>request.env...</code> to all my controller tests. And I have many of them.</p>
  
  <pre><code>require 'spec_helper'
  
  describe UsersController do
  
    before :each do
      @user = FactoryGirl.create(:user)
      request.env['HTTP_ACCEPT_LANGUAGE'] = "en" # ugly
    end
  
    ...
  
  end
  </code></pre>
  
  <p>Can anybody help?</p>
  
  <p>Thanks.</p>