Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
module.exports = require('./lib/scope'); | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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); | ||
if (requestBody) { | ||
ret.push(', '); | ||
ret.push(JSON.stringify(requestBody)); | ||
} | ||
ret.push(')\n'); | ||
|
||
ret.push(' .(') | ||
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) { | ||
var body = []; | ||
http.request = function(options, callback) { | ||
var 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var nock = require('../.') | ||
, tap = require('tap') | ||
, http = require('http'); | ||
|
||
tap.test('records', function(t) { | ||
nock.restore(); | ||
var cb1 = false | ||
, options = { method: 'POST' | ||
, host:'expensecat.iriscouch.com' | ||
, port:80 | ||
, path:'/' } | ||
; | ||
|
||
nock.recorder.rec(true); | ||
var req = http.request(options, function(res) { | ||
cb1 = true | ||
var ret; | ||
res.once('end', function() { | ||
nock.restore(); | ||
ret = nock.recorder.play(); | ||
t.equal(ret.length, 1); | ||
t.equal(ret[0].indexOf("\nnock('expensecat.iriscouch.com')\n .post('/, \"ABCDEF\")\n .("), 0); | ||
t.end(); | ||
}); | ||
}); | ||
req.write('ABCDEF'); | ||
req.end(); | ||
return req; | ||
}); |