Last active
October 24, 2015 06:26
-
-
Save ksheedlo/de392f55f788ee430759 to your computer and use it in GitHub Desktop.
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
'use strict'; | |
global.Promise = global.Promise || require('bluebird'); | |
var request = require('request'), | |
fetch = require('node-fetch'); | |
exports.getRequest = function getRequest() { | |
return new Promise(function (resolve, reject) { | |
request({ | |
url: 'https://api.whatever.co/items', | |
headers: { | |
'X-Foo': 'lolwhatever' | |
}, | |
json: true | |
}, function (err, resp, body) { | |
if (err) { | |
return reject(err); | |
} | |
return resolve(body); | |
}); | |
}); | |
}; | |
exports.getFetch = function getFetch() { | |
return fetch('https://api.whatever.co/items', { | |
headers: { | |
'X-Foo': 'lolwhatever' | |
} | |
}) | |
.then(function (response) { | |
return response.json(); | |
}); | |
}; |
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
'use strict'; | |
var nock = require('nock'), | |
getters = require('../'); | |
describe('fetching', function () { | |
it('getRequest gets the stuff', function (done) { | |
var scope = nock('https://api.whatever.co', { | |
reqheaders: { | |
'X-Foo': 'lolwhatever' | |
} | |
}) | |
.get('/items') | |
.reply(200, [{ sku: '123' }, { sku: '456' }]) | |
getters.getRequest().then(function (resp) { | |
scope.done(); | |
done(); | |
}); | |
}); | |
it('getFetch works without headers', function (done) { | |
var scope = nock('https://api.whatever.co') | |
.get('/items') | |
.reply(200, [{ sku: '123' }, { sku: '456' }]) | |
getters.getFetch().then(function (resp) { | |
scope.done(); | |
done(); | |
}) | |
.catch(function (err) { | |
console.error(err.stack); | |
throw err; | |
}); | |
}); | |
it('getFetch gets the stuff', function (done) { | |
var scope = nock('https://api.whatever.co', { | |
reqheaders: { | |
'X-Foo': 'lolwhatever' | |
} | |
}) | |
.get('/items') | |
.reply(200, [{ sku: '123' }, { sku: '456' }]) | |
getters.getFetch().then(function (resp) { | |
scope.done(); | |
done(); | |
}) | |
.catch(function (err) { | |
console.error(err.stack); | |
throw err; | |
}); | |
}); | |
}); |
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
fetching | |
✓ getRequest gets the stuff | |
✓ getFetch works without headers | |
Error: request to https://api.whatever.co/items failed, reason: Nock: No match for request GET https://api.whatever.co/items | |
at OverriddenClientRequest.<anonymous> (/Users/ken/ksheedlo/basic-nock/node_modules/node-fetch/index.js:117:11) | |
at OverriddenClientRequest.emit (events.js:107:17) | |
at /Users/ken/ksheedlo/basic-nock/node_modules/nock/lib/request_overrider.js:202:11 | |
at process._tickCallback (node.js:355:11) | |
1) getFetch gets the stuff | |
2 passing (2s) | |
1 failing | |
1) fetching getFetch gets the stuff: | |
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment