title: Testing requests that redirect with mocha/supertest in node
body: <p>I can't seem to get the following integration test to pass in an express project using <a href="http://visionmedia.github.com/mocha/" rel="noreferrer">mocha</a>, <a href="https://github.com/visionmedia/supertest" rel="noreferrer">supertest</a>, and <a href="https://github.com/visionmedia/should.js/" rel="noreferrer">should</a> (and coffeescript).</p>
  
  <hr>
  
  <p><strong>The test</strong></p>
  
  <pre><code>should  = require('should')
  request = require('supertest')
  app     = require('../../app')
  
  describe 'authentication', -&gt;
    describe 'POST /sessions', -&gt;
      describe 'success', (done) -&gt;
        it 'displays a flash', (done) -&gt;
          request(app)
            .post('/sessions')
            .type('form')
            .field('user', 'username')
            .field('password', 'password')
            .end (err, res) -&gt;
              res.text.should.include('logged in')
              done()
  </code></pre>
  
  <hr>
  
  <p><strong>Relevant application code</strong></p>
  
  <pre><code>app.post '/sessions', (req, res) -&gt;
    req.flash 'info', "You are now logged in as #{req.body.user}"
    res.redirect '/login'
  </code></pre>
  
  <hr>
  
  <p><strong>Failure</strong></p>
  
  <pre><code>1) authentication POST /sessions success displays a flash:
     AssertionError: expected 'Moved Temporarily. Redirecting to //127.0.0.1:3456/login' to include 'logged in'
  </code></pre>
  
  <hr>
  
  <p>Obviously, the application code doesn't do anything useful. I'm just trying to get the test to pass.</p>
  
  <p>Putting the expectation (<code>res.text.should.include('logged in')</code>) outside the end function and inside the <code>expect</code> function yields the same results. I've also tried a variation of the function calls, for example removing the <code>.type('form')</code> call, and using <code>.send(user: 'username', password: 'password')</code> instead of the two <code>.field()</code> calls.</p>
  
  <p>If it means anything, sending a curl POST request to the the app when it's running locally yields the same output (<code>Moved Temporarily. Redirecting to //127.0.0.1:3456/login</code>)</p>
  
  <p>I have a feeling this is a trivial error. Possibly something I'm forgetting in the application code or the test code.</p>
  
  <p>Any suggestions?</p>
  
  <p><strong>EDIT 1:</strong> It's also worth noting that when clicking the submit button in the browser I get the expected results (a flash message).</p>
  
  <p><strong>EDIT 2:</strong> Further investigation shows the output of <em>any</em> redirect results in the <code>Moved Temporarily. Redirecting to ...</code> response body. This makes me wonder if there is a problem in the way I'm exporting the app in app.js.</p>
  
  <pre><code>var express = require('express')
  var app = express();
  
  module.exports = app;
  </code></pre>