endpointsjs
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -26,3 +26,3 @@ 'use strict'; | ||
return gulp.src('') | ||
.pipe(shell(['mocha test/node-spec -R spec'])); | ||
.pipe(shell(['mocha test/node-spec -R spec -G'])); | ||
} | ||
@@ -29,0 +29,0 @@ |
{ | ||
"name": "endpointsjs", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Simple library for HTTP service clients", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
'use strict'; | ||
var Method = require('./http-method'); | ||
var _ = require('lodash'); | ||
var methodHelper = function(methodString) { | ||
var methodHelper = function(methodString, endpoint) { | ||
var httpMethod = function() { | ||
var endpoint = this; | ||
// Concatenate the Endpoint's thenApplies with the methods thenApplies, | ||
// such that the order goes from the Endpoint's thenApplies to the | ||
// method's thenApplies. Ordering from most general to most specific. | ||
var thenApplies = endpoint.thenApplies.concat( | ||
endpoint[methodString].thenApplies); | ||
var headers = _.extend({}, endpoint.headers, | ||
endpoint[methodString].headers); | ||
var endpointConfig = { | ||
headers: endpoint.headers, | ||
headers: headers, | ||
thenApplies: thenApplies, | ||
@@ -19,0 +22,0 @@ domain: endpoint.getDomain(), |
@@ -6,9 +6,13 @@ 'use strict'; | ||
var request = require('superagent'); | ||
var httpConfigurable = require('./http-configurable'); | ||
function Method(method, endpointConfig) { | ||
var thenApplies = endpointConfig.thenApplies || []; | ||
this.initHttpConfigurable(); | ||
this.mergeThenApplies(thenApplies); | ||
this.params = {}; | ||
this.method = method; | ||
this.headers = endpointConfig.headers || {}; | ||
this.thenApplies = endpointConfig.thenApplies || []; | ||
this.domain = endpointConfig.domain || ''; | ||
@@ -20,16 +24,2 @@ this.pattern = endpointConfig.pattern || '/'; | ||
Method.prototype.header = function(headerKey, headerValue) { | ||
this.headers[headerKey] = headerValue; | ||
return this; | ||
}; | ||
Method.prototype.contentType = function(mimeType) { | ||
return this.header('Content-Type', mimeType); | ||
}; | ||
Method.prototype.accepts = function(mimeType) { | ||
return this.header('Accepts', mimeType); | ||
}; | ||
Method.prototype.param = function(key, value) { | ||
@@ -80,14 +70,2 @@ this.params[key] = value; | ||
Method.prototype.thenApply = function(onFulfilled, onRejected, onProgress) { | ||
var thenApply = { | ||
onFulfilled: onFulfilled, | ||
onRejected: onRejected, | ||
onProgress: onProgress | ||
}; | ||
this.thenApplies.push(thenApply); | ||
return this; | ||
}; | ||
Method.prototype.send = function() { | ||
@@ -128,2 +106,5 @@ var requestObject = this.createRequestObject(); | ||
_.extend(Method.prototype, httpConfigurable); | ||
module.exports = Method; |
'use strict'; | ||
var Create = require('./create'); | ||
var Create = require('./endpoint'); | ||
@@ -5,0 +5,0 @@ var Endpoints = {}; |
@@ -15,2 +15,36 @@ 'use strict'; | ||
describe('interface layout', function() { | ||
var endpoint = Endpoints.create(); | ||
it('endpoint.header returns the endpoint', function() { | ||
endpoint.header('key', 'val') | ||
.should.equal(endpoint); | ||
}); | ||
it('endpoint.domain returns the endpoint', function() { | ||
endpoint.domain('domain') | ||
.should.equal(endpoint); | ||
}); | ||
it('endpoint.contentType returns the endpoint', function() { | ||
endpoint.contentType('json') | ||
.should.equal(endpoint); | ||
}); | ||
it('endpoint.accept returns the endpoint', function() { | ||
endpoint.accept('xml') | ||
.should.equal(endpoint); | ||
}); | ||
it('endpoint.pattern returns the endpoint', function() { | ||
endpoint.pattern('/url') | ||
.should.equal(endpoint); | ||
}); | ||
it('endpoint.methods returns the endpoint', function() { | ||
endpoint.methods('get') | ||
.should.equal(endpoint); | ||
}); | ||
}); | ||
describe('bare bones behavior', function() { | ||
@@ -117,3 +151,3 @@ beforeEach(function() { | ||
describe('rejects the promise on errors', function() { | ||
describe('promise rejection', function() { | ||
beforeEach(function() { | ||
@@ -128,3 +162,3 @@ var endpoint = Endpoints.create() | ||
it('permutes the promise with a specified permutation', function(done) { | ||
it('rejects promises on errors', function(done) { | ||
promise | ||
@@ -159,2 +193,147 @@ .should.eventually.be.rejectedWith('connect ECONNREFUSED') | ||
}); | ||
describe('setting endpoint headers', function() { | ||
var promise; | ||
beforeEach(function() { | ||
var endpoint = Endpoints.create('/endpoint') | ||
.methods('get') | ||
.accept('application/xml') | ||
.contentType('application/json') | ||
.header('from', 'fake@email.com') | ||
.domain('http://localhost:9000'); | ||
mock.get('/endpoint').reply(200); | ||
promise = endpoint.get() | ||
.send(); | ||
}); | ||
it('sets the accept header from the endpoint', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('accept') | ||
.should.eventually.equal('application/xml') | ||
.notify(done); | ||
}); | ||
it('sets the Content Type header from the endpoint', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('content-type') | ||
.should.eventually.equal('application/json') | ||
.notify(done); | ||
}); | ||
it('sets other request header fields from the endpoint', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('from') | ||
.should.eventually.equal('fake@email.com') | ||
.notify(done); | ||
}); | ||
}); | ||
describe('setting per method object headers', function() { | ||
var promise; | ||
beforeEach(function() { | ||
var endpoint = Endpoints.create('/endpoint') | ||
.methods('get') | ||
.domain('http://localhost:9000'); | ||
mock.get('/endpoint').reply(200); | ||
endpoint.get | ||
.accept('application/xml') | ||
.contentType('application/json') | ||
.header('from', 'fake@email.com'); | ||
promise = endpoint | ||
.get() | ||
.send(); | ||
}); | ||
it('sets the accept header from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('accept') | ||
.should.eventually.equal('application/xml') | ||
.notify(done); | ||
}); | ||
it('sets the Content Type header from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('content-type') | ||
.should.eventually.equal('application/json') | ||
.notify(done); | ||
}); | ||
it('sets other request header fields from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('from') | ||
.should.eventually.equal('fake@email.com') | ||
.notify(done); | ||
}); | ||
}); | ||
describe('setting per method instance headers', function() { | ||
var promise; | ||
beforeEach(function() { | ||
var endpoint = Endpoints.create('/endpoint') | ||
.methods('get') | ||
.domain('http://localhost:9000'); | ||
mock.get('/endpoint').reply(200); | ||
promise = endpoint.get() | ||
.accept('application/xml') | ||
.contentType('application/json') | ||
.header('from', 'fake@email.com') | ||
.send(); | ||
}); | ||
it('sets the accept header from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('accept') | ||
.should.eventually.equal('application/xml') | ||
.notify(done); | ||
}); | ||
it('sets the Content Type header from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('content-type') | ||
.should.eventually.equal('application/json') | ||
.notify(done); | ||
}); | ||
it('sets other request header fields from a method', function(done) { | ||
promise | ||
.invoke('rawRequestResponse') | ||
.get('req') | ||
.get('_headers') | ||
.get('from') | ||
.should.eventually.equal('fake@email.com') | ||
.notify(done); | ||
}); | ||
}); | ||
}); |
33423
22
675