Skip to content

Commit

Permalink
Merge pull request webdriverio#117 from christian-bromann/issue/117
Browse files Browse the repository at this point in the history
Implement protocol binding for DELETE /session/:sessionId/cookie
  • Loading branch information
vvo committed Dec 24, 2013
2 parents 8388523 + 065f774 commit 33c51b2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 66 deletions.
18 changes: 6 additions & 12 deletions lib/commands/deleteCookie.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
module.exports = function deleteCookie (name, callback) {

if(typeof name == 'function') {
if(typeof name === 'function') {
callback = name;
name = null;
}

var requestOptions = {
path: '/session/:sessionId/cookie' + (name ? '/:name' : ''),
method: 'DELETE'
};
this.cookie('DELETE', name, function(err,result) {

var self = this;

if(name) {
requestOptions.path = requestOptions.path.replace(/:name/, name);
}

this.requestHandler.create(requestOptions,{},callback);
if(typeof callback === 'function') {
callback(err, result);
}

});
};
2 changes: 1 addition & 1 deletion lib/commands/getCookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function getCookie (name, callback) {
name = null;
}

this.cookie(function(err,result) {
this.cookie('GET', null, function(err,result) {

if(err === null && result.value) {

Expand Down
15 changes: 13 additions & 2 deletions lib/commands/setCookie.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
module.exports = function setCookie (cookieObj, callback) {
this.cookie(cookieObj, function(err, result) {
callback(err, result && result.value);

// throw error if no cookie object is given
if(typeof cookieObj !== 'object') {
throw 'Please specify a cookie object to set (see http://code.google.com/p/selenium/wiki/JsonWireProtocol#Cookie_JSON_Object for documentation.';
}

this.cookie('POST', cookieObj, function(err, result) {

if(typeof callback === 'function') {
callback(err, result && result.value);
}

});

};
32 changes: 20 additions & 12 deletions lib/protocol/cookie.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
module.exports = function cookie (cookieObj, callback) {
/**
* protocol bindings for all cookie operations
* @ref http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie
*/

if(typeof cookieObj === 'function') {
callback = cookieObj;
cookieObj = null;
}
module.exports = function cookie (method, args, callback) {

// set default options
var data = {},
requestOptions = {
path: '/session/:sessionId/cookie',
method: method.toUpperCase()
};

var requestOptions = {
path: '/session/:sessionId/cookie',
method: cookieObj ? 'POST' : 'GET'
};

if(cookieObj && !('secure' in cookieObj)) {
cookieObj.secure = false;
// set cookie param for POST method
if(requestOptions.method === 'POST' && typeof args === 'object') {
data.cookie = args;
}

var data = cookieObj ? { cookie: cookieObj } : {};
// add cookie name tp path URL to delete a specific cookie object
if(requestOptions.method === 'DELETE' && typeof args === 'string') {
requestOptions.path += '/' + args;
}

// create request
this.requestHandler.create(requestOptions,data,callback);

};
81 changes: 42 additions & 39 deletions test/spec/cookies.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
/*module.exports = function(testpageURL,testpageTitle,assert,should,expect){
describe.only('test cookie functionality',function() {
before(h.setup);

describe('test cookie functionality',function() {
it('should set a cookie and read its content afterwards', function(done){
client
.setCookie({name: 'test',value: 'cookie saved!'})
.getCookie('test', function(err,result) {
assert.equal(null, err)
assert.strictEqual(result.name,'test');
assert.strictEqual(result.value,'cookie saved!');
})
.call(done);
});
it('should delete created cookie and is not able to read its content', function(done){
client
.deleteCookie('test')
.getCookie('test', function(err,result) {
assert.equal(null, err)
assert.strictEqual(result,null);
})
.call(done);
});
it('should set a cookie and read its content afterwards', function(done){
this.client
.setCookie({name: 'test',value: 'cookie saved!'})
.getCookie('test', function(err,result) {
assert.equal(null, err);
assert.strictEqual(result.name,'test');
assert.strictEqual(result.value,'cookie saved!');
})
.call(done);
});

it('should create two cookies and delete all at once', function(done){
client
.setCookie({name: 'test',value: 'cookie saved!'})
.setCookie({name: 'test2',value: 'cookie2 saved!'})
.deleteCookie()
.getCookie('test', function(err,result) {
assert.equal(null, err)
assert.strictEqual(result,null);
})
.getCookie('test2', function(err,result) {
assert.equal(null, err)
assert.strictEqual(result,null);
})
.call(done);
});
it('should delete created cookie and is not able to read its content', function(done){
this.client
.deleteCookie('test')
.getCookie('test', function(err,result) {
assert.equal(null, err);
assert.strictEqual(result,null);
})
.call(done);
});

it('should set two cookies and read all at once', function(done){
this.client
.setCookie({name: 'test',value: 'cookie saved!'})
.setCookie({name: 'test2',value: 'cookie2 saved!'})
.getCookie(function(err,result) {
assert.equal(null, err);
assert.strictEqual(2,result.length);
assert.strictEqual('cookie saved!',result[0].value);
assert.strictEqual('cookie2 saved!',result[1].value);
})
.call(done);
});

};*/
it('should delete all previous created cookies at once', function(done){
this.client
.deleteCookie()
.getCookie(function(err,result) {
assert.equal(null, err);
assert.strictEqual(0,result.length);
})
.call(done);
});
});

0 comments on commit 33c51b2

Please sign in to comment.