title: Devise, cucumber, and capybara not authenticating on JSON request
body: <p>I am using backbone.js with rails.  My controller accepts and returns JSON.  I had all my tests working fine until I added in devise authentication.  I cannot get cucumber to stay authenticated.</p>
  
  <p>I have tried everything mentioned on:</p>
  
  <ul>
  <li><a href="https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Cucumber" rel="nofollow noreferrer">devise wiki</a></li>
  <li><a href="https://stackoverflow.com/questions/1271788/session-variables-with-cucumber-stories">here</a> - standard login (go to page, fill in form, click button), setting cookie and having a special application before filter, doing the latter but in rack (couldn't get this to even run)</li>
  <li>doing the above cookie/before_filter using <a href="https://stackoverflow.com/questions/5144724/rails-cucumber-capybara-session-is-not-persisted">this set_cookie method</a></li>
  </ul>
  
  <p>All to no avail.  The login returns fine (200), but when I send my request I get a 401 response.</p>
  
  <p>Here is my controller code:</p>
  
  <pre><code>class ContactsController &lt; ApplicationController
    before_filter :authenticate_user!
  
    ### backbone
    def index
      render :json =&gt; current_user.person.contacts
    end
  
    def show
      render :json =&gt;  current_user.person.contacts.find(params[:id])
    end
  
    def create
      contact = current_user.person.contacts.create! params
      render :json =&gt; contact
    end
  
    def update
      contact =  current_user.person.contacts.find(params[:id])
      contact.update_attributes! params
      render :json =&gt; contact
    end
  
   def destroy
      contact =  current_user.person.contacts.find(params[:id]) 
      contact.destroy
      render :json =&gt; contact  #TODO: change to render nothing
    end
  </code></pre>
  
  <p>Here is one cucumber feature:</p>
  
  <pre><code>Scenario: View contacts
  Given I am logged in as "testy@testing.com"
  And I send and accept JSON
  And the following contacts:
    |name|
    |name 1|
    |name 2|
    |name 3|
    |name 4|
  When I send a GET request for "/contacts"
  Then the response should be "200"
  And the JSON response should be an array with 4 "name" elements
  And I should get the following contacts:
    |name|
    |name 1|
    |name 2|
    |name 3|
    |name 4|
  </code></pre>
  
  <p>The JSON methods are using Rack directly.  I suspect this is where my problems lie, but I don't know how to get past it.</p>
  
  <p>Here are the cuke JSON steps:</p>
  
  <pre><code>World(Rack::Test::Methods)
  
  Given /^I send and accept JSON$/ do
    header 'Accept', 'application/json'
    header 'Content-Type', 'application/json'
  end
  
  When /^I send a GET request for "([^\"]*)"$/ do |path|
    get path
  end
  
  When /^I send a POST request to "([^\"]*)" with the following:$/ do |path, table|
    params = table.hashes.flatten[0].to_json
    post path, params
  end
  
  When /^I send a PUT request to "([^\"]*)" with the following:$/ do |path, table|
    params = table.hashes.flatten[0].to_json
    put path, params
  end
  
  When /^I send a DELETE request to "([^\"]*)"$/ do |path|
    delete path
  end
  
  Then /^the response should be "([^\"]*)"$/ do |status|
    last_response.status.should == status.to_i
  end
  
  Then /^the JSON response should have 1 "([^\"]*)" element$/ do |name|
    page = JSON.parse(last_response.body)
    page[name].should be
  end
  
  Then /^the JSON response should be an array with (\d+) "([^\"]*)" elements$/ do |number_of_children, name|
    page = JSON.parse(last_response.body)
    page.map { |d| d[name] }.length.should == number_of_children.to_i
  end
  </code></pre>