title: Rspec Rails 3.1 Integration test. How to send post request headers for mobile, http basic authentication and JSON?
body: <p>I have an RSPEC integration test for a Rails 3.1 app that needs to test an api for a mobile client by issuing a POST request with JSON params and a mobile header that needs to use http_basic authentication
  As the request object is not available in an integration test I'm kinda stuck</p>
  
  <p>This is the code I have so far</p>
  
  <pre><code>    it "successfully posts scores" do
  # request.env["HTTP_ACCEPT"] = "application/json" #This causes an error as request is nly available in controller tests
  
        post "scores", :score =&gt; {:mobile_user_id =&gt; @mobile_user.id, :points =&gt; 50, :time_taken =&gt; 7275}.to_json,
             :format =&gt; :json, :user_agent =&gt; 'Mobile', 'HTTP_AUTHORIZATION' =&gt;  get_basic_auth
      end
  </code></pre>
  
  <p>The post request does not recognise that I am using http basic authentication but unsure if the format for json is correct. Any help appreciated</p>
  
  <p>get_basic_auth is a helper me4thod that looks like this</p>
  
  <pre><code>  def get_basic_auth
      user = 'user'
      pw = 'secret'
      ActionController::HttpAuthentication::Basic.encode_credentials user, pw
    end
  </code></pre>
  
  <p>I use a before_filter in my controllers that checks for mobile and http_basic_authentication that looks like this</p>
  
  <pre><code>  def authorize
      logger.debug("@@@@ Authorizing request #{request.inspect}")
      if mobile_device?
          authenticate_or_request_with_http_basic do |username, password|
            username == Mobile::Application.config.mobile_login_name &amp;&amp; Mobile::Application.config.mobile_password
          end
      else
        unless current_user
          redirect_to login_url, :notice =&gt; "Please log in"
        end
      end
    end
  </code></pre>
  
  <p>I get a redirect to login so obviously the mobile header is not being accepted so I have no idea really if any of the other headers are working</p>
  
  <p><strong>UPDATE</strong>
  Figured it out</p>
  
  <pre><code>  post("scores", {:score =&gt; {:mobile_user_id =&gt; @mobile_user.id, :points =&gt; 50, :time_taken =&gt; 7275}}.to_json,
         {"HTTP_USER_AGENT" =&gt; "Mobile", 'HTTP_AUTHORIZATION' =&gt; get_basic_auth, 'HTTP_CONTENT_TYPE' =&gt; "application/json"})
  </code></pre>
  
  <p>Does the trick nicely</p>