Commit b38dd073 authored by Aaron Tidwell's avatar Aaron Tidwell

add request callback tests

parent 50b87b68
const qs = require('querystring');
const Client = require('./client').Client;
const errorHandler = require('./error-handler');
const httpsMock = require('https'); //replaced with mock by mockery
function parseOpts() {
......@@ -7,6 +10,9 @@ function parseOpts() {
}
describe('Client Class', () => {
beforeEach(() => {
httpsMock.reset();
});
describe('constructor', () => {
it('should set options to the object that is passed', () => {
const opts = {};
......@@ -195,5 +201,87 @@ describe('Client Class', () => {
}
});
});
it('should call the error handler on anything but a 200 response', () => {
spyOn(errorHandler, 'handle');
client.makeRequest({});
httpsMock.res.statusCode = 500;
httpsMock.listeners.end[0]();
expect(errorHandler.handle).toHaveBeenCalled();
});
it('should call the callback on a 200 response', () => {
const spy = jasmine.createSpy();
client.makeRequest({
callback: spy
});
httpsMock.res.statusCode = 200;
httpsMock.listeners.data[0]('{}');
httpsMock.listeners.end[0]();
expect(spy).toHaveBeenCalled();
});
it('should parse the response as json', () => {
const spy = jasmine.createSpy();
client.makeRequest({
callback: spy
});
httpsMock.res.statusCode = 200;
httpsMock.listeners.data[0]('{}');
httpsMock.listeners.end[0]();
expect(spy).toHaveBeenCalledWith(null, {});
});
it('should convert _ to camelCase if set', () => {
const spy = jasmine.createSpy();
client.makeRequest({
callback: spy
});
httpsMock.res.statusCode = 200;
httpsMock.listeners.data[0]('{"some_prop": 123}');
httpsMock.listeners.end[0]();
expect(spy).toHaveBeenCalledWith(null, {
someProp: 123
});
});
it('should not convert _ to camelCase if unset', () => {
const spy = jasmine.createSpy();
client.options.massageProperties = false;
client.makeRequest({
callback: spy
});
httpsMock.res.statusCode = 200;
httpsMock.listeners.data[0]('{"some_prop": 123}');
httpsMock.listeners.end[0]();
expect(spy).toHaveBeenCalledWith(null, {
some_prop: 123
});
});
it('should not parse the response as json if set to another format', () => {
const spy = jasmine.createSpy();
client.options.format = 'xml';
client.makeRequest({
callback: spy
});
httpsMock.res.statusCode = 200;
httpsMock.listeners.data[0]('<bla>');
httpsMock.listeners.end[0]();
expect(spy).toHaveBeenCalledWith(null, '<bla>');
})
});
});
......@@ -5,17 +5,23 @@ mockery.enable({
});
const httpsMock = {
reset: function() {
this.listeners = {};
this.opts = {};
this.res.statusCode = '';
},
opts: {},
listeners: {
},
res: {
on: (method, cb) => {
if (!httpsMock.listeners[method]) { httpsMock.listeners[method] = []; }
httpsMock.listeners[method].push(cb);
}
},
request: function(opts, pipe) {
this.opts = opts;
pipe({
on: (method, cb) => {
if (!this.listeners[method]) { this.listeners[method] = []; }
this.listeners[method].push(cb);
}
});
pipe(this.res);
return {
end: () => {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment