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?
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.
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.
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.
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).
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()).
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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With