sapi
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,2 +0,5 @@ | ||
### 0.0.4 (SNAPSHOT) | ||
### 0.0.5 (SNAPSHOT) | ||
* Handle non-200 endpoint response. | ||
### 0.0.4 | ||
* Add proxy support. | ||
@@ -3,0 +6,0 @@ * Modify endpoint callback to return SAPI response object. |
@@ -31,2 +31,5 @@ /** | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
@@ -33,0 +36,0 @@ }; |
@@ -31,2 +31,8 @@ /** | ||
cb(null, result); | ||
}, | ||
206: function (result) { | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
@@ -33,0 +39,0 @@ }; |
@@ -31,2 +31,8 @@ /** | ||
cb(null, result); | ||
}, | ||
206: function (result) { | ||
cb(null, result); | ||
}, | ||
400: function (result) { | ||
cb(new Error('Validation error'), result); | ||
} | ||
@@ -33,0 +39,0 @@ }; |
@@ -29,6 +29,5 @@ var request = require('request'), | ||
}; | ||
// use environment variable http_proxy if proxy is not set via Sapi#proxy | ||
if (proxy || process.env.http_proxy) { | ||
params.proxy = proxy || process.env.http_proxy; | ||
if (proxy) { | ||
params.proxy = proxy; | ||
} | ||
@@ -64,2 +63,6 @@ | ||
this.url = (url || 'http://api.sensis.com.au/ob-20110511/test').replace(/\/$/, ''); | ||
if (process.env.http_proxy) { | ||
this._proxy = process.env.http_proxy; | ||
} | ||
} | ||
@@ -66,0 +69,0 @@ |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"homepage": "http://github.com/cliffano/sapi", | ||
@@ -14,0 +14,0 @@ "author": "Cliffano Subagio <blah@cliffano.com> (http://blog.cliffano.com)", |
@@ -1,2 +0,2 @@ | ||
sapi [![http://travis-ci.org/cliffano/sapi](https://secure.travis-ci.org/cliffano/sapi.png?branch=master)](http://travis-ci.org/cliffano/sapi) | ||
Sapi [![http://travis-ci.org/cliffano/sapi](https://secure.travis-ci.org/cliffano/sapi.png?branch=master)](http://travis-ci.org/cliffano/sapi) | ||
----------- | ||
@@ -6,3 +6,3 @@ | ||
This is handy when you want to use [Sensis API](http://developers.sensis.com.au/about) service from a Node.js application. This Sapi module provides a chainable interface to set the endpoint parameters. | ||
This is handy when you want to use [Sensis API](http://developers.sensis.com.au/about) service from a Node.js application. Sapi module provides a chainable interface to set the endpoint parameters. | ||
@@ -82,2 +82,2 @@ Tested with Sensis API version ob-20110511. | ||
* [Report](http://developers.sensis.com.au/docs/endpoint_reference/Report) | ||
* [Metadata](http://developers.sensis.com.au/docs/endpoint_reference/Metadata) | ||
* [Metadata](http://developers.sensis.com.au/docs/endpoint_reference/Metadata) |
@@ -10,3 +10,3 @@ var bag = require('bagofholding'), | ||
function create(checks, mocks) { | ||
return sandbox.require('../../lib/getbylistingid', { | ||
return sandbox.require('../../lib/endpoint/getbylistingid', { | ||
requires: mocks ? mocks.requires : {}, | ||
@@ -20,10 +20,55 @@ globals: {} | ||
mocks = {}; | ||
getbylistingid = create(checks, mocks); | ||
}); | ||
describe('bar', function () { | ||
describe('name', function () { | ||
it('should foo when bar', function () { | ||
it('should have endpoint name', function () { | ||
getbylistingid.name.should.equal('getByListingId'); | ||
}); | ||
}); | ||
describe('params', function () { | ||
it('should have required params', function () { | ||
should.exist(getbylistingid.params.required); | ||
}); | ||
it('should have optional params', function () { | ||
should.exist(getbylistingid.params.optional); | ||
}); | ||
}); | ||
describe('path', function () { | ||
it('should return endpoint name as path', function () { | ||
getbylistingid.path().should.equal('getByListingId'); | ||
}); | ||
}); | ||
describe('handlers', function () { | ||
it('should pass result to callback when success handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
getbylistingid.handlers(cb)['200']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
it('should pass error and result to callback when validation error handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
getbylistingid.handlers(cb)['400']({ foo: 'bar' }); | ||
checks.handler_err.message.should.equal('Validation error'); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
}); | ||
}); | ||
@@ -10,3 +10,3 @@ var bag = require('bagofholding'), | ||
function create(checks, mocks) { | ||
return sandbox.require('../../lib/metadata', { | ||
return sandbox.require('../../lib/endpoint/metadata', { | ||
requires: mocks ? mocks.requires : {}, | ||
@@ -20,10 +20,44 @@ globals: {} | ||
mocks = {}; | ||
metadata = create(checks, mocks); | ||
}); | ||
describe('bar', function () { | ||
describe('name', function () { | ||
it('should foo when bar', function () { | ||
it('should have endpoint name', function () { | ||
metadata.name.should.equal('metadata'); | ||
}); | ||
}); | ||
describe('params', function () { | ||
it('should have required params', function () { | ||
should.exist(metadata.params.required); | ||
}); | ||
it('should have optional params', function () { | ||
should.exist(metadata.params.optional); | ||
}); | ||
}); | ||
describe('path', function () { | ||
it('should return endpoint name and dataType param as path', function () { | ||
metadata.path({ dataType: 'somedatatype' }).should.equal('metadata/somedatatype'); | ||
}); | ||
}); | ||
describe('handlers', function () { | ||
it('should pass result to callback when success handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
metadata.handlers(cb)['200']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
}); | ||
}); | ||
@@ -10,3 +10,3 @@ var bag = require('bagofholding'), | ||
function create(checks, mocks) { | ||
return sandbox.require('../../lib/report', { | ||
return sandbox.require('../../lib/endpoint/report', { | ||
requires: mocks ? mocks.requires : {}, | ||
@@ -20,10 +20,66 @@ globals: {} | ||
mocks = {}; | ||
report = create(checks, mocks); | ||
}); | ||
describe('bar', function () { | ||
describe('name', function () { | ||
it('should foo when bar', function () { | ||
it('should have endpoint name', function () { | ||
report.name.should.equal('report'); | ||
}); | ||
}); | ||
describe('params', function () { | ||
it('should have required params', function () { | ||
should.exist(report.params.required); | ||
}); | ||
it('should have optional params', function () { | ||
should.exist(report.params.optional); | ||
}); | ||
}); | ||
describe('path', function () { | ||
it('should return endpoint name and eventName param as path', function () { | ||
report.path({ eventName: 'someeventname' }).should.equal('report/someeventname'); | ||
}); | ||
}); | ||
describe('handlers', function () { | ||
it('should pass result to callback when success handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
report.handlers(cb)['200']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
it('should pass result to callback when search modified handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
report.handlers(cb)['206']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
it('should pass error and result to callback when validation error handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
report.handlers(cb)['400']({ foo: 'bar' }); | ||
checks.handler_err.message.should.equal('Validation error'); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
}); | ||
}); | ||
@@ -10,3 +10,3 @@ var bag = require('bagofholding'), | ||
function create(checks, mocks) { | ||
return sandbox.require('../../lib/search', { | ||
return sandbox.require('../../lib/endpoint/search', { | ||
requires: mocks ? mocks.requires : {}, | ||
@@ -20,10 +20,66 @@ globals: {} | ||
mocks = {}; | ||
search = create(checks, mocks); | ||
}); | ||
describe('bar', function () { | ||
describe('name', function () { | ||
it('should foo when bar', function () { | ||
it('should have endpoint name', function () { | ||
search.name.should.equal('search'); | ||
}); | ||
}); | ||
describe('params', function () { | ||
it('should have required params', function () { | ||
should.exist(search.params.required); | ||
}); | ||
it('should have optional params', function () { | ||
should.exist(search.params.optional); | ||
}); | ||
}); | ||
describe('path', function () { | ||
it('should return endpoint name as path', function () { | ||
search.path().should.equal('search'); | ||
}); | ||
}); | ||
describe('handlers', function () { | ||
it('should pass result to callback when success handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
search.handlers(cb)['200']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
it('should pass result to callback when search modified handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
search.handlers(cb)['206']({ foo: 'bar' }); | ||
should.not.exist(checks.handler_err); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
it('should pass error and result to callback when validation error handler is called', function (done) { | ||
function cb(err, result) { | ||
checks.handler_err = err; | ||
checks.handler_result = result; | ||
done(); | ||
} | ||
search.handlers(cb)['400']({ foo: 'bar' }); | ||
checks.handler_err.message.should.equal('Validation error'); | ||
checks.handler_result.foo.should.equal('bar'); | ||
}); | ||
}); | ||
}); | ||
134
test/sapi.js
var bag = require('bagofholding'), | ||
_jscov = require('../lib/sapi'), | ||
sandbox = require('sandboxed-module'), | ||
@@ -12,3 +13,3 @@ should = require('should'), | ||
requires: mocks ? mocks.requires : {}, | ||
globals: {} | ||
globals: mocks ? mocks.globals : {} | ||
}); | ||
@@ -22,8 +23,135 @@ } | ||
describe('bar', function () { | ||
describe('sapi', function () { | ||
it('should foo when bar', function () { | ||
it('should set key and default url when url is not provided', function () { | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.params.key.should.equal('somekey'); | ||
sapi.url.should.equal('http://api.sensis.com.au/ob-20110511/test'); | ||
}); | ||
it('should set specified key and url when provided', function () { | ||
sapi = new (create(checks, mocks))('somekey', 'http://someurl'); | ||
sapi.params.key.should.equal('somekey'); | ||
sapi.url.should.equal('http://someurl'); | ||
}); | ||
it('should pass error to callback when an error occurs while sending request', function (done) { | ||
mocks.request_err = new Error('someerror'); | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))(); | ||
sapi.search(function cb(err, result) { | ||
checks.sapi_http_err = err; | ||
checks.sapi_http_result = result; | ||
done(); | ||
}); | ||
checks.sapi_http_err.message.should.equal('someerror'); | ||
should.not.exist(checks.sapi_http_result); | ||
}); | ||
it('should pass authentication failed error to callback when result has status code 403 and key is provided', function (done) { | ||
mocks.request_result = { statusCode: 403 }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.search(function cb(err, result) { | ||
checks.sapi_http_err = err; | ||
checks.sapi_http_result = result; | ||
done(); | ||
}); | ||
checks.sapi_http_err.message.should.equal('Authentication failed - invalid key somekey'); | ||
should.not.exist(checks.sapi_http_result); | ||
}); | ||
it('should pass authentication required error to callback when result has status code 403 and key is not provided', function (done) { | ||
mocks.request_result = { statusCode: 403 }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))(); | ||
sapi.search(function cb(err, result) { | ||
checks.sapi_http_err = err; | ||
checks.sapi_http_result = result; | ||
done(); | ||
}); | ||
checks.sapi_http_err.message.should.equal('SAPI requires authentication - set key to SAPI instance'); | ||
should.not.exist(checks.sapi_http_result); | ||
}); | ||
it('should pass error with status code and body to callback when request responds with unexpected status code', function (done) { | ||
mocks.request_result = { statusCode: 503, body: 'unexpectedbody' }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.search(function cb(err, result) { | ||
checks.sapi_http_err = err; | ||
checks.sapi_http_result = result; | ||
done(); | ||
}); | ||
checks.sapi_http_err.message.should.equal('Unexpected status code 503 from SAPI\nResponse body:\nunexpectedbody'); | ||
should.not.exist(checks.sapi_http_result); | ||
}); | ||
it('should construct params when parameter functions are chained', function (done) { | ||
mocks.request_result = { statusCode: 403 }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.query('somequery').location('somelocation').search(function cb(err, result) { | ||
done(); | ||
}); | ||
checks.request_opts.qs.query.should.equal('somequery'); | ||
checks.request_opts.qs.location.should.equal('somelocation'); | ||
}); | ||
it('should pass result to custom handler callback', function (done) { | ||
mocks.request_result = { statusCode: 200, body: '{ "foo": "bar" }' }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.search(function cb(err, result) { | ||
checks.sapi_http_err = err; | ||
checks.sapi_http_result = result; | ||
done(); | ||
}); | ||
should.not.exist(checks.sapi_http_err); | ||
checks.sapi_http_result.foo.should.equal('bar'); | ||
}); | ||
}); | ||
describe('proxy', function () { | ||
it('should set proxy', function () { | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.proxy('http://someproxy'); | ||
sapi._proxy.should.equal('http://someproxy'); | ||
}); | ||
it('should allow chainable proxy setting', function () { | ||
sapi = new (create(checks, mocks))('somekey').proxy('http://someproxy'); | ||
sapi._proxy.should.equal('http://someproxy'); | ||
}); | ||
it('should set environment variable http_proxy as proxy when it is not set via proxy function', function (done) { | ||
mocks.process_env = { http_proxy: 'http://someproxy' }; | ||
mocks.request_result = { statusCode: 403 }; | ||
mocks.requires = { | ||
request: bag.mock.request(checks, mocks) | ||
}; | ||
mocks.globals = { | ||
process: bag.mock.process(checks, mocks) | ||
}; | ||
sapi = new (create(checks, mocks))('somekey'); | ||
sapi.search(function cb(err, result) { | ||
done(); | ||
}); | ||
checks.request_opts.proxy.should.equal('http://someproxy'); | ||
}); | ||
}); | ||
}); | ||
25678
635
82