title: Rails - Losing session with Integration Tests and Capybara - CSRF related?
body: <p>I'm using Rails 3.1.0.rc4 and I'm working on doing integration tests with capybara's new Steak-like DSL and Rspec (using Devise authentication)</p>
  
  <p>The issue I'm having is that when I run an integration test, the rack-test driver from capybara seems to just completely lose the user's logged in session, in fact, the session seems to just clear out altogether.</p>
  
  <p>After days of debugging, I'm at a complete loss as to why.  Going line by line through the middleware stack, I believe I've ruled the problem down to something going on in the <code>ActiveRecord::SessionStore</code> that is causing this.  I've read <a href="https://stackoverflow.com/questions/5000333/how-does-rails-csrf-protection-work">here</a> that Rails will clear out a session if it can't validate the CSRF token, which leaves me to believe that I've got something configured wrong, and for some reason this one test is not authenticating the CSRF token correctly.</p>
  
  <p>This is what is in my session_store.rb in the /initializers directory:</p>
  
  <pre><code>MyApp::Application.config.session_store :active_record_store
  </code></pre>
  
  <p>Does anyone who knows about CSRF protection in rails have any leads on why this may be happening? </p>
  
  <p>Also, here are some things to note:</p>
  
  <ul>
  <li>the thing I'm trying to test actually works within the browser itself, only this one test is dropping the session</li>
  <li>the session seems to get dropped after the submission of a form to which the action url is to another server.  I'm using the VCR gem for capturing the requests/responses to this external server in the test, and while I believe I've ruled the external request as the problem, this may have something directly to do with the CSRF token not authenticating, thus clearing out the session.</li>
  <li>other tests involving logging in / using sessions are not dropping sessions</li>
  </ul>
  
  <p>Can anyone give me any leads as to what is going on here exactly, and why the one test just seems to arbitrarily drop its session and fail on me?  I've done lots of debugging and have tried everything I can possible think of.</p>