diff options
Diffstat (limited to 'follow-redirects/test/util.js')
-rw-r--r-- | follow-redirects/test/util.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/follow-redirects/test/util.js b/follow-redirects/test/util.js new file mode 100644 index 0000000..4ff6974 --- /dev/null +++ b/follow-redirects/test/util.js @@ -0,0 +1,51 @@ +var concat = require("concat-stream"); + +function redirectsTo() { + var args = Array.prototype.slice.call(arguments); + return function (req, res) { + res.redirect.apply(res, args); + }; +} + +function sendsJson(json) { + return function (req, res) { + res.json(json); + }; +} + +function concatJson(resolve, reject) { + return function (res) { + res.pipe(concat({ encoding: "string" }, function (string) { + try { + res.parsedJson = JSON.parse(string); + resolve(res); + } + catch (err) { + reject(new Error("error parsing " + JSON.stringify(string) + "\n caused by: " + err.message)); + } + })).on("error", reject); + }; +} + +function delay(clock, msecs, handler) { + return function (req, res) { + clock.tick(msecs); + handler(req, res); + }; +} + +function asPromise(cb) { + return function (result) { + return new Promise(function (resolve, reject) { + cb(resolve, reject, result); + }); + }; +} + +module.exports = { + asPromise: asPromise, + concatJson: concatJson, + delay: delay, + redirectsTo: redirectsTo, + sendsJson: sendsJson, +}; |