Instantly share code, notes, and snippets.
CreatedApril 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
| varrequest=require('superagent'); | |
| /** | |
| *@constructor | |
| */ | |
| module.exports=Client; | |
| functionClient(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
| varClient=require('./client'); | |
| varassert=require('assert'); | |
| varnock=require('nock'); | |
| describe('Client',function(){ | |
| it('calls flickr.photos.search',function(done){ | |
| varsubject=newClient(process.env.API_KEY); | |
| varflickr=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 freeto join this conversation on GitHub. Already have an account?Sign in to comment