From 6c32bf4f0591f33e255870fec6af89067b92f841 Mon Sep 17 00:00:00 2001 From: simov Date: Thu, 14 Apr 2016 10:51:24 +0300 Subject: [PATCH] Add callback option --- README.md | 1 + index.js | 2 +- tests/test-api.js | 30 ++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/test-api.js diff --git a/README.md b/README.md index 64c43e742..42fbec365 100644 --- a/README.md +++ b/README.md @@ -814,6 +814,7 @@ default in Linux can be anywhere from 20-120 seconds][linux-timeout]). - `time` - If `true`, the request-response cycle (including all redirects) is timed at millisecond resolution, and the result provided on the response's `elapsedTime` property. - `har` - A [HAR 1.2 Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), will be processed from HAR format into options overwriting matching values *(see the [HAR 1.2 section](#support-for-har-1.2) for details)* +- `callback` - alternatively pass the request's callback in the options object The callback argument gets 3 arguments: diff --git a/index.js b/index.js index 4d0c748da..b2b321785 100755 --- a/index.js +++ b/index.js @@ -37,7 +37,7 @@ function initParams(uri, options, callback) { extend(params, uri) } - params.callback = callback + params.callback = callback || params.callback return params } diff --git a/tests/test-api.js b/tests/test-api.js new file mode 100644 index 000000000..a01ee60c2 --- /dev/null +++ b/tests/test-api.js @@ -0,0 +1,30 @@ +'use strict' + +var http = require('http') + , request = require('../index') + , tape = require('tape') + , server + + +tape('setup', function (t) { + server = http.createServer() + server.on('request', function (req, res) { + res.writeHead(202) + req.pipe(res) + }) + server.listen(6767, t.end) +}) + +tape('callback option', function (t) { + request({ + url: 'http://localhost:6767', + callback: function (err, res, body) { + t.equal(res.statusCode, 202) + t.end() + } + }) +}) + +tape('cleanup', function(t) { + server.close(t.end) +})