Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a cookie on the request with chai

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?

like image 247
Homewrecker Avatar asked Jun 27 '16 11:06

Homewrecker


People also ask

Can you set cookies with a GET request?

Also I would say it is perfectly acceptable to change or set a cookie in response for the GET request because you just return some data.

How do I send cookies from client to server?

To send cookies to the server, you need to add the "Cookie: name=value" header to your request. To send multiple Cookies in one cookie header, you can separate them with semicolons. In this Send Cookies example, we are sending HTTP cookies to the ReqBin echo URL.

What is mocha chai?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

How to keep cookies with each request in Chai?

Promise=require('q');}varchai=require('chai');chai.use(require('chai-http')); Retaining cookies with each request Sometimes you need to keep cookies from one request, and send them with the next (for example, when you want to login with the first request, then access an authenticated-only resource later).

How to open the server for incoming requests in Chai?

Note:This feature is only supported on Node.js, not in web browsers. chai.request(app).get('/') When passing an appto request; it will automatically open the server for incoming requests (by calling listen()) and, once a request has been made the server will automatically shut down (by calling .close()).

How do I set a cookie?

Setting a cookie uses the following syntax: Let’s break this down into its components: document.cookie is the command used to create a new cookie. ‘newCookie’ is a string that sets the cookie value. It has its own syntax to be aware of: name=value For readability reasons name should imply what the cookie stores (e.g. username)

Can I set my own cookies for ihttpclientfactory?

Note if propagation of the request cookies is not what you need (sorry Op) you can set your own cookies when configuring the client factory. Inject the IHttpClientFactory into your controller or middleware.


1 Answers

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

like image 198
Pedro Blaszczak Avatar answered Oct 10 '22 15:10

Pedro Blaszczak