Supertest : Can't set headers after they are sent
<p>express and mongoose.</p>

<p>For testing I am using supertest, mocha and mockgoose. </p>

<p>When I try to set the auth token for two consecutive tests, the second one fails throwing <code>Can't set headers after they are sent.</code> </p>

<p>The test case is shown below.</p>

<pre><code>var chai = require('chai'),
    expect = chai.expect,
    server = null, 
    request = require('supertest'),
    mockgoose = require('mockgoose'),
    mongoose = require('mongoose');

describe('Organization', () =&gt; {

    var token = 'success_token',
        orgName = 'default_org_name',
        users,
        organization;

    before((done) =&gt; {
        mockgoose(mongoose);
        server = request(require('../../server/server.js'));
    });

    it('should get list of organizations for a proper user', (done) =&gt; {

        server
            .get('/api/organization/')
            .set({token : token})
            .expect(200)
            .end((err, res) =&gt; {
                if (err) return done(err);
                expect(res.body.length).to.equal(1);
                expect(res.body[0].name).to.equal(orgName);
                done();
            });

    });

    it('should send a 500 response for a incorrect user', (done) =&gt; {

        var errorToken = 'error token';

        server
            .get('/api/organization/')
            .set({token : errorToken})
            .expect(500)
            .end((err, res) =&gt; {
                if (err) return done(err);
                done();
            });

    });

    after((done) =&gt; {
        mockgoose.reset();
        done();
    });


});
</code></pre>

<p>The first test case passes, however the second test case fails. 
It throws an error</p>

<pre><code>Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (/home/tricon/aniket/dev/publab/triconpublish_api/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/home/tricon/aniket/dev/publab/triconpublish_api/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/home/tricon/aniket/dev/publab/triconpublish_api/node_modules/express/lib/response.js:249:15)
    at ServerResponse.send (/home/tricon/aniket/dev/publab/triconpublish_api/node_modules/express/lib/response.js:151:21)
    at /home/tricon/aniket/dev/publab/triconpublish_api/server/filter.js:9:1744
    at Query.&lt;anonymous&gt; (/home/tricon/aniket/dev/publab/triconpublish_api/node_modules/mongoose/lib/query.js:2169:28)
    at /home/tricon/aniket/dev/publab/triconpublish_api/node_modules/kareem/index.js:177:19
    at /home/tricon/aniket/dev/publab/triconpublish_api/node_modules/kareem/index.js:109:16
    at _combinedTickCallback (node.js:370:9)
    at process._tickDomainCallback (node.js:425:11)
</code></pre>

<p>I don't get the reason. </p>

<p>You can notice that I am setting two different tokens. One for success scenario and another for error scenario. If I set the same success token in the second test case, it doesn't throw this error.</p>

<p>Please Help !!!</p>