Created
April 25, 2016 18:43
-
-
Save jeremyruppel/8b837f439663db325aaa2437a2259934 to your computer and use it in GitHub Desktop.
Testing an HTTP client: Part II
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
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 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 request = require('superagent'); | |
var assert = require('assert'); | |
var sinon = require('sinon'); | |
describe('Client', function () { | |
it('calls flickr.photos.search', function (done) { | |
var subject = new Client(process.env.API_KEY); | |
var stub = sinon.stub(require('superagent').Request.prototype); | |
stub.query.returnsThis(); | |
stub.end.yieldsAsync(null, { statusCode: 200, body: { 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
Thanks