Skip to content

Commit

Permalink
Merge pull request #133 from yashsriv/fix/agent-cookies
Browse files Browse the repository at this point in the history
Add option to assert stored cookies of an agent
  • Loading branch information
keithamus committed Mar 6, 2017
2 parents 12ddde4 + 3ba0206 commit 3a37080
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/http.js
Expand Up @@ -315,7 +315,7 @@ module.exports = function (chai, _) {
/**
* ### .cookie
*
* Assert that a `Request` or `Response` object has a cookie header with a
* Assert that a `Request`, `Response` or `Agent` object has a cookie header with a
* given key, (optionally) equal to value
*
* ```js
Expand All @@ -325,6 +325,9 @@ module.exports = function (chai, _) {
* expect(res).to.have.cookie('session_id');
* expect(res).to.have.cookie('session_id', '1234');
* expect(res).to.not.have.cookie('PHPSESSID');
* expect(agent).to.have.cookie('session_id');
* expect(agent).to.have.cookie('session_id', '1234');
* expect(agent).to.not.have.cookie('PHPSESSID');
* ```
*
* @param {String} parameter name
Expand All @@ -341,9 +344,13 @@ module.exports = function (chai, _) {
header = (getHeader(this._obj, 'cookie') || '').split(';');
}

cookie = Cookie.CookieJar();
cookie.setCookies(header);
cookie = cookie.getCookie(key, Cookie.CookieAccessInfo.All);
if (this._obj instanceof chai.request.agent && this._obj.jar) {
cookie = this._obj.jar.getCookie(key, Cookie.CookieAccessInfo.All);
} else {
cookie = Cookie.CookieJar();
cookie.setCookies(header);
cookie = cookie.getCookie(key, Cookie.CookieAccessInfo.All);
}

if (arguments.length === 2) {
this.assert(
Expand Down
39 changes: 39 additions & 0 deletions test/http.js
Expand Up @@ -341,4 +341,43 @@ describe('assertions', function () {
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');

});

it('#cookie (agent)', function () {
var agent = chai.request.agent();
var cookies = [
'name=value',
'name2=value2; Expires=Wed, 09 Jun 2021 10:18:14 GMT',
'name3=value3; Domain=.somedomain.com',
];
if (agent.jar) // Using superagent.Agent (node)
agent.jar.setCookies(cookies);
else // using superagent.Request (browser)
agent.set('set-cookie', cookies);

agent.should.have.cookie('name');
agent.should.have.cookie('name2');
agent.should.have.cookie('name3');
agent.should.have.cookie('name', 'value');
agent.should.have.cookie('name2', 'value2');
agent.should.have.cookie('name3', 'value3');
agent.should.not.have.cookie('bar');
agent.should.not.have.cookie('name2', 'bar');

(function () {
agent.should.not.have.cookie('name');
}).should.throw('expected cookie \'name\' to not exist');

(function () {
agent.should.have.cookie('foo');
}).should.throw('expected cookie \'foo\' to exist');

(function () {
agent.should.not.have.cookie('name', 'value');
}).should.throw('expected cookie \'name\' to not have value \'value\'');

(function () {
agent.should.have.cookie('name2', 'value');
}).should.throw('expected cookie \'name2\' to have value \'value\' but got \'value2\'');

});
});

0 comments on commit 3a37080

Please sign in to comment.