8

Consider following code:

    it('Test logout', function (done) {
    async.each(config.www, function (url, callback) {
        var healthCheck = url + '/'
        chai.request(url).get('/')
            .then(function (res) {
                expect(res, healthCheck).to.have.status(200);
                expect(res.text.indexOf(url + '/logout?type=user">Log out</a>'), 'Logout is incorrect').to.be.greaterThan(0);
                callback();
            })
            .catch(function (err) {
                callback(err);
            })
    }, done);
});

The problem is that I need to set a cookie to bypass a splash-page. Any ideas on how I can achieve this?

Homewrecker
  • 1,076
  • 1
  • 15
  • 38

2 Answers2

20

This should work:

chai.request(url)
    .get('/')
    .set('Cookie', 'cookieName=cookieValue;otherName=otherValue')
    .then(...)

Cookies are send to the server as headers value on "Cookie" key, so you just have set them manually.

It works for me with mocha and chai-http, and i was able to recive the cookie values in my koajs v2 app

Pedro Blaszczak
  • 336
  • 3
  • 6
0

To augment Pedro's answer I needed to set the session ID into the cookie. To do this I generated the cookie data with stringify on the inner object and using "cookieName=".

const cValue = "cookieName=" + JSON.stringify({sessionId: sessionId});
chai.request(url)
    .get("/userData")
    .set('Cookie', cValue);
    .then(...)
Bryan
  • 1,103
  • 12
  • 16