-
-
Save jhartikainen/afbcfeead71a2984c167 to your computer and use it in GitHub Desktop.
node http api testing specs
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 assert = require('assert'); | |
var sinon = require('sinon'); | |
var PassThrough = require('stream').PassThrough; | |
var http = require('http'); | |
var api = require('../api.js'); | |
describe('api', function() { | |
beforeEach(function() { | |
this.request = sinon.stub(http, 'request'); | |
}); | |
afterEach(function() { | |
http.request.restore(); | |
}); | |
it('should convert get result to object', function(done) { | |
var expected = { hello: 'world' }; | |
var response = new PassThrough(); | |
response.write(JSON.stringify(expected)); | |
response.end(); | |
this.request.callsArgWith(1, response) | |
.returns(new PassThrough()); | |
api.get(function(err, result) { | |
assert.deepEqual(result, expected); | |
done(); | |
}); | |
}); | |
it('should send post params in request body', function() { | |
var params = { foo: 'bar' }; | |
var expected = JSON.stringify(params); | |
var request = new PassThrough(); | |
var write = sinon.spy(request, 'write'); | |
this.request.returns(request); | |
api.post(params, function() { }); | |
assert(write.withArgs(expected).calledOnce); | |
}); | |
it('should pass request error to callback', function(done) { | |
var expected = 'some error'; | |
var request = new PassThrough(); | |
this.request.returns(request); | |
api.get(function(err, result) { | |
assert.equal(err, expected); | |
done(); | |
}); | |
request.emit('error', expected); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment