Permalink
Browse files

mods to nock, essentially creating my own lightweight fork.

  • Loading branch information...
1 parent 32b5ed9 commit cc02c805a565c91969eff71e384af162b393a32b @pyrat committed Apr 11, 2012
Showing with 4 additions and 115 deletions.
  1. +1 −9 index.js
  2. +1 −5 lib/intercept.js
  3. +0 −98 lib/recorder.js
  4. +2 −3 lib/scope.js
View
@@ -1,9 +1 @@
-var recorder = require('./lib/recorder')
-module.exports = require('./lib/scope');
-
-module.exports.recorder = {
- rec : recorder.record
- , clear : recorder.clear
- , play : recorder.outputs
-};
-module.exports.restore = recorder.restore;
+module.exports = require('./lib/scope');
View
@@ -35,11 +35,7 @@ function remove(interceptor) {
function stringifyRequest(options) {
var method = options.method || 'GET';
var path = options.path;
- var body = options.body;
- if (body && typeof(body) !== 'string') {
- body = body.toString();
- }
- return method + ' ' + path + ' ' + body;
+ return method + ' ' + path;
}
function getHeader(request, name) {
View
@@ -1,98 +0,0 @@
-var http = require('http');
-var oldRequest = http.request;
-var inspect = require('util').inspect;
-
-var SEPARATOR = '\n<<<<<<-- cut here -->>>>>>\n';
-
-var outputs = [];
-
-function generateRequestAndResponse(body, options, res, datas) {
- var requestBody = body.map(function(buffer) {
- return buffer.toString('utf8');
- }).join('');
-
- var responseBody = datas.map(function(buffer) {
- return buffer.toString('utf8');
- }).join('');
-
- ret = [];
- ret.push('\nnock(\'');
- ret.push(options.host);
- ret.push('\')\n');
- ret.push(' .');
- ret.push((options.method || 'GET').toLowerCase());
- ret.push('(\'');
- ret.push(options.path);
- ret.push("'");
- if (requestBody) {
- ret.push(', ');
- ret.push(JSON.stringify(requestBody));
- }
- ret.push(")\n");
-
- ret.push(' .reply(');
- ret.push(res.statusCode.toString());
- ret.push(', ');
- ret.push(JSON.stringify(responseBody));
- if (res.headers) {
- ret.push(', ');
- ret.push(inspect(res.headers));
- }
- ret.push(');\n');
-
- return ret.join('');
-}
-
-function record(dont_print) {
- http.request = function(options, callback) {
- var body = []
- , req, oldWrite, oldEnd;
-
- req = oldRequest.call(http, options, function(res) {
- var datas = [];
-
- res.on('data', function(data) {
- datas.push(data);
- });
-
- res.once('end', function() {
- var out = generateRequestAndResponse(body, options, res, datas);
- outputs.push(out);
- if (! dont_print) { console.log(SEPARATOR + out + SEPARATOR); }
- });
-
- callback.apply(res, arguments);
-
- });
- oldWrite = req.write;
- req.write = function(data) {
- if ('undefined' !== typeof(data)) {
- if (data) {body.push(data); }
- oldWrite.call(req, data);
- }
- };
- oldEnd = req.end;
- req.end = function(data) {
- if ('undefined' !== typeof(data)) {
- req.write(data);
- }
- oldEnd.apply(req, arguments);
- };
- return req;
- };
-}
-
-function restore() {
- http.request = oldRequest;
-}
-
-function clear() {
- outputs = [];
-}
-
-exports.record = record;
-exports.outputs = function() {
- return outputs;
-};
-exports.restore = restore;
-exports.clear = clear;
View
@@ -97,9 +97,8 @@ function startScope(basePath) {
}
var matchKey = method + ' ' + proto + '://' + options.host + path;
- // if (body) { matchKey += (' ' + body); }
- matches = matchKey === this._key;
- logger('matching ' + matchKey + ' to ' + this._key + ': ' + matches);
+ matches = matchKey === this._key;
+ console.log('matching ' + matchKey + ' to ' + this._key + ': ' + matches);
return matches;
}

0 comments on commit cc02c80

Please sign in to comment.