regard
Version:
Sugar-interface to access multiple data sources.
129 lines (106 loc) • 3.52 kB
JavaScript
var _ = require('lodash'),
Chai = require('chai'),
Connector = require('../../lib/connector'),
Q = require('q'),
HttpConnector = require('../../lib/connectors/http');
var assert = Chai.assert,
expect = Chai.expect;
var clazz = HttpConnector,
foo, bar;
function createPromise(func, param) {
var processing = Q.defer();
func(param, processing.resolve, processing.reject);
return processing.promise;
}
var mockServer = require('../mockHttpServer');
describe('HttpConnector', function () {
before(mockServer.start);
after(mockServer.stop);
it('should must have key equals to its filename ("http")', function () {
foo = clazz();
expect(foo.key).to.eql(Connector.generateKey('../../lib/connectors/http'));
});
it('should have method "get" in context', function () {
foo = clazz();
expect(foo.context.method).to.eql('get');
});
it('should accept path starts with "http://"', function () {
foo = clazz();
expect(foo.checkPath('http://')).to.be.ok;
expect(foo.checkPath('https://')).to.not.be.ok;
});
it('should accept unirest methods '+HttpConnector.Methods, function () {
foo = clazz();
_.forEach(HttpConnector.Methods, function (value) {
expect(foo.checkMethod(value)).to.be.ok;
});
});
it('should have beforeRequest ( [method] [path] [headers] [body] ) method', function () {
foo = clazz();
bar = {
path: 'foo',
context: {
headers: {},
method: 'get'
}
};
var func = foo.handlers.get('get').before;
expect(bar.path).to.eql('foo');
func(bar, 'bar');
expect(bar.path).to.eql('foo/bar');
func = foo.handlers.get('default').before;
func(bar, 'bax');
expect(bar.path).to.eql('foo/bar/bax');
func(bar, 'head');
expect(bar.context.method).to.eql('head');
func(bar, 'patch', 'baz');
expect(bar.context.method).to.eql('patch');
expect(bar.path).to.eql('foo/bar/bax/baz');
func(bar, 'put', {foo:'bar'});
expect(bar.context).to.eql({method:'put', headers:{foo:'bar'}});
func(bar, {foo:'baz'});
expect(bar.context).to.eql({method:'put', headers:{foo:'baz'}});
func(bar, 'post', 'x', {foo:'bar'});
expect(bar.context).to.eql({method:'post', headers:{foo:'bar'}});
func(bar, 'post', 'x', {foo:'bar'}, {foo:'bar'});
expect(bar.context.body).to.eql({foo:'bar'});
});
it('should failed when bad url is given', function () {
foo = clazz();
bar = {
path: 'foo',
context: HttpConnector.SETTINGS
};
var func = createPromise(_.get(foo.handlers.get('default'), 'process'), bar);
return func.then(function () {
assert().fail();
}, function (err) {
expect(_.isError(err.error)).to.be.ok;
});
});
it('should succeed when good url is given', function () {
foo = clazz();
bar = {
path: 'http://localhost:3001/request',
context: {
method: 'get',
headers: {},
ssl: true,
foo: 'bar'
}
};
var func = createPromise(_.get(foo.handlers.get('default'), 'process'), bar);
return func.then(function (res) {
expect(res.body.url).to.eql('http://localhost:3001/request');
}, function (err) {
assert().fail();
});
});
it('should have helper for each REST methods', function () {
foo = clazz();
_.forEach(_.remove(HttpConnector.Methods, 'HEAD'), function (x) {
var func = foo.handlers[x.toLowerCase()];
expect(func).is.a('function');
});
});
});