Skip to content

Commit

Permalink
Merge pull request #28 from ffoysal/master
Browse files Browse the repository at this point in the history
add support for patch request
  • Loading branch information
jeffbski authored Oct 14, 2016
2 parents ac779a9 + 4002de3 commit 1bd73a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Each array of opertions will be performed in series one after another unless an

Each operation can have the following properties:

- one of these common REST properties `get`, `head`, `put`, `post`, `del` (using del rather than delete since delete is a JS reserved word) with a value pointing to the URI, ex: `{ get: 'http://localhost:8000/foo' }`
- one of these common REST properties `get`, `head`, `put`, `post`, `patch`, `del` (using del rather than delete since delete is a JS reserved word) with a value pointing to the URI, ex: `{ get: 'http://localhost:8000/foo' }`
- alternatively can specify `method` (use uppercase) and `uri` directly, ex: `{ method: 'GET', uri: 'http://localhost:8000/foo' }`
- `json` optionally provide data which will be JSON stringified and provided as body also setting content type to application/json, ex: `{ put: 'http://localhost:8000/foo', json: { foo: 10 } }`
- `headers` - optional headers to set, ex: `{ get: 'http://localhost:8000/foo', headers: { 'Accept-Encoding': 'gzip'}`
Expand Down
7 changes: 5 additions & 2 deletions lib/bench-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var https = require('https');
var measured = require('measured');
var request = require('request');
var EventEmitter = require('events').EventEmitter;
var SUBSTITUTED_KEYS = ['get', 'head', 'put', 'post', 'del', 'json', 'body'];
var SUBSTITUTED_KEYS = ['get', 'head', 'put', 'post', 'del', 'json', 'body', 'patch'];

function benchmark(flow, runOptions) {
if (!runOptions || !runOptions.iterations || !runOptions.limit) {
Expand Down Expand Up @@ -197,6 +197,9 @@ function getRequestOptions(env, action) {
} else if (action.del) {
action.method = 'DELETE';
action.uri = action.del;
} else if (action.patch) {
action.method = 'PATCH';
action.uri = action.patch;
}
return action;
}
Expand Down Expand Up @@ -333,7 +336,7 @@ function substituteFnWhereNeeded(actions) {
actions = actions || [];
return actions.map(function (action) {
return Object.keys(action).reduce(function (accum, key) {
if ((key === 'get' || key === 'head' || key === 'put' || key === 'post' || key === 'del') &&
if ((key === 'get' || key === 'head' || key === 'put' || key === 'post' || key === 'del' || key === 'patch') &&
needsSubtitution(action[key])) {
accum[key] = function (tokens) {
return action[key].replace(/\#\{INDEX\}/g, tokens.INDEX);
Expand Down
25 changes: 25 additions & 0 deletions test/bench-rest.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ test('put/get flow with token substitution', function (done) {
});
});

test('simple patch/get flow', function (done) {
var flow = {
main: [
{ patch: 'http://localhost:8008/foo', json: 'mydata' },
{ get: 'http://localhost:8008/foo' }
]
};
var runOptions = {
limit: 1, // limiting to single at a time so can guarantee order for test verification
iterations: 2
};
var errors = [];
requests.length = 0;
benchrest(flow, runOptions)
.on('error', function (err, ctxName) { errors.push(err); })
.on('end', function (stats, errorCount) {
if (errorCount) return done(errors[0] || 'unknown error');
t.equal(requests.length, runOptions.iterations * flow.main.length);
t.deepEqual(requests[0], { method: 'PATCH', url: '/foo', data: '"mydata"' });
t.deepEqual(requests[1], { method: 'GET', url: '/foo', data: '' });
t.deepEqual(requests[2], { method: 'PATCH', url: '/foo', data: '"mydata"' });
t.deepEqual(requests[3], { method: 'GET', url: '/foo', data: '' });
done();
});
});

test('allow flow to be defined as single string URL implying GET', function (done) {
var flow = 'http://localhost:8008';
Expand Down

0 comments on commit 1bd73a9

Please sign in to comment.