@trayio/threadneedle
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -39,2 +39,10 @@ var when = require('when'); | ||
//The format of the response should always be an object with headers and body | ||
function formatResponse (headers, body) { | ||
return { | ||
headers: ( _.isPlainObject(headers) ? headers : {} ), | ||
body: body | ||
}; | ||
} | ||
// Create the method | ||
@@ -51,75 +59,117 @@ threadneedle[methodName] = function (params) { | ||
logger.info(methodName+': Running method function.'); | ||
return when(config.call(threadneedle, params)); | ||
return when.promise(function (resolve, reject) { | ||
when(config.call(threadneedle, params)) | ||
.done( | ||
function (body) { resolve(formatResponse({}, body)); }, | ||
function (err) { resolve(formatResponse({}, err)); } | ||
); | ||
}); | ||
} | ||
return when.promise(function (resolve, reject) { | ||
// Method, always lowercased | ||
var method = config.method.toLowerCase(); | ||
// Kick off that promise chain | ||
when() | ||
return when.promise(function (resolve, reject) { | ||
// Run a `before` if set on the params. | ||
.then(function () { | ||
logger.info('Running method `'+methodName+'` `before`.'); | ||
return globalize.before.call(threadneedle, config, params); | ||
}) | ||
//Handle afterHeader for resolving | ||
function afterHeadersResolve (body, result) { | ||
// Set a bunch of local variables, formatted and templated | ||
.then(function (result) { | ||
params = result; | ||
logger.info(methodName+': running `afterHeaders` hook'); | ||
globalize.afterHeaders.call(threadneedle, config, null, body, params, result.response) | ||
logger.info(methodName+': substituting parameters'); | ||
.done( | ||
function (headers) { | ||
resolve(formatResponse(headers, body)); | ||
}, | ||
function (error) { | ||
reject(formatResponse({}, error)); | ||
} | ||
); | ||
// Add a temp file parameter for file handling operations | ||
if (config.fileHandler === true) { | ||
params.temp_file = '/tmp/'+guid(); | ||
} | ||
// The URL endpoint. Query parameters allowed from here. | ||
var url = globalize.baseUrl.call(threadneedle, config, params); | ||
// var url = globalize.call(threadneedle, config.globals, 'url', config, params); | ||
//Handle afterHeader for rejecting | ||
function afterHeadersReject (error, payload, response) { | ||
// Add query parameters intelligently, without conflicting from query parameters | ||
// already specified in the URL. | ||
var query = globalize.object.call(threadneedle, 'query', config, params); | ||
_.each(query, function (value, key) { | ||
if (_.isArray(value) && value.length > 0) { | ||
value = value.join(','); | ||
} | ||
if (!_.isUndefined(value) && !(_.isString(value) && value === '')) { | ||
url = setParam(url, key, value); | ||
} | ||
}); | ||
logger.info(methodName+': running `afterHeaders` hook'); | ||
globalize.afterHeaders.call(threadneedle, config, error, payload, params, response) | ||
// The request options, substituted | ||
var options = globalize.object.call(threadneedle, 'options', config, params); | ||
.done( | ||
function (headers) { | ||
reject(formatResponse(headers, error)); | ||
}, | ||
function (err) { | ||
reject(formatResponse({}, err || error)); | ||
} | ||
); | ||
// Post/put/delete data | ||
var data; | ||
switch (method) { | ||
case 'get': | ||
case 'head': | ||
break; | ||
default: | ||
data = globalize.object.call(threadneedle, 'data', config, params); | ||
} | ||
return { | ||
url: url, | ||
data: data, | ||
options: options, | ||
method: method | ||
}; | ||
}) | ||
// Method, always lowercased | ||
var method = config.method.toLowerCase(); | ||
// Run the `beforeRequest` | ||
.then(function (request) { | ||
logger.info(methodName+': running `beforeRequest` hook'); | ||
return globalize.beforeRequest.call(threadneedle, config, request, params); | ||
}) | ||
// Kick off that promise chain | ||
when() | ||
// Run the actual request | ||
.then(function (request) { | ||
// Run a `before` if set on the params. | ||
.then(function () { | ||
logger.info('Running method `'+methodName+'` `before`.'); | ||
return globalize.before.call(threadneedle, config, params); | ||
}) | ||
// Set a bunch of local variables, formatted and templated | ||
.then(function (result) { | ||
params = result; | ||
logger.info(methodName+': substituting parameters'); | ||
// Add a temp file parameter for file handling operations | ||
if (config.fileHandler === true) { | ||
params.temp_file = '/tmp/'+guid(); | ||
} | ||
// The URL endpoint. Query parameters allowed from here. | ||
var url = globalize.baseUrl.call(threadneedle, config, params); | ||
// var url = globalize.call(threadneedle, config.globals, 'url', config, params); | ||
// Add query parameters intelligently, without conflicting from query parameters | ||
// already specified in the URL. | ||
var query = globalize.object.call(threadneedle, 'query', config, params); | ||
_.each(query, function (value, key) { | ||
if (_.isArray(value) && value.length > 0) { | ||
value = value.join(','); | ||
} | ||
if (!_.isUndefined(value) && !(_.isString(value) && value === '')) { | ||
url = setParam(url, key, value); | ||
} | ||
}); | ||
// The request options, substituted | ||
var options = globalize.object.call(threadneedle, 'options', config, params); | ||
// Post/put/delete data | ||
var data; | ||
switch (method) { | ||
case 'get': | ||
case 'head': | ||
break; | ||
default: | ||
data = globalize.object.call(threadneedle, 'data', config, params); | ||
} | ||
return { | ||
url: url, | ||
data: data, | ||
options: options, | ||
method: method | ||
}; | ||
}) | ||
// Run the `beforeRequest` | ||
.then(function (request) { | ||
logger.info(methodName+': running `beforeRequest` hook'); | ||
return globalize.beforeRequest.call(threadneedle, config, request, params); | ||
}) | ||
// Run the actual request | ||
.then(function (request) { | ||
return when.promise(function(resolve, reject) { | ||
@@ -194,19 +244,37 @@ | ||
}); | ||
}) | ||
}); | ||
}) | ||
// Handle the after success and failure messages | ||
.done(function (result) { | ||
logger.info(methodName+': running `afterSuccess` hook'); | ||
globalize.afterSuccess.call(threadneedle, config, result.body, params, result.response).done(resolve, reject); | ||
}, function (err) { | ||
logger.info(methodName+': running `afterFailure` hook', err); | ||
var payload = ( err.payload ? err.payload : err ), | ||
response = ( err.response ? err.response : {} ); | ||
globalize.afterFailure.call(threadneedle, config, payload, params, response).done(reject, reject); | ||
}); | ||
// Handle the after success and failure messages | ||
.done( | ||
function (result) { | ||
logger.info(methodName + ': running `afterSuccess` hook'); | ||
globalize.afterSuccess.call(threadneedle, config, result.body, params, result.response) | ||
.done( | ||
function (body) { afterHeadersResolve(body, result); }, | ||
function (error) { afterHeadersReject(error, result.body, result.response); } | ||
); | ||
}, | ||
function (err) { | ||
var payload = (err.payload ? err.payload : err), | ||
response = (err.response ? err.response : {}); | ||
function rejectAfterHeaders(error) { | ||
afterHeadersReject(error, payload, response); | ||
} | ||
logger.info(methodName + ': running `afterFailure` hook', err); | ||
globalize.afterFailure.call(threadneedle, config, payload, params, response) | ||
.done(rejectAfterHeaders, rejectAfterHeaders); | ||
} | ||
); | ||
}); | ||
}; | ||
}; |
@@ -23,2 +23,10 @@ var when = require('when'); | ||
//The format of the response should always be an object with headers and body | ||
function formatResponse (headers, body) { | ||
return { | ||
headers: ( _.isPlainObject(headers) ? headers : {} ), | ||
body: body | ||
}; | ||
} | ||
threadneedle[methodName] = function (params) { | ||
@@ -32,11 +40,48 @@ | ||
logger.info(methodName+': Running method function.'); | ||
return when(config.call(threadneedle, params)); | ||
return when.promise(function (resolve, reject) { | ||
when(config.call(threadneedle, params)) | ||
.done( | ||
function (body) { resolve(formatResponse({}, body)); }, | ||
function (err) { resolve(formatResponse({}, err)); } | ||
); | ||
}); | ||
} | ||
logger.info('Running method `' + methodName + '` `before`.'); | ||
//Do SOAP stuff | ||
return when.promise(function (resolve, reject) { | ||
//Handle afterHeader for resolving | ||
function afterHeadersResolve (body, result) { | ||
logger.info(methodName+': running `afterHeaders` hook'); | ||
globalize.afterHeaders.call(threadneedle, config, null, body, params, result.response) | ||
.done( | ||
function (headers) { | ||
resolve(formatResponse(headers, body)); | ||
}, | ||
function (error) { | ||
reject(formatResponse({}, error)); | ||
} | ||
); | ||
} | ||
//Handle afterHeader for rejecting | ||
function afterHeadersReject (error, payload, response) { | ||
logger.info(methodName+': running `afterHeaders` hook'); | ||
globalize.afterHeaders.call(threadneedle, config, error, payload, params, response) | ||
.done( | ||
function (headers) { | ||
reject(formatResponse(headers, error)); | ||
}, | ||
function (err) { | ||
reject(formatResponse({}, err || error)); | ||
} | ||
); | ||
} | ||
// Kick off that promise chain | ||
@@ -47,2 +92,3 @@ when() | ||
.then(function () { | ||
logger.info('Running method `' + methodName + '` `before`.'); | ||
return globalize.before.call(threadneedle, config, params); | ||
@@ -189,15 +235,28 @@ }) | ||
.done( | ||
function (result) { | ||
logger.info(methodName+': running `afterSuccess` hook'); | ||
globalize.afterSuccess.call(threadneedle, config, result.body, params, result.response).done(resolve, reject); | ||
}, function (err) { | ||
logger.info(methodName+': running `afterFailure` hook', err); | ||
var payload = ( err.payload ? err.payload : err ), | ||
response = ( err.response ? err.response : {} ); | ||
globalize.afterFailure.call(threadneedle, config, payload, params, response).done(reject, reject); | ||
} | ||
); | ||
function (result) { | ||
logger.info(methodName + ': running `afterSuccess` hook'); | ||
globalize.afterSuccess.call(threadneedle, config, result.body, params, result.response) | ||
.done( | ||
function (body) { afterHeadersResolve(body, result); }, | ||
function (error) { afterHeadersReject(error, result.body, result.response); } | ||
); | ||
}, | ||
function (err) { | ||
var payload = (err.payload ? err.payload : err), | ||
response = (err.response ? err.response : {}); | ||
function rejectAfterHeaders(error) { | ||
afterHeadersReject(error, payload, response); | ||
} | ||
logger.info(methodName + ': running `afterFailure` hook', err); | ||
globalize.afterFailure.call(threadneedle, config, payload, params, response) | ||
.done(rejectAfterHeaders, rejectAfterHeaders); | ||
} | ||
); | ||
}); | ||
@@ -204,0 +263,0 @@ |
@@ -21,3 +21,3 @@ /* | ||
if (_.isFunction(threadneedle._globalOptions.before) && !require('./localOnly')(config, 'before')) { | ||
return when(threadneedle._globalOptions.before(params)); | ||
return when(threadneedle._globalOptions.before(params)); | ||
} | ||
@@ -24,0 +24,0 @@ }) |
@@ -10,3 +10,4 @@ module.exports = { | ||
afterSuccess: require('./afterSuccess'), | ||
afterHeaders: require('./afterHeaders'), | ||
afterFailure: require('./afterFailure') | ||
}; |
{ | ||
"name": "@trayio/threadneedle", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "A framework for simplifying working with HTTP-based APIs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -51,3 +51,3 @@ # threadneedle | ||
* [global](#global) | ||
* [SOAP Mode](#SOAP Mode) | ||
* [SOAP Mode](#soap-mode) | ||
@@ -59,2 +59,4 @@ | ||
### addMethod - REST template | ||
You can declare template-style parameters to be passed into specific fields, using Mustache-style templating. | ||
@@ -72,5 +74,6 @@ | ||
* [before](#before) | ||
* [beforeRequest](#beforeRequest) | ||
* [beforeRequest](#beforerequest) | ||
* [afterSuccess](#aftersuccess) | ||
* [afterFailure](#afterfailure) | ||
* [afterHeaders](#afterheaders) | ||
@@ -277,3 +280,3 @@ `addMethod` uses JavaScript promises (using [When.js](https://github.com/cujojs/when)), which allows for the chaining of multiple API calls together, and smart error handling. | ||
## beforeRequest | ||
### beforeRequest | ||
@@ -341,6 +344,47 @@ If you'd like to do some final checks and tweaks **before** the actual request is made, but **after** | ||
### afterHeaders | ||
Sometimes you'll want to modify the response headers in some way. You can do | ||
### Function inputs | ||
```js | ||
{ | ||
method: 'get', | ||
url: 'https://{{dc}}.api.mailchimp.com/2.0/users?apikey={{apiKey}}', | ||
expects: 200, | ||
afterHeaders: function (error, params, body, res) { | ||
return { | ||
operation: 'cleanup_op', | ||
data: { | ||
abc: '123' | ||
} | ||
}; | ||
// You can also return a promise to do async logic. It should resolve | ||
// with the header object. | ||
} | ||
} | ||
``` | ||
`afterHeaders` must always return an object, else it will be ignored. | ||
The parameters are: | ||
- error - this will be null for a resolving afterSuccess, else it will contain the error | ||
- params | ||
- body | ||
- res | ||
**Note:** if afterHeaders rejects/errors, this error will take precedence over any other error and will form the `body` part of the response. Also, the local `afterHeaders` will merge with the global one if provided, with local object taking precedence. | ||
### addMethod - REST template - response | ||
A method which runs a `REST template` will always return the response in a particular format: | ||
```json | ||
{ | ||
"headers": {}, | ||
"body": {} | ||
} | ||
``` | ||
The `body` will contain the main response or error, while `headers` is available for specifying additional meta data via `afterHeaders`. | ||
### addMethod - function | ||
Sometimes you'll have a method which isn't REST-based, or you'd like to use a third-party wrapper. | ||
@@ -412,2 +456,3 @@ | ||
* [afterFailure](#afterfailure-1) | ||
* [afterHeaders](#afterHeaders-1) | ||
@@ -591,3 +636,22 @@ Example usage: | ||
### afterHeaders | ||
Runs after a method runs successfully, immediately **before** the `afterHeaders` function | ||
of the individual method. | ||
If there is meta data that needs to be specified with every `REST template` method, this is a good place to set it. | ||
```js | ||
{ | ||
afterFailure: function (err, params) { | ||
if (err.response.statusCode === 429) { | ||
err.code = 'call_limit_exceeded'; | ||
} | ||
// You can also return a promise which should resolve having modified the error | ||
} | ||
} | ||
``` | ||
<br/> | ||
@@ -629,2 +693,3 @@ <br/> | ||
- `afterFailure` | ||
- `afterHeaders` | ||
@@ -636,1 +701,4 @@ | ||
- `query` | ||
### addMethod - SOAP template - response | ||
The response format is identical to the REST template response. |
@@ -118,3 +118,3 @@ var assert = require('assert'); | ||
threadneedle[name]().done(function (result) { | ||
assert.equal(result, 'ok'); | ||
assert.equal(result.body, 'ok'); | ||
done(); | ||
@@ -139,3 +139,3 @@ }); | ||
}).done(function (result) { | ||
assert.equal(result, '123'); | ||
assert.equal(result.body, '123'); | ||
done(); | ||
@@ -170,4 +170,4 @@ }); | ||
}).done(function (result) { | ||
assert.deepEqual(result.query, { key: '123' }); | ||
assert.deepEqual(result.body, { name: 'Chris', age: 25 }); | ||
assert.deepEqual(result.body.query, { key: '123' }); | ||
assert.deepEqual(result.body.body, { name: 'Chris', age: 25 }); | ||
done(); | ||
@@ -197,3 +197,3 @@ }); | ||
}).done(function (result) { | ||
assert.equal(result.authorization, 'Basic 123'); | ||
assert.equal(result.body.authorization, 'Basic 123'); | ||
done(); | ||
@@ -223,3 +223,3 @@ }); | ||
}).done(function (result) { | ||
assert.equal(result.authorization, 'Basic Y2hyaXM6aGVsbG8='); | ||
assert.equal(result.body.authorization, 'Basic Y2hyaXM6aGVsbG8='); | ||
done(); | ||
@@ -241,4 +241,4 @@ }); | ||
threadneedle[name]().done(function (result) {}, function (err) { | ||
assert.equal(err.message, 'Invalid response status code'); | ||
threadneedle[name]().done(function (result) {}, function (result) { | ||
assert.equal(result.body.message, 'Invalid response status code'); | ||
done(); | ||
@@ -260,4 +260,4 @@ }); | ||
threadneedle[name]().done(function (result) {}, function (err) { | ||
assert.equal(err.message, 'Invalid response status code'); | ||
threadneedle[name]().done(function (result) {}, function (result) { | ||
assert.equal(result.body.message, 'Invalid response status code'); | ||
done(); | ||
@@ -279,4 +279,4 @@ }); | ||
threadneedle[name]().done(function (result) {}, function (err) { | ||
assert.equal(err.message, 'Invalid response body'); | ||
threadneedle[name]().done(function (result) {}, function (result) { | ||
assert.equal(result.body.message, 'Invalid response body'); | ||
done(); | ||
@@ -299,3 +299,3 @@ }); | ||
threadneedle[name]().done(function (result) { | ||
assert.deepEqual(result, { result: true }); | ||
assert.deepEqual(result.body, { result: true }); | ||
done(); | ||
@@ -317,4 +317,4 @@ }); | ||
threadneedle[name]().done(function() {}, function (err) { | ||
assert.equal(err.message, 'Invalid response status code'); | ||
threadneedle[name]().done(function() {}, function (result) { | ||
assert.equal(result.body.message, 'Invalid response status code'); | ||
done(); | ||
@@ -337,3 +337,3 @@ }); | ||
threadneedle[name]().done(function (result) { | ||
assert.deepEqual(result, { result: true }); | ||
assert.deepEqual(result.body, { result: true }); | ||
done(); | ||
@@ -355,4 +355,4 @@ }); | ||
threadneedle[name]().done(function() {}, function (err) { | ||
assert.equal(err.message, 'Invalid response body'); | ||
threadneedle[name]().done(function() {}, function (result) { | ||
assert.equal(result.body.message, 'Invalid response body'); | ||
done(); | ||
@@ -385,3 +385,3 @@ }); | ||
}).done(function(result) { | ||
assert.deepEqual(result, { | ||
assert.deepEqual(result.body, { | ||
firstName: 'Chris', | ||
@@ -419,3 +419,3 @@ lastName: 'Houghton', | ||
}).done(function(result) { | ||
assert.deepEqual(result, { | ||
assert.deepEqual(result.body, { | ||
firstName: 'Chris', | ||
@@ -429,2 +429,3 @@ lastName: 'Houghton', | ||
it('should run `afterSuccess` on the params synchronously', function (done) { | ||
@@ -451,3 +452,3 @@ var name = randString(10); | ||
}).done(function(result) { | ||
assert.deepEqual(result, { age: 25 }); | ||
assert.deepEqual(result.body, { age: 25 }); | ||
done(); | ||
@@ -480,3 +481,3 @@ }); | ||
}).done(function(result) { | ||
assert.deepEqual(result, { firstName: 'Chris', age: 25 }); | ||
assert.deepEqual(result.body, { firstName: 'Chris', age: 25 }); | ||
done(); | ||
@@ -508,3 +509,3 @@ }); | ||
}).done(function(result) { | ||
assert.deepEqual(result.data, [{ firstName: 'Chris' }]); | ||
assert.deepEqual(result.body.data, [{ firstName: 'Chris' }]); | ||
done(); | ||
@@ -514,2 +515,3 @@ }); | ||
it('should run `afterFailure` on the params synchronously', function (done) { | ||
@@ -536,4 +538,4 @@ var name = randString(10); | ||
firstName: 'Chris' | ||
}).done(function() {}, function (err) { | ||
assert.deepEqual(err, { | ||
}).done(function() {}, function (result) { | ||
assert.deepEqual(result.body, { | ||
message: 'Invalid response status code', | ||
@@ -576,4 +578,4 @@ response: { | ||
firstName: 'Chris' | ||
}).done(function() {}, function (err) { | ||
assert.deepEqual(err, { | ||
}).done(function() {}, function (result) { | ||
assert.deepEqual(result.body, { | ||
message: 'Invalid response status code', | ||
@@ -617,4 +619,4 @@ response: { | ||
firstName: 'Chris' | ||
}).done(function() {}, function (err) { | ||
assert.equal(err.meh, 'no error here'); | ||
}).done(function() {}, function (result) { | ||
assert.equal(result.body.meh, 'no error here'); | ||
done(); | ||
@@ -624,2 +626,295 @@ }); | ||
it('should run `afterHeaders` on the params synchronously', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, { | ||
method: 'post', | ||
url: host + '/' + name, | ||
afterHeaders: function (error, params, body, res) { | ||
return { | ||
metaData: 'ABC' | ||
}; | ||
}, | ||
data: { | ||
firstName: '{{firstName}}' | ||
} | ||
}); | ||
app.post('/'+name, function (req, res) { | ||
res.status(200).json(req.body); | ||
}); | ||
threadneedle[name]({ | ||
firstName: 'Chris' | ||
}).done(function(result) { | ||
assert.deepEqual(result.headers, { metaData: 'ABC' }); | ||
done(); | ||
}); | ||
}); | ||
it('should run `afterHeaders` on the params asynchronously', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, { | ||
method: 'post', | ||
url: host + '/' + name, | ||
afterHeaders: function (error, params, body, res) { | ||
return when.promise(function (resolve) { | ||
resolve({ | ||
metaData: 'XYZ' | ||
}); | ||
}); | ||
}, | ||
data: { | ||
firstName: '{{firstName}}' | ||
} | ||
}); | ||
app.post('/'+name, function (req, res) { | ||
res.status(200).json(req.body); | ||
}); | ||
threadneedle[name]({ | ||
firstName: 'Chris' | ||
}).done(function(result) { | ||
assert.deepEqual(result.headers, { metaData: 'XYZ' }); | ||
done(); | ||
}); | ||
}); | ||
function afterHeaderTestModel (name) { | ||
return { | ||
method: 'post', | ||
url: host + '/' + name, | ||
expects: 200, | ||
afterSuccess: function (body, params, res) { | ||
if (params.asFlag) { | ||
throw new Error('afterSuccess Error'); | ||
} | ||
}, | ||
afterFailure: function (body, params, res) { | ||
if (params.afFlag) { | ||
throw new Error('afterFailure Error'); | ||
} | ||
}, | ||
afterHeaders: function (error, params, body, res) { | ||
if (params.ahFlag) { | ||
throw new Error('afterHeaders Error'); | ||
} else { | ||
return { | ||
gotError: !!error | ||
}; | ||
} | ||
} | ||
}; | ||
}; | ||
it('Should place `afterHeaders` object in header after afterSuccess resolve', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(200).json({ success: true }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: false, | ||
ahFlag: false | ||
}) | ||
.done( | ||
function (result) { | ||
assert.deepEqual(result.headers.gotError, false); | ||
assert.deepEqual(result.body.success, true); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.fail('Wrong clause - failing'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` error in body after afterSuccess resolve', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(200).json({ success: true }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: false, | ||
ahFlag: true | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers, {}); | ||
assert.deepEqual(result.body.message, 'afterHeaders Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` object in header after afterSuccess reject', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(200).json({ success: true }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: true, | ||
afFlag: false, | ||
ahFlag: false | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers.gotError, true); | ||
assert.deepEqual(result.body.message, 'afterSuccess Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` error in body after afterSuccess reject', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(200).json({ success: true }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: true, | ||
afFlag: false, | ||
ahFlag: true | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers, {}); | ||
assert.deepEqual(result.body.message, 'afterHeaders Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` object in header after afterFailure resolve', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(400).json({ success: false }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: false, | ||
ahFlag: false | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers.gotError, true); | ||
assert.deepEqual(result.body.response.body.success, false); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` error in body after afterFailure resolve', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(400).json({ success: false }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: false, | ||
ahFlag: true | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers, {}); | ||
assert.deepEqual(result.body.message, 'afterHeaders Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` object in header after afterFailure reject', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(400).json({ success: false }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: true, | ||
ahFlag: false | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers.gotError, true); | ||
assert.deepEqual(result.body.message, 'afterFailure Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('Should place `afterHeaders` error in body after afterFailure reject', function (done) { | ||
var name = randString(10); | ||
threadneedle.addMethod(name, afterHeaderTestModel(name)); | ||
app.post('/' + name, function (req, res) { | ||
res.status(400).json({ success: false }); | ||
}); | ||
threadneedle[name]({ | ||
asFlag: false, | ||
afFlag: true, | ||
ahFlag: true | ||
}) | ||
.done( | ||
function (result) { | ||
assert.fail('Wrong clause - succeeding'); | ||
done(); | ||
}, | ||
function (result) { | ||
assert.deepEqual(result.headers, {}); | ||
assert.deepEqual(result.body.message, 'afterHeaders Error'); | ||
done(); | ||
} | ||
); | ||
}); | ||
it('should add a {{temp_file}} parameter when `fileHandler` is true', function (done) { | ||
@@ -626,0 +921,0 @@ var name = randString(10); |
@@ -146,3 +146,4 @@ var assert = require('assert'); | ||
.then(function (results) { | ||
assert(results['GetEventsResult']['Success']); | ||
assert.deepEqual(results.headers, {}); | ||
assert(results.body['GetEventsResult']['Success']); | ||
}) | ||
@@ -182,3 +183,3 @@ | ||
.then(function (results) { | ||
assert(results['GetEventsResult']['Success']); | ||
assert(results.body['GetEventsResult']['Success']); | ||
}) | ||
@@ -244,3 +245,4 @@ | ||
.then(function (results) { | ||
assert(results['GetEventsResult']['Success']); | ||
assert.deepEqual(results.headers, {}); | ||
assert(results.body['GetEventsResult']['Success']); | ||
}) | ||
@@ -289,3 +291,4 @@ | ||
function (err) { | ||
assert(err.message === 'Test Error'); | ||
assert.deepEqual(err.headers, {}); | ||
assert(err.body.message === 'Test Error'); | ||
done(); | ||
@@ -333,3 +336,3 @@ } | ||
function (err) { | ||
assert(err.message === 'Test Error'); | ||
assert(err.body.message === 'Test Error'); | ||
done(); | ||
@@ -365,3 +368,3 @@ } | ||
function (val) { | ||
assert(val === 'Chris'); | ||
assert.deepEqual(val.body, 'Chris'); | ||
done(); | ||
@@ -415,2 +418,8 @@ }, | ||
orderBy: 'ID DESC', | ||
}, | ||
afterHeaders: function () { | ||
return { | ||
success: true | ||
} | ||
} | ||
@@ -425,3 +434,4 @@ | ||
function (val) { | ||
assert(val['GetEventsResult']['Success']); | ||
assert.deepEqual(val.headers.success, true); | ||
assert(val.body['GetEventsResult']['Success']); | ||
done(); | ||
@@ -428,0 +438,0 @@ }, |
@@ -9,805 +9,926 @@ var assert = require('assert'); | ||
describe('#url', function () { | ||
describe('#url', function () { | ||
it('should add the global url on the front unless it starts with http(s)://', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}; | ||
it('should add the global url on the front unless it starts with http(s)://', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}; | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: 'http://yourdomain.com/mypath' }, {}), | ||
'http://yourdomain.com/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: 'http://yourdomain.com/mypath' }, {}), | ||
'http://yourdomain.com/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: 'https://yourdomain.com/mypath' }, {}), | ||
'https://yourdomain.com/mypath' | ||
); | ||
}); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: 'https://yourdomain.com/mypath' }, {}), | ||
'https://yourdomain.com/mypath' | ||
); | ||
}); | ||
it('should substitute parameters to string urls', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://{{dc}}.mydomain.com' | ||
} | ||
}; | ||
it('should substitute parameters to string urls', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://{{dc}}.mydomain.com' | ||
} | ||
}; | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath/{{id}}' }, { | ||
dc: 'us5', | ||
id: '123' | ||
}), | ||
'http://us5.mydomain.com/mypath/123' | ||
); | ||
}); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath/{{id}}' }, { | ||
dc: 'us5', | ||
id: '123' | ||
}), | ||
'http://us5.mydomain.com/mypath/123' | ||
); | ||
}); | ||
it('should substitute array parameters as comma separated', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://{{dc}}.mydomain.com' | ||
} | ||
}; | ||
it('should substitute array parameters as comma separated', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://{{dc}}.mydomain.com' | ||
} | ||
}; | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath/{{id}}?opt_fields={{fields}}' }, { | ||
dc: 'us5', | ||
id: '123', | ||
fields: ['id', 'name', 'is_organization'] | ||
}), | ||
'http://us5.mydomain.com/mypath/123?opt_fields=id,name,is_organization' | ||
); | ||
}); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath/{{id}}?opt_fields={{fields}}' }, { | ||
dc: 'us5', | ||
id: '123', | ||
fields: ['id', 'name', 'is_organization'] | ||
}), | ||
'http://us5.mydomain.com/mypath/123?opt_fields=id,name,is_organization' | ||
); | ||
}); | ||
it('should substitute parameters to function urls', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: function (params) { | ||
return 'http://'+params.dc+'.mydomain.com'; | ||
} | ||
} | ||
}; | ||
it('should substitute parameters to function urls', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: function (params) { | ||
return 'http://'+params.dc+'.mydomain.com'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { | ||
url: function(params) { | ||
return '/mypath/' + params.id; | ||
} | ||
}, { | ||
dc: 'us5', | ||
id: '123' | ||
}), | ||
'http://us5.mydomain.com/mypath/123' | ||
); | ||
}); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { | ||
url: function(params) { | ||
return '/mypath/' + params.id; | ||
} | ||
}, { | ||
dc: 'us5', | ||
id: '123' | ||
}), | ||
'http://us5.mydomain.com/mypath/123' | ||
); | ||
}); | ||
it('should not run the global when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}; | ||
it('should not run the global when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}; | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath', globals: false }, {}), | ||
'/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath', globals: false }, {}), | ||
'/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath', globals: { baseUrl: false } }, {}), | ||
'/mypath' | ||
); | ||
assert.strictEqual( | ||
globalize.baseUrl.call(sample, { url: '/mypath', globals: { baseUrl: false } }, {}), | ||
'/mypath' | ||
); | ||
}); | ||
}); | ||
}); | ||
describe('#object', function () { | ||
describe('#object', function () { | ||
it('should globalize to an object on a shallow level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
it('should globalize to an object on a shallow level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
id: '123', | ||
name: 'Chris', | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
}); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
id: '123', | ||
name: 'Chris', | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
}); | ||
it('should globalize to an object on a deep level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris', | ||
height: { | ||
m: 1.9 | ||
} | ||
} | ||
} | ||
}; | ||
it('should globalize to an object on a deep level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris', | ||
height: { | ||
m: 1.9 | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
age: 25, | ||
height: { | ||
cm: 180, | ||
m: 1.8 | ||
} | ||
} | ||
}, {}), { | ||
id: '123', | ||
name: 'Chris', | ||
age: 25, | ||
height: { | ||
cm: 180, | ||
m: 1.8 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
); | ||
}); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
age: 25, | ||
height: { | ||
cm: 180, | ||
m: 1.8 | ||
it('should substitute to an global object on a deep level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
firstName: '{{firstName}}', | ||
lastName: '{{lastName}}' | ||
} | ||
} | ||
} | ||
}, {}), { | ||
id: '123', | ||
name: 'Chris', | ||
age: 25, | ||
height: { | ||
cm: 180, | ||
m: 1.8 | ||
} | ||
} | ||
); | ||
}); | ||
}; | ||
it('should substitute to an global object on a deep level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
firstName: '{{firstName}}', | ||
lastName: '{{lastName}}' | ||
} | ||
} | ||
}; | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
name: '{{name}}' | ||
} | ||
}, { | ||
name: 'Chris Houghton', | ||
firstName: 'Chris', | ||
lastName: 'Houghton' | ||
}), { | ||
id: '123', | ||
name: 'Chris Houghton', | ||
firstName: 'Chris', | ||
lastName: 'Houghton' | ||
} | ||
); | ||
}); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
data: { | ||
name: '{{name}}' | ||
} | ||
}, { | ||
name: 'Chris Houghton', | ||
firstName: 'Chris', | ||
lastName: 'Houghton' | ||
}), { | ||
id: '123', | ||
name: 'Chris Houghton', | ||
firstName: 'Chris', | ||
lastName: 'Houghton' | ||
} | ||
); | ||
}); | ||
it('should return local string if data is a string', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
it('should return local string if data is a string', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: false, | ||
data: "Lorem ipsum" | ||
}, {}), | ||
"Lorem ipsum" | ||
); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: false, | ||
data: "Lorem ipsum" | ||
}, {}), | ||
"Lorem ipsum" | ||
); | ||
}); | ||
}); | ||
it('should not globalize when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
it('should not globalize when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
data: { | ||
id: '123', | ||
name: 'Chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: false, | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: false, | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: { | ||
data: false | ||
}, | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
}); | ||
assert.deepEqual( | ||
globalize.object.call(sample, 'data', { | ||
globals: { | ||
data: false | ||
}, | ||
data: { | ||
age: 25, | ||
height: 180 | ||
} | ||
}, {}), { | ||
age: 25, | ||
height: 180 | ||
} | ||
); | ||
}); | ||
}); | ||
describe('#before', function () { | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
params.dc = 'us5'; | ||
} | ||
} | ||
}; | ||
describe('#before', function () { | ||
globalize.before.call(sample, {}, { | ||
url: '/mydomain' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' }); | ||
done(); | ||
}); | ||
}); | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
params.dc = 'us5'; | ||
} | ||
} | ||
}; | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
return when.promise(function (resolve, reject) { | ||
params.dc = 'us5'; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
globalize.before.call(sample, {}, { | ||
url: '/mydomain' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' }); | ||
done(); | ||
}); | ||
}); | ||
globalize.before.call(sample, {}, { | ||
url: '/mydomain' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' }); | ||
done(); | ||
}); | ||
}); | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
return when.promise(function (resolve, reject) { | ||
params.dc = 'us5'; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
globalize.before.call(sample, {}, { | ||
url: '/mydomain' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' }); | ||
done(); | ||
}); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
globalize.before.call(sample, { | ||
before: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function(params) { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
it('should not run global before when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
params.dc = 'us5'; | ||
} | ||
} | ||
}; | ||
globalize.before.call(sample, { | ||
before: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function(params) { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
globalize.before.call(sample, { | ||
globals: false, | ||
url: '/mydomain/{{id}}' | ||
}, { | ||
id: '123' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { id: '123' }); | ||
}); | ||
it('should not run global before when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
before: function (params) { | ||
params.dc = 'us5'; | ||
} | ||
} | ||
}; | ||
globalize.before.call(sample, { | ||
globals: { | ||
before: false | ||
}, | ||
url: '/mydomain/{{id}}' | ||
}, { | ||
id: '123' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { id: '123' }); | ||
done(); | ||
}); | ||
}); | ||
globalize.before.call(sample, { | ||
globals: false, | ||
url: '/mydomain/{{id}}' | ||
}, { | ||
id: '123' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { id: '123' }); | ||
}); | ||
it('should use baseUrl rather than url, but still fall back to url', function () { | ||
assert.strictEqual( | ||
globalize.baseUrl.call({ | ||
_globalOptions: { | ||
baseUrl: 'http://mydomain.com' | ||
} | ||
}, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
globalize.before.call(sample, { | ||
globals: { | ||
before: false | ||
}, | ||
url: '/mydomain/{{id}}' | ||
}, { | ||
id: '123' | ||
}).done(function (params) { | ||
assert.deepEqual(params, { id: '123' }); | ||
done(); | ||
}); | ||
assert.strictEqual( | ||
globalize.baseUrl.call({ | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
}); | ||
}); | ||
it('should use baseUrl rather than url, but still fall back to url', function () { | ||
assert.strictEqual( | ||
globalize.baseUrl.call({ | ||
_globalOptions: { | ||
baseUrl: 'http://mydomain.com' | ||
} | ||
}, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
describe.skip('#beforeRequest', function () { | ||
assert.strictEqual( | ||
globalize.baseUrl.call({ | ||
_globalOptions: { | ||
url: 'http://mydomain.com' | ||
} | ||
}, { url: '/mypath' }, {}), | ||
'http://mydomain.com/mypath' | ||
); | ||
}); | ||
}); | ||
describe.skip('#beforeRequest', function () { | ||
describe('#expects', function () { | ||
it('should set the expects object when specified in global', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ | ||
statusCode: [200, 201], | ||
body: ['chris'] | ||
}]); | ||
}); | ||
it('should be overridden by the local config', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
expects: { | ||
statusCode: 201 | ||
} | ||
}), [{ | ||
statusCode: [201] | ||
}]); | ||
describe('#expects', function () { | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
expects: 202 | ||
}), [{ | ||
statusCode: [202] | ||
}]); | ||
}); | ||
it('should set the expects object when specified in global', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
it('should not merge when there are functions on the global or local level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: { | ||
body: 'steve' | ||
} | ||
}).length, 2); | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ | ||
statusCode: [200, 201], | ||
body: ['chris'] | ||
}]); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
expects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
it('should be overridden by the local config', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
expects: { | ||
statusCode: 201 | ||
} | ||
}), [{ | ||
statusCode: [201] | ||
}]); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: [200] | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
}); | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
expects: 202 | ||
}), [{ | ||
statusCode: [202] | ||
}]); | ||
}); | ||
it('should not run global when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
it('should not merge when there are functions on the global or local level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: { | ||
body: 'steve' | ||
} | ||
}).length, 2); | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
globals: false | ||
}), [{}]); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
expects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
it('should not run global when globals.expects is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: [200] | ||
} | ||
}; | ||
assert.strictEqual(globalize.expects.call(sample, { | ||
expects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
globals: { | ||
expects: false | ||
} | ||
}), [{}]); | ||
}); | ||
}); | ||
it('should not run global when globals is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
describe('#notExpects', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
it('should set the expects object when specified in global', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, {}), [{ statusCode: [200] }]); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, {}), [{ | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
globals: false | ||
}), [{}]); | ||
}); | ||
body: ['chris'] | ||
}]); | ||
}); | ||
it('should not run global when globals.expects is false', function () { | ||
var sample = { | ||
_globalOptions: { | ||
expects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, {}), [{ statusCode: [200] }]); | ||
it('should be overridden by the local config', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
statusCode: 201 | ||
} | ||
}), [{ | ||
statusCode: [201] | ||
}]); | ||
var sample = { | ||
_globalOptions: { | ||
expects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.expects.call(sample, { | ||
globals: { | ||
expects: false | ||
} | ||
}), [{}]); | ||
}); | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: 202 | ||
}), [{ | ||
statusCode: [202] | ||
}]); | ||
}); | ||
}); | ||
describe('#notExpects', function () { | ||
it('should not set the notExpects object when false is specified in globals', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
it('should set the expects object when specified in global', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, {}), [{ statusCode: [200] }]); | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
}, | ||
globals: false | ||
}), [{ | ||
body: ['steve'] | ||
}]); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, {}), [{ | ||
statusCode: [200, 201], | ||
body: ['chris'] | ||
}]); | ||
}); | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
}, | ||
globals: { | ||
notExpects: false | ||
} | ||
}), [{ | ||
body: ['steve'] | ||
}]); | ||
}); | ||
it('should be overridden by the local config', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: 200 | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
statusCode: 201 | ||
} | ||
}), [{ | ||
statusCode: [201] | ||
}]); | ||
it('should not merge when there are functions on the global or local level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
} | ||
}).length, 2); | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: 202 | ||
}), [{ | ||
statusCode: [202] | ||
}]); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: [200] | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
}); | ||
}); | ||
it('should not set the notExpects object when false is specified in globals', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: { | ||
statusCode: [200, 201], | ||
body: 'chris' | ||
} | ||
} | ||
}; | ||
describe('#afterSuccess', function () { | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
}, | ||
globals: false | ||
}), [{ | ||
body: ['steve'] | ||
}]); | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
body.success = true; | ||
} | ||
} | ||
}; | ||
assert.deepEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
}, | ||
globals: { | ||
notExpects: false | ||
} | ||
}), [{ | ||
body: ['steve'] | ||
}]); | ||
}); | ||
globalize.afterSuccess.call(sample, {}, {}).done(function (body) { | ||
assert.deepEqual(body, { success: true }); | ||
done(); | ||
}); | ||
}); | ||
it('should not merge when there are functions on the global or local level', function () { | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: { | ||
body: 'steve' | ||
} | ||
}).length, 2); | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
return when.promise(function (resolve, reject) { | ||
body.success = true; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: function () { | ||
return 'Bad things'; | ||
} | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
globalize.afterSuccess.call(sample, {}, {}).done(function (body) { | ||
assert.deepEqual(body, { success: true }); | ||
done(); | ||
}); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
notExpects: [200] | ||
} | ||
}; | ||
assert.strictEqual(globalize.notExpects.call(sample, { | ||
notExpects: function () { | ||
return 'Locally bad things'; | ||
} | ||
}).length, 2); | ||
}); | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (params) { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
globalize.afterSuccess.call(sample, { | ||
afterSuccess: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function(params) { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
describe('#afterSuccess', function () { | ||
it('should not run the globals when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
body.success = true; | ||
} | ||
} | ||
}; | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
body.success = true; | ||
} | ||
} | ||
}; | ||
globalize.afterSuccess.call(sample, { | ||
globals: false | ||
}, {}).done(function (body) { | ||
assert.deepEqual(body, {}); | ||
//done(); | ||
}); | ||
globalize.afterSuccess.call(sample, {}, {}).done(function (body) { | ||
assert.deepEqual(body, { success: true }); | ||
done(); | ||
}); | ||
globalize.afterSuccess.call(sample, { | ||
globals: { | ||
afterSuccess: false | ||
} | ||
}, {}).done(function (body) { | ||
assert.deepEqual(body, {}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
return when.promise(function (resolve, reject) { | ||
body.success = true; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
describe('#afterFailure', function () { | ||
globalize.afterSuccess.call(sample, {}, {}).done(function (body) { | ||
assert.deepEqual(body, { success: true }); | ||
done(); | ||
}); | ||
}); | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
err.code = 'oauth_refresh'; | ||
} | ||
} | ||
}; | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
globalize.afterFailure.call(sample, {}, {}).done(function (err) { | ||
assert.deepEqual(err, { code: 'oauth_refresh' }); | ||
done(); | ||
}); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (params) { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
return when.promise(function (resolve, reject) { | ||
err.code = 'oauth_refresh'; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
globalize.afterSuccess.call(sample, { | ||
afterSuccess: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function(params) { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
globalize.afterFailure.call(sample, {}, {}).done(function (err) { | ||
assert.deepEqual(err, { code: 'oauth_refresh' }); | ||
done(); | ||
}); | ||
}); | ||
it('should not run the globals when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterSuccess: function (body) { | ||
body.success = true; | ||
} | ||
} | ||
}; | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
globalize.afterSuccess.call(sample, { | ||
globals: false | ||
}, {}).done(function (body) { | ||
assert.deepEqual(body, {}); | ||
//done(); | ||
}); | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function () { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
globalize.afterSuccess.call(sample, { | ||
globals: { | ||
afterSuccess: false | ||
} | ||
}, {}).done(function (body) { | ||
assert.deepEqual(body, {}); | ||
done(); | ||
}); | ||
}); | ||
globalize.afterFailure.call(sample, { | ||
afterFailure: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function() { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should not run the global when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
err.code = 'oauth_refresh'; | ||
} | ||
} | ||
}; | ||
describe('#afterFailure', function () { | ||
globalize.afterFailure.call(sample, { | ||
globals: false | ||
}, {}).done(function (err) { | ||
assert.deepEqual(err, {}); | ||
//done(); | ||
}); | ||
it('should run the global before method when declared', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
err.code = 'oauth_refresh'; | ||
} | ||
} | ||
}; | ||
globalize.afterFailure.call(sample, { | ||
globals: { | ||
afterFailure: false | ||
} | ||
}, {}).done(function (err) { | ||
assert.deepEqual(err, {}); | ||
done(); | ||
}); | ||
}); | ||
globalize.afterFailure.call(sample, {}, {}).done(function (err) { | ||
assert.deepEqual(err, { code: 'oauth_refresh' }); | ||
done(); | ||
}); | ||
}); | ||
it('should allow for a global promise async', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
return when.promise(function (resolve, reject) { | ||
err.code = 'oauth_refresh'; | ||
resolve(); | ||
}); | ||
} | ||
} | ||
}; | ||
globalize.afterFailure.call(sample, {}, {}).done(function (err) { | ||
assert.deepEqual(err, { code: 'oauth_refresh' }); | ||
done(); | ||
}); | ||
}); | ||
describe('#afterHeaders', function () { | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
it('should run the global before method when declared', function (done) { | ||
var sampleThread = { | ||
_globalOptions: { | ||
afterHeaders: function (error, params, body, res) { | ||
return { | ||
success: true | ||
}; | ||
} | ||
} | ||
}; | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function () { | ||
if (!calledFirst) calledFirst = 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
globalize.afterHeaders.call(sampleThread, {}, null, {}, {}, {}).done(function (header) { | ||
assert.deepEqual(header, { | ||
success: true | ||
}); | ||
done(); | ||
}); | ||
}); | ||
globalize.afterFailure.call(sample, { | ||
afterFailure: function() { | ||
if (!calledFirst) calledFirst = 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function() { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
it('should allow for a global promise async', function (done) { | ||
var sampleThread = { | ||
_globalOptions: { | ||
afterHeaders: function (error, params, body, res) { | ||
return when.promise(function (resolve, reject) { | ||
resolve({ | ||
success: true | ||
}); | ||
}); | ||
} | ||
} | ||
}; | ||
it('should not run the global when globals is false', function (done) { | ||
var sample = { | ||
_globalOptions: { | ||
afterFailure: function (err) { | ||
err.code = 'oauth_refresh'; | ||
} | ||
} | ||
}; | ||
globalize.afterHeaders.call(sampleThread, {}, {}).done(function (header) { | ||
assert.deepEqual(header, { | ||
success: true | ||
}); | ||
done(); | ||
}); | ||
}); | ||
globalize.afterFailure.call(sample, { | ||
globals: false | ||
}, {}).done(function (err) { | ||
assert.deepEqual(err, {}); | ||
//done(); | ||
}); | ||
it('should call the global promise before the local one', function (done) { | ||
var calledFirst; | ||
var calls = 0; | ||
globalize.afterFailure.call(sample, { | ||
globals: { | ||
afterFailure: false | ||
} | ||
}, {}).done(function (err) { | ||
assert.deepEqual(err, {}); | ||
done(); | ||
}); | ||
}); | ||
var sampleThread = { | ||
_globalOptions: { | ||
afterHeaders: function (error, params, body, res) { | ||
calledFirst = calledFirst || 'global'; | ||
calls++; | ||
} | ||
} | ||
}; | ||
globalize.afterHeaders.call(sampleThread, { | ||
afterHeaders: function () { | ||
calledFirst = calledFirst || 'local'; | ||
calls++ | ||
} | ||
}, {}).done(function (params) { | ||
assert.equal(calledFirst, 'global'); | ||
assert.equal(calls, 2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should make local take precedence over global via defaultsDeep', function (done) { | ||
var sampleThread = { | ||
_globalOptions: { | ||
afterHeaders: function (error, params, body, res) { | ||
return { | ||
test: 123 | ||
}; | ||
} | ||
} | ||
}; | ||
globalize.afterHeaders.call(sampleThread, { | ||
afterHeaders: function () { | ||
return { | ||
test: 456 | ||
}; | ||
} | ||
}, {}).done(function (headers) { | ||
assert.equal(headers.test, 456); | ||
done(); | ||
}); | ||
}); | ||
it('should not run the globals when globals is false', function (done) { | ||
var sampleThread = { | ||
_globalOptions: { | ||
afterHeaders: function (error, params, body, res) { | ||
return { | ||
success: true | ||
}; | ||
} | ||
} | ||
}; | ||
globalize.afterHeaders.call(sampleThread, { | ||
globals: false | ||
}, {}).done(function (headers) { | ||
assert.deepEqual(headers, {}); | ||
//done(); | ||
}); | ||
globalize.afterHeaders.call(sampleThread, { | ||
globals: { | ||
afterHeaders: false | ||
} | ||
}, {}).done(function (headers) { | ||
assert.deepEqual(headers, {}); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
149255
46
3703
696