使用chai在请求上设置cookie

Hom*_*ker 4 javascript cookies chai

考虑以下代码:

    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);
});
Run Code Online (Sandbox Code Playgroud)

问题是我需要设置一个cookie来绕过一个启动页面.有关如何实现这一目标的任何想法?

小智 8

这应该工作:

chai.request(url)
    .get('/')
    .set('Cookie', 'cookieName=cookieValue;otherName=otherValue')
    .then(...)
Run Code Online (Sandbox Code Playgroud)

Cookie作为"Cookie"键上的标题值发送到服务器,因此您只需手动设置它们.

它适用于我的mocha和chai-http,我能够在我的koajs v2应用程序中重现cookie值