chai-http icon indicating copy to clipboard operation
chai-http copied to clipboard

Is there a way to share cookies between multiple agents?

Open rajchandra3 opened this issue 4 years ago • 0 comments

Hey, I am trying to write tests that involve making requests to hosted servers (via URL ) say(via agent1 and agent2) & local server(agent3). I want to fetch cookies from a server in agent1 and share that with agent2 and agent3. Is there a way to do it?

Sharing some code snippets for clarity in NodeJs.

import chai from 'chai';
import chaiHttp from 'chai-http';
import app from 'app.js';

let agent1 = chai.request.agent('https://server1.com/');
let agent2 = chai.request.agent('https://server2.com/');
let agent3 = chai.request.agent(app);

const getCookie = async () => {
	return await agent1
		.post('/signin')
		.send({
			email: '[email protected]',
			password: '***',
		});
};

describe('Do Checks', () => {
    it('random', async (done) => {
		const response = await getCookie();
		await agent2
			.post('/update/username')
			.send({
				"username": "rajchandra3"
			})
			.then(res=>{
			         //code is never executed
                                  //do something with res
			})
			.catch(e=>{
				throw e; //throws error because cookies are missing 
			});
		done();
    });
})

Can I share cookies between these agents?

rajchandra3 avatar Dec 01 '20 19:12 rajchandra3