I'm writing code interacting with PayPal classic API. The first part of this interaction is to send request to PayPal and get a token from them. For that I use simple https request:
function makePayPalRequestForToken(options, callback) {
var requestOptions = {
host: config.paypal.endpoint,
path: '/nvp?' + qs.stringify(options),
method: 'GET'
};
var req = https.get(requestOptions, function(res) {
var data = '';
res.on('data', function(chunk) {
data = data + chunk;
});
res.on('end', function() {
callback(null, data);
});
});
req.on('error', function(e) {
callback(e);
});
}
It works perfectly ok with PayPal sandbox, however, now I want to unit test my code and I don't know how to mock the response I get from PayPal.
I checked that the row response from PayPal is following:
<Buffer 54 4f 4b 45 4e 3d 45 43 25 32 64 35 44 53 33 38 35 31 37 4e 4e 36 36 37 34 37 33 4e 26 54 49 4d 45 53 54 41 4d 50 3d 32 30 31 35 25 32 64 30 35 25 32 64 ...>
So it looks like binary data. I wanted to use nock to mock the response, but I wonder how I could do this? How to make nock to response with the binary version of my response?
I tried something like this:
nock('https://' + config.paypal.endpoint)
.filteringPath(function() {
return '/';
})
.get('/')
.reply(200, 'myresponse', {'content-type': 'binary'});
But then I'm getting:
Uncaught Error: stream.push() after EOF
and it looks like no data is send in the mocked response.