Created
April 25, 2016 18:36
-
-
Save jeremyruppel/fd25c723a5962a49936f174d765aa11a to your computer and use it in GitHub Desktop.
Testing an HTTP client: Part I
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 assert = require('assert'); | |
describe('Client', function () { | |
it('calls flickr.photos.search', function (done) { | |
var subject = new Client(process.env.API_KEY); | |
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