Skip to content

Commit 3a37080

Browse files
authored
Merge pull request #133 from yashsriv/fix/agent-cookies
Add option to assert stored cookies of an agent
2 parents 12ddde4 + 3ba0206 commit 3a37080

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

lib/http.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ module.exports = function (chai, _) {
315315
/**
316316
* ### .cookie
317317
*
318-
* Assert that a `Request` or `Response` object has a cookie header with a
318+
* Assert that a `Request`, `Response` or `Agent` object has a cookie header with a
319319
* given key, (optionally) equal to value
320320
*
321321
* ```js
@@ -325,6 +325,9 @@ module.exports = function (chai, _) {
325325
* expect(res).to.have.cookie('session_id');
326326
* expect(res).to.have.cookie('session_id', '1234');
327327
* expect(res).to.not.have.cookie('PHPSESSID');
328+
* expect(agent).to.have.cookie('session_id');
329+
* expect(agent).to.have.cookie('session_id', '1234');
330+
* expect(agent).to.not.have.cookie('PHPSESSID');
328331
* ```
329332
*
330333
* @param {String} parameter name
@@ -341,9 +344,13 @@ module.exports = function (chai, _) {
341344
header = (getHeader(this._obj, 'cookie') || '').split(';');
342345
}
343346

344-
cookie = Cookie.CookieJar();
345-
cookie.setCookies(header);
346-
cookie = cookie.getCookie(key, Cookie.CookieAccessInfo.All);
347+
if (this._obj instanceof chai.request.agent && this._obj.jar) {
348+
cookie = this._obj.jar.getCookie(key, Cookie.CookieAccessInfo.All);
349+
} else {
350+
cookie = Cookie.CookieJar();
351+
cookie.setCookies(header);
352+
cookie = cookie.getCookie(key, Cookie.CookieAccessInfo.All);
353+
}
347354

348355
if (arguments.length === 2) {
349356
this.assert(

test/http.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,43 @@ describe('assertions', function () {
341341
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');
342342

343343
});
344+
345+
it('#cookie (agent)', function () {
346+
var agent = chai.request.agent();
347+
var cookies = [
348+
'name=value',
349+
'name2=value2; Expires=Wed, 09 Jun 2021 10:18:14 GMT',
350+
'name3=value3; Domain=.somedomain.com',
351+
];
352+
if (agent.jar) // Using superagent.Agent (node)
353+
agent.jar.setCookies(cookies);
354+
else // using superagent.Request (browser)
355+
agent.set('set-cookie', cookies);
356+
357+
agent.should.have.cookie('name');
358+
agent.should.have.cookie('name2');
359+
agent.should.have.cookie('name3');
360+
agent.should.have.cookie('name', 'value');
361+
agent.should.have.cookie('name2', 'value2');
362+
agent.should.have.cookie('name3', 'value3');
363+
agent.should.not.have.cookie('bar');
364+
agent.should.not.have.cookie('name2', 'bar');
365+
366+
(function () {
367+
agent.should.not.have.cookie('name');
368+
}).should.throw('expected cookie \'name\' to not exist');
369+
370+
(function () {
371+
agent.should.have.cookie('foo');
372+
}).should.throw('expected cookie \'foo\' to exist');
373+
374+
(function () {
375+
agent.should.not.have.cookie('name', 'value');
376+
}).should.throw('expected cookie \'name\' to not have value \'value\'');
377+
378+
(function () {
379+
agent.should.have.cookie('name2', 'value');
380+
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');
381+
382+
});
344383
});

0 commit comments

Comments
 (0)