Created
April 25, 2016 19:03
-
-
Save jeremyruppel/d92a62400f635b42249adc041cdecc96 to your computer and use it in GitHub Desktop.
Testing an HTTP client: Part III
This file contains hidden or 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
var request = require('superagent'); | |
/** | |
* @constructor | |
*/ | |
module.exports = Client; | |
function Client(api_key) { | |
this.params = {}; | |
this.params.api_key = api_key; | |
this.params.format = 'json'; | |
this.params.nojsoncallback = 1; | |
} | |
Client.prototype = Object.create(null); | |
Client.prototype.search = function (text, done) { | |
request('GET', 'https://api.flickr.com/services/rest') | |
.query('method=flickr.photos.search') | |
.query('text=' + text) | |
.query(this.params) | |
.end(done); | |
}; |
This file contains hidden or 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
var Client = require('./client'); | |
var assert = require('assert'); | |
var nock = require('nock'); | |
describe('Client', function () { | |
it('calls flickr.photos.search', function (done) { | |
var subject = new Client(process.env.API_KEY); | |
var flickr = nock('https://api.flickr.com') | |
.get('/services/rest') | |
.query({ | |
method: 'flickr.photos.search', | |
format: 'json', | |
nojsoncallback: 1, | |
api_key: process.env.API_KEY, | |
text: 'coffee' | |
}) | |
.reply(200, { stat: 'ok' }); | |
subject.search('coffee', function (err, res) { | |
assert.ifError(err); | |
assert.equal(res.statusCode, 200); | |
assert.equal(res.body.stat, 'ok'); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment