restjs-api
Advanced tools
Comparing version 0.1.0 to 0.1.1
35
index.js
@@ -1,4 +0,4 @@ | ||
var request = require("request"), | ||
configProperties = [ | ||
"qs", "headers", "body", "followRedirect", | ||
var request = require("request"); | ||
var configProperties = [ | ||
"qs", "body", "followRedirect", | ||
"followAllRedirects", "maxRedirects", "encoding", | ||
@@ -13,3 +13,4 @@ "timeout", "proxy", "jar", "auth", "oauth", "aws" | ||
if (!config.endpoint) throw Error("'endpoint' property is missing."); | ||
if (config.headers && typeof config.headers!=='object') throw Error("'headers' property is invalid."); | ||
// variables | ||
@@ -82,7 +83,8 @@ var self = this; | ||
if (typeof options !== 'object') return cb(new Error("'options' argument must be an object instance.")); | ||
if (options.headers && typeof options.headers!=='object') cb(Error("'options.headers' property is invalid.")); | ||
// Set the options that will be used in the request. | ||
// The set of options will be the result of merging globalConfig and options. | ||
var optionsToUse = {}; | ||
configProperties.forEach(function(prop){ | ||
configProperties.forEach(function (prop) { | ||
if (prop in options) optionsToUse[prop] = options[prop]; | ||
@@ -92,2 +94,5 @@ else if (prop in self.config) optionsToUse[prop] = self.config[prop]; | ||
// set default headers | ||
optionsToUse.headers = merge(options.headers, self.config.headers); | ||
// set required options | ||
@@ -134,3 +139,21 @@ optionsToUse.method = options.method || "GET"; | ||
}; | ||
// Merges two objects. Properties from object 'a' prevail over properties from object 'b'. | ||
var merge = function (a, b) { | ||
var result = {}; | ||
var propsA = Object | ||
.keys(a || {}) | ||
.forEach(function (propA) { result[propA] = a[propA]; }); | ||
Object | ||
.keys(b || {}) | ||
.filter(function (propB) { return !(propB in result); }) | ||
.forEach(function (propB) { | ||
result[propB] = b[propB]; | ||
}); | ||
return result; | ||
}; | ||
}; | ||
{ | ||
"name": "restjs-api", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Kidozen's connector to invoke REST services", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -38,2 +38,3 @@ # REST client for Node.js | ||
* `endpoint`: Required string. URL to the service. | ||
* `headers`: Optional object containing http headers, defaults to {}. | ||
@@ -40,0 +41,0 @@ ``` |
@@ -1,2 +0,3 @@ | ||
var assert = require('assert'); | ||
var assert = require('assert'); | ||
var http = require('http'); | ||
@@ -6,6 +7,20 @@ describe('Rest connector:', function(){ | ||
var Connector = require('../index.js'); | ||
var config = { | ||
var server = null; | ||
var config = null; | ||
beforeEach(function() { | ||
config = { | ||
endpoint: 'http://localhost:9615' | ||
}; | ||
}); | ||
afterEach(function(done) { | ||
if (server) { | ||
server.close(done); | ||
server = null; | ||
} else { | ||
done(); | ||
} | ||
}); | ||
it('Can be created', function(){ | ||
@@ -50,4 +65,4 @@ assert.ok(Connector); | ||
it('should make HTTP requests', function (done) { | ||
var http = require('http'); | ||
http.createServer(function (req, res) { | ||
server = http.createServer(function (req, res) { | ||
assert.equal('GET', req.method); | ||
@@ -69,2 +84,45 @@ assert.equal('/foo', req.url); | ||
}); | ||
describe ("headers", function() { | ||
it('should use headers from configuration', function (done) { | ||
server = http.createServer(function (req, res) { | ||
assert.equal("bar", req.headers.foo) | ||
res.end(); | ||
}).listen(9615); | ||
config.headers = { foo: "bar" }; | ||
var instance = new Connector(config); | ||
instance.exec({ method:'get', path: '/foo' }, function() { | ||
done(); | ||
}); | ||
}); | ||
it('should use headers from request', function (done) { | ||
server = http.createServer(function (req, res) { | ||
assert.equal("bar", req.headers.foo) | ||
res.end(); | ||
}).listen(9615); | ||
var instance = new Connector(config); | ||
instance.exec({ method:'get', path: '/foo', headers: { foo: "bar" }}, function() { | ||
done(); | ||
}); | ||
}); | ||
it('headers from request should override headers from configuration', function (done) { | ||
server = http.createServer(function (req, res) { | ||
assert.equal("ok", req.headers.foo) | ||
res.end(); | ||
}).listen(9615); | ||
config.headers = { foo: "fail" }; | ||
var instance = new Connector(config); | ||
instance.exec({ method:'get', path: '/foo', headers: { foo: "ok" }}, function() { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14803
236
86