@pgte I've taken your example for nockback, and attempted to filter the request body to return something else. Below is my code and console output, which shows that the filtering isn't working as I expected based on the docs (you'll note that the console logs in the before function are never output).
The issue is probably with my implementation, so I apologize in advance if I'm simply misunderstanding the docs. However, any help would be greatly appreciated.
The end goal for me is to be able to hijack requests that have timestamps in either the body (with filteringRequestBody) and url (with filteringPath), all while using the nock.back functionality.
var http = require('http');
var nockBack = require('nock').back;
var request = require('request');
nockBack.setMode('lockdown');
nockBack.fixtures = __dirname + '/nockFixtures'; //this only needs to be set once in your test helper
var before = function(scope) {
console.log('in the before function');
scope.filteringRequestBody = function(body, aRecordedBody) {
console.log('filtering request body');
return '<html><head><title>INTERCEPTION!</title></head><body><h1>Your call was intercepted!</body></html>';
};
}
console.log('starting nock back test');
// recording of the fixture
nockBack('zomboFixture.json', function(nockDone) {
request.get('http://zombo.com', function(err, res, body) {
nockDone();
console.log('response: ', body);
// usage of the created fixture
nockBack('zomboFixture.json', function (nockDone) {
http.get('http://zombo.com/').end(); // respond body "Ok"
this.assertScopesFinished(); //throws an exception if all nocks in fixture were not satisfied
http.get('http://zombo.com/').end(); // throws exception because someFixture.json only had one call
nockDone(); //never gets here
});
});
});
My console output is:
>> node test.js
starting nock back test
response: <html>
<head>
<title>ZOMBO</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!--Please Visit 15footstick.com our other website. ThankZ -->
</head>
<body bgcolor="#FFFFFF">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=4,0,0,0"
id=inrozxa width=100% height=100%>
<param name=movie value="welcomenew6.swf">
<param name=quality value=high>
<param name=bgcolor value=#FFFFFF>
<embed src="inrozxa.swf" quality=high bgcolor=#FFFFFF width=100% height=100% type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">
</embed>
</object>
</body>
</html>
events.js:85
throw er; // Unhandled 'error' event
^
Error: Nock: No match for request GET http://zombo.com/
at RequestOverrider.end (/workspace/nocktesting/node_modules/nock/lib/request_overrider.js:250:17)
at OverriddenClientRequest.RequestOverrider.req.end (/workspace/nocktesting/node_modules/nock/lib/request_overrider.js:161:7)
at Object.exports.get (http.js:54:7)
at Object.<anonymous> (/workspace/nocktesting/test.js:32:12)
at Back (/workspace/nocktesting/node_modules/nock/lib/back.js:59:12)
at Request._callback (/workspace/nocktesting/test.js:28:5)
at Request.self.callback (/workspace/nocktesting/node_modules/request/request.js:198:22)
at Request.emit (events.js:110:17)
at Request.<anonymous> (/workspace/nocktesting/node_modules/request/request.js:1082:10)
at Request.emit (events.js:129:20)
@kelaban any input?
Hi all @sidabs it seems like the example might be wrong. Looking at lib/back.js#L47
it seems like you need to pass an object with {before: before} to the nock call
try something like
var beforeFunc = function() {
console.log("in before")
}
nockBack('zomboFixture.json', {before: beforeFunc}, function(nockDone) {
...
})
Looking at this now i realize it's a pretty old issue :sweat_smile: but hope it helps.
hey @bfeigin, thanks! Could you give an example of how we could leverage this to filter a path that has a dynamic timestamp?
For example, I was able to attach a filteringRequestBody to the scope, but filteringPath didn't work.
var beforeFunc = function(scope) {
console.log('intercepting path...');
scope.filteringPath = function() {
console.log('filtering the path...');
return '/hello-world';
};
scope.filteringRequestBody = function() {
console.log('filtering request body...');
return {
hello: 'pluto'
};
};
};
nockBack('my-fixture.json', { before: beforeFunc}, function(nockDone) {
// do something
});
The above results in the output that shows that filteringPath isn't properly being called.
intercepting path...
filtering request body...
Pretty old issue, but for anyone finding this, filteringPath goes in the scope options. So the above function would be:
scope.options.filteringPath = function() {
console.log('filtering the path...');
return '/hello-world';
};
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it鈥檚 related. Thank you!
Most helpful comment
Hi all @sidabs it seems like the example might be wrong. Looking at lib/back.js#L47
it seems like you need to pass an object with {before: before} to the nock call
try something like
Looking at this now i realize it's a pretty old issue :sweat_smile: but hope it helps.