oniyi-http-plugin-format-url-template
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -8,2 +8,3 @@ 'use strict'; | ||
const _ = require('lodash'); | ||
const debug = require('debug')('oniyi:http-client:plugin:format-url-template'); | ||
@@ -13,2 +14,4 @@ // internal modules | ||
const PHASE_NAME = 'format-url-template'; | ||
const defaultPluginOptions = { | ||
@@ -29,11 +32,12 @@ applyToUrl: true, | ||
const { uri } = requestOptions; | ||
if (!applyToUrl || !uri) { | ||
if (!(applyToUrl && uri)) { | ||
return uri; | ||
} | ||
const { href } = uri; | ||
const formattedHref = formatHref(href, requestOptions, valuesMap); | ||
// provided uri can be either String or an Object | ||
const isUriString = _.isString(uri); | ||
// automatically escapes characters for us (https://nodejs.org/dist/latest-v6.x/docs/api/url.html#url_escaped_characters) | ||
return url.parse(formattedHref); | ||
const formattedHref = formatHref(isUriString ? uri : uri.href, requestOptions, valuesMap); | ||
return isUriString ? formattedHref : url.parse(formattedHref); | ||
}; | ||
@@ -70,30 +74,29 @@ | ||
const pluginOptions = _.merge({}, defaultPluginOptions, params); | ||
debug('Plugin options compiled: %o', pluginOptions); | ||
/** | ||
* the actual plugin execution (runs per-request) | ||
* | ||
* @method load | ||
* @param {Object} req HTTP request object | ||
* @param {Object} requestOptions options for executing HTTP request with oniyi-http-client | ||
* @param {Function} callback [description] | ||
* @example | ||
* // simplified `req` and `requestOptions.uri` args | ||
* load(null, { uri: '/api/foo/{ authType }/bar', authType: 'oauth' }, (err, requestOptions) => { | ||
* console.log(requestOptions.uri); // '/api/foo/oauth/bar' | ||
* }); | ||
*/ | ||
const load = (req, requestOptions, callback) => { | ||
const { plugins: { formatUrlTemplate: pluginOptionsParam = {} } = {} } = requestOptions; | ||
const options = _.merge({}, pluginOptions, pluginOptionsParam); | ||
return { | ||
name: PHASE_NAME, | ||
onRequest: [{ | ||
phaseName: PHASE_NAME, | ||
handler: (ctx, next) => { | ||
const { options: requestOptions } = ctx; | ||
const { | ||
plugins: { | ||
formatUrlTemplate, | ||
} = {}, | ||
} = requestOptions; | ||
const options = _.merge({}, pluginOptions, formatUrlTemplate); | ||
const uri = processUri(requestOptions, options); | ||
const qs = processQuerystring(requestOptions, options); | ||
const uri = processUri(requestOptions, options); | ||
debug('request options uri updated: %o', uri); | ||
const qs = processQuerystring(requestOptions, options); | ||
debug('request options qs updated: %o', qs); | ||
callback(null, Object.assign({}, requestOptions, { uri, qs })); | ||
}; | ||
// update the context with latest changes of request options | ||
_.assign(ctx, { options: _.assign({}, requestOptions, { uri, qs }) }); | ||
return { | ||
name: 'format-url-template', | ||
load, | ||
next(); | ||
}, | ||
}], | ||
}; | ||
@@ -100,0 +103,0 @@ }; |
{ | ||
"name": "oniyi-http-plugin-format-url-template", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Plugin for injecting template values into request href and/or query string", | ||
@@ -26,12 +26,13 @@ "license": "MIT", | ||
"dependencies": { | ||
"lodash": "4.17.4" | ||
"debug": "^3.1.0", | ||
"lodash": "^4.17.5" | ||
}, | ||
"devDependencies": { | ||
"ava": "0.20.0", | ||
"babel-eslint": "7.2.3", | ||
"eslint": "3.15.0", | ||
"eslint-config-oniyi": "4.4.0", | ||
"eslint-plugin-ava": "4.2.1", | ||
"jsdoc-to-markdown": "3.0.0", | ||
"nyc": "10.3.2" | ||
"ava": "^0.25.0", | ||
"babel-eslint": "^8.2.1", | ||
"eslint": "^4.17.0", | ||
"eslint-config-oniyi": "^5.1.0", | ||
"eslint-plugin-ava": "^4.5.0", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"nyc": "^11.4.1" | ||
}, | ||
@@ -38,0 +39,0 @@ "homepage": "https://github.com/benkroeger/oniyi-http-plugin-format-url-template#readme", |
@@ -15,4 +15,5 @@ # oniyi-http-plugin-format-url-template | ||
const clientOptions = {}; | ||
const httpClient = new OniyiHttpClient(clientOptions); | ||
const options = { | ||
requestPhases: ['initial','format-url-template', 'final'], | ||
}; | ||
@@ -28,3 +29,15 @@ const pluginOptions = { | ||
httpClient.use(oniyiHttpPluginUrlTemplate(pluginOptions)); | ||
const plugin = oniyiHttpPluginUrlTemplate(pluginOptions); | ||
const phaseMapOptions = { | ||
requestPhaseMap: { | ||
'format-url-template': 'url-template', | ||
}, | ||
responsePhaseMap: { | ||
final: 'end', | ||
}, | ||
}; | ||
const httpClient = OniyiHttpClient | ||
.create(options) // create custom http client with defined phases lists | ||
.use(plugin, phaseMapOptions); // mount a plugin | ||
``` | ||
@@ -48,2 +61,3 @@ | ||
``` | ||
This plugin relies on logic implemented in [oniyi-http-client](https://npmjs.org/package/oniyi-http-client), which has extensive documentation on how phase lists work and what conventions must be followed when implementing a plugin. | ||
@@ -103,3 +117,3 @@ ## Conventions | ||
httpClient.use(oniyiHttpPluginAuthType(pluginOptions)); | ||
httpClient.use(oniyiHttpPluginUrlTemplate(pluginOptions)); | ||
``` | ||
@@ -146,3 +160,3 @@ | ||
It is recommended to use this plugin in combination with `oniyi-http-plugin-credentials`, since it resolves `authType` | ||
It is recommended to use this plugin in combination with [oniyi-http-plugin-credentials](https://npmjs.org/package/oniyi-http-plugin-credentials), since it resolves `authType` | ||
which is being used by this plugin. | ||
@@ -154,3 +168,3 @@ | ||
authType: 'basic', | ||
uri: 'my/custom/{ wrongAuthType }/path', | ||
uri: 'my/custom/{ authType }/path', | ||
//... | ||
@@ -160,6 +174,32 @@ } | ||
If `conventions 1 && 2` have not been implemented properly, plugin will leave `url` object with _encoded_ special | ||
characters / tags as follows: | ||
-- **convention 5** -- | ||
Please provide uri parameter as a String or Url object: | ||
```js | ||
const uriAsString = 'my/custom/authType }/path'; | ||
const parsedUri = { | ||
auth: null, | ||
hash: null, | ||
host:'host.com', | ||
hostname: 'host.com', | ||
href:'host.com/my/custom/{ authType/path', | ||
path:'/my/custom/path', | ||
pathname:'/my/custom/path', | ||
port: null, | ||
protocol:'https:', | ||
query: null, | ||
search: null, | ||
slashes: true, | ||
} | ||
``` | ||
If `conventions 1 && 2` have not been implemented properly, response uri will look like this: | ||
``` | ||
// uri as string request | ||
{ | ||
uri: 'my/custom/authType }/path', | ||
} | ||
// uri as parsed object | ||
{ | ||
uri: { | ||
@@ -170,5 +210,5 @@ auth: null, | ||
hostname: 'host.com', | ||
href:'host.com/my/custom/%7B%20wrongAuthType%20%7D/path', | ||
path:'/my/custom/%7B%20wrongAuthType%20%7D/path', | ||
pathname:'/my/custom/%7B%20wrongAuthType%20%7D/path', | ||
href:'host.com/my/custom/%7B%20authType/path', | ||
path:'/my/custom/%7B%20authType/path', | ||
pathname:'/my/custom/%7B%20authType/path', | ||
port: null, | ||
@@ -181,3 +221,6 @@ protocol:'https:', | ||
} | ||
``` | ||
The reason why `uriAsString` response is not encoded as `parsedUri` is because plugin takes `href` prop from `parsedUri`, | ||
uses `url.parse(href);` which encodes all special characters that are used in the href path. | ||
@@ -197,16 +240,3 @@ This way plugin is notifying service that something went wrong while setting up, most likely because of a typo. | ||
const httpResponse = { | ||
uri: { | ||
auth: null, | ||
hash: null, | ||
host:'host.com', | ||
hostname: 'host.com', | ||
href:'host.com/my/custom/path', | ||
path:'/my/custom/path', | ||
pathname:'/my/custom/path', | ||
port: null, | ||
protocol:'https:', | ||
query: null, | ||
search: null, | ||
slashes: true, | ||
}, | ||
uri: 'my/custom/path', | ||
} | ||
@@ -229,2 +259,2 @@ ``` | ||
This way it should be relatively simple injecting any parameter into service `uri / qs` path, in order to load the same | ||
service by different auth providers (ibm-connections-cloud, microsoft...) by using custom template mapping. | ||
service with different auth providers (ibm-connections-cloud, microsoft...) by using custom template mapping. |
@@ -25,10 +25,9 @@ 'use strict'; | ||
const initPlugin = formatUrlTemplate(); | ||
const { load } = initPlugin; | ||
const { onRequest } = initPlugin; | ||
function getValues(obj) { | ||
return Object.keys(obj).map(key => obj[key]); | ||
} | ||
const phaseHook = onRequest[0]; | ||
// uri variables | ||
const uri = url.parse('https://apps.na.collabserv.com/files/{ authType }/api/feed'); | ||
const uriAsString = 'https://apps.na.collabserv.com/files/{ authType }/api/feed'; | ||
const uriTemplateCombination = url.parse('https://apps.na.collabserv.com/files/{ authType }/api/{ mockParam }/feed'); | ||
@@ -53,6 +52,6 @@ | ||
_.assign(t.context, { | ||
load, | ||
getValues, | ||
phaseHook, | ||
valuesMap, | ||
uri, | ||
uriAsString, | ||
uriTemplateCombination, | ||
@@ -68,16 +67,27 @@ noAuthTypeTemplate, | ||
/* Metadata test */ | ||
test('validate {formatUrlTemplate} values and type', (t) => { | ||
test('main module return `name` and `onRequest` props', (t) => { | ||
t.true(_.isObject(formatUrlTemplate())); | ||
const { name, load } = formatUrlTemplate(); | ||
const { name, onRequest } = formatUrlTemplate(); | ||
t.true(_.isString(name)); | ||
t.true(_.isFunction(load)); | ||
t.true(_.isArray(onRequest)); | ||
t.is(name, 'format-url-template'); | ||
t.is(load.length, 3, 'there should be 3 arguments in {load} function'); | ||
t.is(onRequest.length, 1); | ||
}); | ||
test('`onRequest` return `phaseName` and `handler` prop', (t) => { | ||
t.true(_.isObject(formatUrlTemplate())); | ||
const { onRequest } = formatUrlTemplate(); | ||
const formatUrlTemplatePhaseHook = onRequest[0]; | ||
const { phaseName, handler } = formatUrlTemplatePhaseHook; | ||
t.is(phaseName, 'format-url-template'); | ||
t.true(_.isFunction(handler)); | ||
t.is(handler.length, 2); | ||
}); | ||
/* Successful scenarios validations */ | ||
test.cb('validation when { authType } is provided, authType: "basic"', (t) => { | ||
const { load, uri } = t.context; | ||
const { phaseHook, uri } = t.context; | ||
const requestOptions = { | ||
@@ -87,7 +97,14 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
Object.keys(requestOptions).forEach(elem => t.true(elem in modifiedParams, `${elem} should be a member of modifiedParams`)); | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
@@ -103,4 +120,6 @@ t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
const { uri, valuesMap, multipleQs } = t.context; | ||
const { load } = formatUrlTemplate({ valuesMap }); | ||
const { onRequest } = formatUrlTemplate({ valuesMap }); | ||
const phaseHook = onRequest[0]; | ||
const requestOptions = { | ||
@@ -119,10 +138,16 @@ uri, | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const { getValues } = t.context; | ||
const { qs } = modifiedParams; | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
getValues(multipleQs).forEach((templateValue) => { | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { qs } = ctx.options; | ||
_.values(multipleQs).forEach((templateValue) => { | ||
const [, , value] = templateValue.match(regex); | ||
const mappedValue = requestOptions[value]; | ||
t.true(Object.keys(qs).some(key => qs[key] === mappedValue), `{${mappedValue}} should be one of the values of {modifiedParams}`); | ||
t.true(Object.keys(qs).some(key => qs[key] === mappedValue), | ||
`{${mappedValue}} should be one of the values of {modifiedParams}`); | ||
}); | ||
@@ -136,4 +161,6 @@ | ||
const { uri, valuesMap } = t.context; | ||
const { load } = formatUrlTemplate({ valuesMap }); | ||
const { onRequest } = formatUrlTemplate({ valuesMap }); | ||
const phaseHook = onRequest[0]; | ||
const requestOptions = { | ||
@@ -144,11 +171,19 @@ uri, | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath }, authType } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
Object.keys(requestOptions).forEach(elem => t.true(elem in modifiedParams, `${elem} should be a member of modifiedParams`)); | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(valuesMap.authType[authType]), `${valuesMap.authType[authType]} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(valuesMap.authType[authType]), | ||
`${valuesMap.authType[authType]} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.end(); | ||
@@ -158,4 +193,37 @@ }); | ||
test.cb('validation when {authType} is provided, authType: "basic", uri provided as a String', (t) => { | ||
const { uriAsString, valuesMap } = t.context; | ||
const { onRequest } = formatUrlTemplate({ valuesMap }); | ||
const phaseHook = onRequest[0]; | ||
const requestOptions = { | ||
uri: uriAsString, | ||
authType: 'basic', | ||
}; | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: originalPath, authType } = requestOptions; | ||
const { uri: modifiedPath } = ctx.options; | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(valuesMap.authType[authType]), | ||
`${valuesMap.authType[authType]} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.end(); | ||
}); | ||
}); | ||
test.cb('validation when {authType} && {mockParam} are provided, authType: "oauth"', (t) => { | ||
const { load, uriTemplateCombination } = t.context; | ||
const { phaseHook, uriTemplateCombination } = t.context; | ||
@@ -168,12 +236,20 @@ const requestOptions = { | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath }, authType, mockParam } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
Object.keys(requestOptions).forEach(elem => t.true(elem in modifiedParams, `${elem} should be a member of modifiedParams`)); | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(authType), `${authType} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(mockParam), `${mockParam} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(authType), | ||
`${authType} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.true(modifiedPath.includes(mockParam), | ||
`${mockParam} should be part of modified path. provided: {${modifiedPath}}`); | ||
t.end(); | ||
@@ -184,3 +260,3 @@ }); | ||
test.cb('validation when "applyToUrl" is set to false, "applyToQueryString" is set to true', (t) => { | ||
const { load, uri, qs: qsOriginal } = t.context; | ||
const { phaseHook, uri, qs: qsOriginal } = t.context; | ||
const requestOptions = { | ||
@@ -198,5 +274,12 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath }, mockParam } = requestOptions; | ||
const { uri: { href: modifiedPath }, qs } = modifiedParams; | ||
const { uri: { href: modifiedPath }, qs } = ctx.options; | ||
@@ -212,5 +295,6 @@ t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. | ||
const { uri } = t.context; | ||
const { load } = formatUrlTemplate({ | ||
applyToUrl: false, | ||
}); | ||
const { onRequest } = formatUrlTemplate({ applyToUrl: false }); | ||
const phaseHook = onRequest[0]; | ||
const requestOptions = { | ||
@@ -220,9 +304,17 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath }, authType } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.not(modifiedPath.includes(authType), `{authType} ${authType} should not be injected in url. provided: ${modifiedPath}`); | ||
t.not(modifiedPath.includes(authType), | ||
`{authType} ${authType} should not be injected in url. provided: ${modifiedPath}`); | ||
t.end(); | ||
@@ -234,3 +326,3 @@ }); | ||
const { uri } = t.context; | ||
const { load } = formatUrlTemplate({ | ||
const { onRequest } = formatUrlTemplate({ | ||
formatUrlTemplate: { | ||
@@ -241,2 +333,4 @@ applyToUrl: false, | ||
const phaseHook = onRequest[0]; | ||
// 'applyToUrl' set via requestOptions should have advantage over the initialized one | ||
@@ -253,9 +347,16 @@ const requestOptions = { | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath }, authType } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.not(modifiedPath.includes(authType), `{authType} ${authType} should not be injected in url. provided: ${modifiedPath}`); | ||
t.not(modifiedPath.includes(authType), | ||
`{authType} ${authType} should not be injected in url. provided: ${modifiedPath}`); | ||
@@ -269,3 +370,3 @@ t.end(); | ||
test.cb('validation when { authType } is not provided in url template, authType: "basic"', (t) => { | ||
const { load, noAuthTypeTemplate: uri } = t.context; | ||
const { phaseHook, noAuthTypeTemplate: uri } = t.context; | ||
const requestOptions = { | ||
@@ -275,10 +376,19 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
Object.keys(requestOptions).forEach(elem => t.true(elem in modifiedParams, `${elem} should be a member of modifiedParams`)); | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.false(modifiedPath.includes('basic'), `"basic" should not be part of modified path. provided: {${modifiedPath}}`); | ||
t.false(modifiedPath.includes('basic'), | ||
`"basic" should not be part of modified path. provided: {${modifiedPath}}`); | ||
t.end(); | ||
@@ -289,3 +399,3 @@ }); | ||
test.cb('validation when wrong { authType } is provided as url template, authType: "basic"', (t) => { | ||
const { load, wrongTemplateUrl: uri } = t.context; | ||
const { phaseHook, wrongTemplateUrl: uri } = t.context; | ||
const requestOptions = { | ||
@@ -295,11 +405,20 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
Object.keys(requestOptions).forEach(elem => t.true(elem in modifiedParams, `${elem} should be a member of modifiedParams`)); | ||
Object.keys(requestOptions) | ||
.forEach(elem => t.true(elem in ctx.options, `${elem} should be a member of modifiedParams`)); | ||
t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.false(modifiedPath.includes('basic'), `"basic" should not be part of modified path. provided: {${modifiedPath}}`); | ||
t.false(modifiedPath.includes('basic'), | ||
`"basic" should not be part of modified path. provided: {${modifiedPath}}`); | ||
t.end(); | ||
@@ -310,3 +429,3 @@ }); | ||
test.cb('validation when no "qs" template is provided', (t) => { | ||
const { load, uri, qsNoTemplate } = t.context; | ||
const { phaseHook, uri, qsNoTemplate } = t.context; | ||
const requestOptions = { | ||
@@ -322,5 +441,12 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { qs: originalQs } = requestOptions; | ||
const { qs: modifiedQs } = modifiedParams; | ||
const { qs: modifiedQs } = ctx.options; | ||
@@ -334,3 +460,3 @@ t.deepEqual(originalQs, modifiedQs, `original query string should be different from modified query string. | ||
test.cb('validation when wrong "qs" template is provided', (t) => { | ||
const { load, uri, qs } = t.context; | ||
const { phaseHook, uri, qs } = t.context; | ||
const requestOptions = { | ||
@@ -346,5 +472,12 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { qs: originalQs } = requestOptions; | ||
const { qs: modifiedQs } = modifiedParams; | ||
const { qs: modifiedQs } = ctx.options; | ||
t.deepEqual(originalQs, modifiedQs, `original query string should be different from modified query string. | ||
@@ -357,3 +490,3 @@ original: {${originalQs.userid}}, modified: {${modifiedQs.userid}}`); | ||
test.cb('validation when "applyToUrl" is set to false', (t) => { | ||
const { load, uri } = t.context; | ||
const { phaseHook, uri } = t.context; | ||
const requestOptions = { | ||
@@ -368,5 +501,12 @@ uri, | ||
}; | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
@@ -381,3 +521,3 @@ t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. | ||
test.cb('validate when "authType" provided with an empty string', (t) => { | ||
const { load, uri } = t.context; | ||
const { phaseHook, uri } = t.context; | ||
const requestOptions = { | ||
@@ -388,10 +528,18 @@ uri, | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
t.not(originalPath, modifiedPath, `original uri path should be different from modified path. | ||
original: {${originalPath}}, modified: {${modifiedPath}}`); | ||
t.falsy(modifiedPath.match(/([^:]\/)\/+/g), `modified path: {${modifiedPath}} should not contain double slashes`); | ||
t.falsy(modifiedPath.match(/(authType)/, `modified path: {${modifiedPath}} should not contain "authType" when an empty string is provided`)); | ||
t.falsy(modifiedPath.match(/([^:]\/)\/+/g), | ||
`modified path: {${modifiedPath}} should not contain double slashes`); | ||
t.falsy(modifiedPath.match(/(authType)/, | ||
`modified path: {${modifiedPath}} should not contain "authType" when an empty string is provided`)); | ||
t.end(); | ||
@@ -402,3 +550,3 @@ }); | ||
test.cb('validate when "authType" is equal to undefined / not present', (t) => { | ||
const { load, uri } = t.context; | ||
const { phaseHook, uri } = t.context; | ||
const requestOptions = { | ||
@@ -409,5 +557,11 @@ uri, | ||
load(null, requestOptions, (err, modifiedParams) => { | ||
const ctx = { | ||
options: requestOptions, | ||
}; | ||
phaseHook.handler(ctx, (err) => { | ||
t.ifError(err); | ||
const { uri: { href: originalPath } } = requestOptions; | ||
const { uri: { href: modifiedPath } } = modifiedParams; | ||
const { uri: { href: modifiedPath } } = ctx.options; | ||
@@ -414,0 +568,0 @@ t.is(originalPath, modifiedPath, `original uri path should not be different from modified path. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37758
653
250
2
+ Addeddebug@^3.1.0
+ Addeddebug@3.2.7(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedms@2.1.3(transitive)
- Removedlodash@4.17.4(transitive)
Updatedlodash@^4.17.5