Skip to content

Commit

Permalink
Added patch convenience method to socket client
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Feb 14, 2017
1 parent 15b215a commit e458031
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
25 changes: 25 additions & 0 deletions sails.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,31 @@
};


/**
* Simulate a PATCH request to sails
* e.g.
* `socket.patch('/event/3', changedFields, $spinner.hide)`
*
* @api public
* @param {String} url :: destination URL
* @param {Object} data :: parameters to send with the request [optional]
* @param {Function} cb :: callback function to call when finished [optional]
*/

SailsSocket.prototype.patch = function(url, data, cb) {

// `data` is optional
if (typeof data === 'function') {
cb = data;
data = {};
}

return this.request({
method: 'patch',
params: data,
url: url
}, cb);
};

/**
* Simulate a DELETE request to sails
Expand Down
10 changes: 9 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var EXPECTED_RESPONSES = {
},
'delete /hello': { body: 'deleted!'},
'post /hello': { body: 'posted!'},
'put /hello': { body: 'putted!'}
'put /hello': { body: 'putted!'},
'patch /hello': { body: 'patched!'}

};
var setupRoutes = _setupRoutes(EXPECTED_RESPONSES);
Expand Down Expand Up @@ -101,6 +102,13 @@ describe('io.socket', function () {
});
});

it('should be able to send a PATCH request and receive the expected response', function (cb) {
io.socket.patch('/hello', function (body, jwr) {
assertResponse('patch /hello', arguments);
return cb();
});
});

it('should receive JSON as a POJO, not a string', function (cb) {
io.socket.get('/someJSON', function (body, jwr) {
assertResponse('get /someJSON', arguments);
Expand Down

0 comments on commit e458031

Please sign in to comment.