Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing an HTTP client: Part IV
var path = require("path");
/**
* GET /services/rest?api_key=9147f8d3b61c82089641b1268482c2a1&format=json&nojsoncallback=1&method=flickr.photos.search&text=coffee
*
* host: api.flickr.com
* accept-encoding: gzip, deflate
* user-agent: node-superagent/1.8.3
* connection: close
*/
module.exports = function (req, res) {
res.statusCode = 200;
res.setHeader("date", "Mon, 25 Apr 2016 19:08:37 GMT");
res.setHeader("content-type", "application/json");
res.setHeader("content-length", "5853");
res.setHeader("cache-control", "private");
res.setHeader("x-served-by", "www257.flickr.gq1.yahoo.com");
res.setHeader("vary", "Accept-Encoding");
res.setHeader("content-encoding", "gzip");
res.setHeader("age", "0");
res.setHeader("server", "ATS");
res.setHeader("connection", "close");
res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));
res.write(new Buffer("H4sIAAAAAAAAA+1cW3PbRpb+K23xIbMpmen7xQ8zseRrNpZdljPZ1GrH1Wg0REQgwMHFWlYy/31PA5QEgFBCL6LJi10JBQJg4+D0uXzn0v3L0WZV1EV19OSXo4299EdPyHF7AGcYwwK++HJ3AePjo7qobXb05AiucSaPjrufHz3571+O0hjOUym4EYIyTeFicZ37Es4KpTEllHx7hjGcrrwrfQ3nLUmcjrGJdXu2/NTeTRQOP05suT56QuGZaZ3B84+eZrEvrc3RaZEk3qPH6J0tr9BzW9Vwe1ptmihLXfsGaZWUqc+BItx+ses027ZfKpuEd/nX8S3BmCnMMea8RzDnAlMtJRDM+gRrTTkmkVZ4QLBUfIrg09Ku12l+iZKiREma26z6G3pW5F/V6Looy+0xuvZfffLosqjRtmhKFFl3tUQfyi0K37bNer1FblW4IrO1R66Ax/kY+WpT+qoqUORtXj1CP3q0sjBMvfJrlObIok8W3r7ewniXx/A1Dmy6G6c7m8dw5XqVhoH7l5bAULcKRzBqFQZFzgK/09wj/88m/WQzn9eoLhBDrtlUqEiAsnZCgKRN5m3l0brJ3epzJwX3JiVIEcZU9qWISJgroylM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tjHDPopISKAOlWy6gtrvZQKPBe4OHFNbOoqLvKmrJXpjtyACQWraNditAIJ1XIaWyXYDryZ0nq19nBYu5BIvizi26zytSzB/W/StW4VU8sc8vVzVH2MXGtfj+fXYwBGOh/0l92EUcGLUMu/UsNrTVUf//xjlrFjnwJAWoOzsdjDbaJNZ18LuCdDyBaPMnHMW9qbqawGWGNR2r49As8RKzV082nQDT2pBbwtOZK99BbYQnTQlBIApOgHjmaIiv9u3s9WDb8sm2paFBf9ZVp3/But7Ba4/K25W0Ld2s7bZtS1rdNJ2fp6C3z/NbBVS/904aV6H1PslwIAqtZ2cfPb+ACOYRCCmUIOQklMK1tfsReyeUOoFVma0e5ecdskW3jWgml1syR9T/nheahTMrjIAMvEh1AoDlCrsxHh98/T+rX8MtXu8hX8H8Rach9AGYuYhtXx6y5KH4m0Igw+i1sRxFCkVj3hrHpK3ZMxbkNyDeOsEsFVrn4xz+NOpkIeiFouDqJXYgcxi40cNhNN5sYeSBDLaReA+aqOw01TksBvGd/dUtB+It4DmzEG8jRyVWko32hjons1ZHoDasAG1knSwBeV9O2bL2DtBpefjuPQ3OklnJpy05iMLcN/eDGBGhVHUqIM4OWtvhv85Plrb//0IIVFx7eOP4FebrK6OngCleHhpt5k5h7c6Al8a6Cyujv71f12JhJb5XAAA", "base64"));
res.end();
return __filename;
};
var request = require('superagent');
/**
* @constructor
*/
module.exports = Client;
function Client(api_key, host) {
this.params = {};
this.params.api_key = api_key;
this.params.format = 'json';
this.params.nojsoncallback = 1;
this.host = host || 'https://api.flickr.com';
}
Client.prototype = Object.create(null);
Client.prototype.search = function (text, done) {
request('GET', this.host + '/services/rest')
.query('method=flickr.photos.search')
.query('text=' + text)
.query(this.params)
.end(done);
};
var Client = require('./client');
var assert = require('assert');
var yakbak = require('yakbak');
var http = require('http');
var proxy = http.createServer(yakbak('https://api.flickr.com', {
dirname: __dirname
}));
describe('Client', function () {
before(function (done) {
proxy.listen(4567, done);
});
it('calls flickr.photos.search', function (done) {
var subject = new Client(process.env.API_KEY, 'http://localhost:4567');
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
You can’t perform that action at this time.