-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Receiptful/feature/timeout
Add optional timeout to requests
- Loading branch information
Showing
12 changed files
with
324 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"preset": "airbnb", | ||
"disallowMultipleVarDecl": { | ||
"allExcept": ["require"] | ||
}, | ||
"maxErrors": "Infinity", | ||
"requireTrailingComma": null, | ||
"requirePaddingNewLinesAfterBlocks": null, | ||
"requirePaddingNewLinesBeforeLineComments": null, | ||
"requireSpacesInsideObjectBrackets": "all", | ||
"disallowSpacesInFunctionExpression": { | ||
"beforeOpeningRoundBrace": true | ||
}, | ||
"disallowSpacesInAnonymousFunctionExpression": { | ||
"beforeOpeningRoundBrace": true | ||
}, | ||
"requireSpacesInAnonymousFunctionExpression": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
node_js: | ||
- '6' | ||
- '5' | ||
- '4' | ||
deploy: | ||
provider: npm | ||
api_key: | ||
secure: Fj94zdVEW7vdnA2lQ2J2u5Jgt49XG9t5vTWf6/wHrhC85b1p+QGCNbfYg7+NkiCK7ChDealidty2ZPEcjdAlBErYbg29caKT6iNgVJ+sebm5ECy/V8Vfy6bqD8M6eI5kcOR2Iwwn22bt9P2kown/O3uZvvGd58eUpDBsf/Ru5DsoMt4/gNqQLZdAE+GWVqPVA4R7hL1ehCn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9KIKApyDnZ5jlaJO5FpaIWaSYTvUHoc/sPXUwLSYQFWYzWm6FhbvkIa3wQ7xdZxvoi70+e5iqsuW+LV9CzoVNFOBgvpPAj7t2cev2jgS+b7f3Z4ivG02hse/Hyc0U8jWDxyTPvkc= | ||
notifications: | ||
slack: | ||
secure: oHvm9eOny5Pg3NzQash8U75BWMZ9oT9liM2LoOmTZTpKa04nKhkwQSYdpG7VkRK9Txfvh0zg2F2lC35QrbiP7T3IqkzRF5ktqxLnO8GhNhXd754vZ3b6k093hP9l8OG8tEKqkEi7uceV+1qkmgaNTbY8qk0641oBvqM1J1cXuQTmdtixl1+fXCkybfiVNRmDoAEEk0vDB+/MVLPvUSQigvZmI85pLmGlfZWbJ1PG/lIcU3tJ48ys4T18P4C97xN8KZIxcakmbWFzcB8iS/9t3HVyuS9CvSFqab3gRVc1TIcCwIApgGxtAFQtDerz4haWnodjh18+KGwRxRUqLP3erw1s1NSbhgQzC+kfV0IL14tdrvPuA0f0edwAv4qA9s9eyjgmo/Q5BdWx5TWTNpKC4Umj6IzE5YrCT0aHxqMTT1KTqw4e4Lv+Ml6ScZhi34vhe4zo9QF0oCmRJ+8a203SgODni4l2rmW1FsYd+kGCKD9e8CBjI7kr5cPkPr6h1wgsPH6x7iW4SxWOymABdqh6HPCnMHK63fRo7wvZfw50Hrtv9ieCc64tx2TIYdxjMBHlvbxOCi7WkJSH5VXq7XAK+7zrVSuvyO27DY6DPhPS8A4YGEcOmoWfmeanTGUkpjcciyN3w+TfSfDbr7K3LCWAG5r7SynUWEWJqZW3N8XeE1k= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const logger = require('../lib/logger'), | ||
sinon = require('sinon'); | ||
|
||
describe('logger', () => { | ||
const self = { }; | ||
|
||
beforeEach(() => self.sandbox = sinon.sandbox.create()); | ||
afterEach(() => self.sandbox.restore()); | ||
|
||
it('Should be set to a log level of zero by default', () => { | ||
logger.logLevel.should.equal(0); | ||
}); | ||
|
||
it('Should log an error at any log level', () => { | ||
const spy = self.sandbox.spy(console, 'error'); | ||
logger.error('Logging test error at 0'); | ||
logger.level = 1; | ||
logger.error('Logging test error at 1'); | ||
|
||
sinon.assert.calledTwice(spy); | ||
}); | ||
|
||
it('Should log info at level one', () => { | ||
var spy = self.sandbox.spy(console, 'log'); | ||
logger.logLevel = 1; | ||
logger.info('Logging test error at 1'); | ||
|
||
sinon.assert.calledOnce(spy); | ||
}); | ||
|
||
it('Should not log info at level zero', () => { | ||
const spy = self.sandbox.spy(console, 'log'); | ||
logger.logLevel = 0; | ||
logger.info('Logging test error at 1'); | ||
|
||
sinon.assert.notCalled(spy); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
'use strict'; | ||
|
||
const Request = require('../lib/request'), | ||
chai = require('chai'), | ||
nock = require('nock'); | ||
|
||
const should = chai.should(); | ||
|
||
describe('Request', () => { | ||
beforeEach(() => nock.cleanAll()); | ||
|
||
const rOAuth = new Request({ | ||
hostname: 'http://foo.com', | ||
consumerKey: 'foo', | ||
secret: 'foo', | ||
headers: { | ||
test: 'header' | ||
} | ||
}); | ||
|
||
const rBasic = new Request({ | ||
hostname: 'https://foo.com', | ||
ssl: true, | ||
port: 443, | ||
consumerKey: 'foo', | ||
secret: 'foo', | ||
headers: { | ||
test: 'header' | ||
} | ||
}); | ||
|
||
it('Should return an error if hostname is missing', () => { | ||
should.Throw(() => { | ||
new Request(); | ||
}, Error); | ||
}); | ||
|
||
it('Should return an error on bad request', done => { | ||
const api = nock('http://foo.com') | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.post('/orders?xxx', {}) | ||
.reply(400, { success: true }); | ||
|
||
rOAuth.complete('post', '/orders', {}, err => { | ||
err.should.be.an.instanceof(Error); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return an error on internal server error', done => { | ||
const api = nock('http://foo.com') | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.post('/orders?xxx', {}) | ||
.reply(500, { success: true }); | ||
|
||
rOAuth.complete('post', '/orders', {}, err => { | ||
err.should.be.an.instanceof(Error); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return an error the request JSON is malformed', done => { | ||
const api = nock('http://foo.com') | ||
.defaultReplyHeaders({ | ||
'content-type': 'application/json' | ||
}) | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.post('/orders?xxx', {}) | ||
.reply(200, '<malformed>'); | ||
|
||
rOAuth.complete('post', '/orders', {}, err => { | ||
err.should.be.an.instanceof(Error); | ||
err.message.should.match(/Unexpected token </); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return content for http using OAuth', done => { | ||
const api = nock('http://foo.com') | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.post('/orders?xxx', {}) | ||
.reply(200, '{ "success": true }'); | ||
|
||
rOAuth.complete('post', '/orders', {}, (err, data) => { | ||
should.not.exist(err); | ||
data.should.be.an.instanceof(Object) | ||
.and.have.property('success', true); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should support data for GET requests', done => { | ||
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers | ||
const api = nock('https://foo.com/') | ||
.get('/orders') | ||
.query({ | ||
consumer_key: 'foo', | ||
consumer_secret: 'foo', | ||
filter: { | ||
limit: 10 | ||
} | ||
}) | ||
.reply(200, '{ "success": true }', { | ||
'content-type': 'application/json' | ||
}); | ||
|
||
rBasic.complete('get', '/orders', { 'filter[limit]': 10 }, (err, data) => { | ||
should.not.exist(err); | ||
data.should.be.an.instanceof(Object) | ||
.and.have.property('success', true); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return content for https using Basic Auth', done => { | ||
const api = nock('https://foo.com/') | ||
.post('/orders') | ||
.query(true) | ||
.reply(200, '{ "success": true }'); | ||
|
||
rBasic.complete('post', '/orders', {}, (err, data) => { | ||
should.not.exist(err); | ||
data.should.be.an.instanceof(Object) | ||
.and.have.property('success', true); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return content for when not a json', done => { | ||
const api = nock('http://foo.com') | ||
.defaultReplyHeaders({ | ||
'content-type': 'application/xml' | ||
}) | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.post('/orders?xxx', {}) | ||
.reply(200, '<xml></xml>'); | ||
|
||
rOAuth.complete('post', '/orders', {}, (err, data) => { | ||
api.done(); | ||
should.not.exist(err); | ||
data.should.equal('<xml></xml>'); | ||
api.done(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Should return an error if "errors" are found in the response', done => { | ||
const api = nock('https://foo.com') | ||
.filteringPath(/\?.*/g, '?xxx') | ||
.get('/errors?xxx') | ||
.reply(200, '{ "errors": ["An error has occurred."] }', { | ||
'content-type': 'application/json' | ||
}); | ||
|
||
rBasic.complete('get', '/errors', {}, err => { | ||
api.done(); | ||
err.should.be.an.instanceof(Error); | ||
err.message.should.equal('["An error has occurred."]'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.