Permalink
Browse files

Fix #124: .persist() broken when used with .replyWithFile()

  • Loading branch information...
1 parent 4fe1226 commit ff1755ebcced6c38b169f1d275d60ec1d7a3796f @tonyskn tonyskn committed Nov 8, 2013
Showing with 40 additions and 1 deletion.
  1. +7 −1 lib/scope.js
  2. +33 −0 tests/test_intercept.js
View
@@ -92,6 +92,7 @@ function startScope(basePath, options) {
function replyWithFile(statusCode, filePath, headers) {
var readStream = fs.createReadStream(filePath);
readStream.pause();
+ this.filePath = filePath;
return reply.call(this, statusCode, readStream, headers);
}
@@ -170,6 +171,11 @@ function startScope(basePath, options) {
}
function discard() {
+ if (persist && this.filePath) {
+ this.body = fs.createReadStream(this.filePath);
+ this.body.pause();
+ }
+
if (! persist) remove(this._key, this);
}
@@ -403,4 +409,4 @@ module.exports = startScope;
module.exports.cleanAll = cleanAll;
module.exports.activate = globalIntercept.activate;
module.exports.disableNetConnect = globalIntercept.disableNetConnect
-module.exports.enableNetConnect = globalIntercept.enableNetConnect
+module.exports.enableNetConnect = globalIntercept.enableNetConnect
View
@@ -1687,6 +1687,39 @@ test('persists interceptors', function(t) {
}).end();
});
+test("persist reply with file", function(t) {
+ var dataCalled = false
+
+ var scope = nock('http://www.filereplier.com')
+ .persist()
+ .get('/')
+ .replyWithFile(200, __dirname + '/../assets/reply_file_1.txt')
+ .get('/test')
+ .reply(200, 'Yay!');
+
+ for (var i=0; i < 2; i++) {
+ var req = http.request({
+ host: "www.filereplier.com"
+ , path: '/'
+ , port: 80
+ }, function(res) {
+
+ t.equal(res.statusCode, 200);
+ res.on('end', function() {
+ t.ok(dataCalled);
+ t.end();
+ });
+ res.on('data', function(data) {
+ dataCalled = true;
+ t.equal(data.toString(), "Hello from the file!", "response should match");
+ });
+
+ });
+ req.end();
+ }
+
+});
+
test('(re-)activate after restore', function(t) {
var scope = nock('http://google.com')
.get('/')

0 comments on commit ff1755e

Please sign in to comment.