From 4c2c518db2e59ed9746438cb49127b3a8aac0644 Mon Sep 17 00:00:00 2001 From: Noah Dietz Date: Tue, 28 May 2019 13:55:50 -0700 Subject: [PATCH] chore: update docs --- docs/compilation.html | 16 +- docs/global.html | 2 +- docs/index.html | 97 ++++-- docs/index.js.html | 73 +++-- docs/lib_compile.js.html | 142 ++++---- docs/lib_handlebar-helpers.js.html | 35 +- docs/lib_process.js.html | 503 +++++++++++++++-------------- docs/lib_util.js.html | 28 +- docs/processing.html | 34 +- docs/templateHelpers.html | 2 +- 10 files changed, 512 insertions(+), 420 deletions(-) diff --git a/docs/compilation.html b/docs/compilation.html index c69be98..f0b21a1 100644 --- a/docs/compilation.html +++ b/docs/compilation.html @@ -132,7 +132,7 @@

compileSource:
@@ -526,7 +526,7 @@

compi
Source:
@@ -723,7 +723,7 @@

Source:
@@ -966,7 +966,7 @@

prepar
Source:
@@ -1267,7 +1267,7 @@

GeneratedPath

Source:
@@ -1392,7 +1392,7 @@

GeneratedTest

Source:
@@ -1540,7 +1540,7 @@

GeneratedTransaction

Source:
@@ -1616,7 +1616,7 @@
Type:

- Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/global.html b/docs/global.html index 7d5d17c..d3aeff7 100644 --- a/docs/global.html +++ b/docs/global.html @@ -605,7 +605,7 @@
Type:

- Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index 8c8e0df..63ff038 100644 --- a/docs/index.html +++ b/docs/index.html @@ -46,18 +46,25 @@

Home

Namespaces

  • -

    Build Status

    -

    OpenAPI Test Templates (oatts)

    +

    Build Status

    +

    OpenAPI Test Templates (oatts)

    +

    Generate basic unit test scaffolding for your OpenAPI specification.

    -

    Disclaimer

    This is not an officially supported Google product.

    +

    Disclaimer

    +

    This is not an officially supported Google product.

    oatts is based off of the swagger-test-templates module and the lessons learned during its development.

    This is a work in progress.

    -

    Goal

    The goal of oatts is to provide a standalone module for generating Node.js unit test code scaffolding based on a given OpenAPI specification.

    +

    Goal

    +

    The goal of oatts is to provide a standalone module for generating Node.js unit test code scaffolding based on a given OpenAPI specification.

    The hope is that by providing such a tool, API developers will be encouraged to test the contract between their spec and backend early, often and continuously as the project grows.

    -

    Usage

    There are a couple ways to use oatts.

    -

    Module

    Install via npm

    -
    npm install --save oatts

    Then use it in code

    +

    Usage

    +

    There are a couple ways to use oatts.

    +

    Module

    +

    Install via npm

    +
    npm install --save oatts
    +
    +

    Then use it in code

    var oatts = require('oatts');
     
     var options = {
    @@ -66,8 +73,13 @@ 

    Module

    Install via npm

    var tests = oatts.generate('/path/to/openapi.yaml', options); -console.log(tests)

    Command line interface

    Install globally via npm

    -
    npm install -g oatts

    Then use in your command line

    +console.log(tests) + +

    Command line interface

    +

    Install globally via npm

    +
    npm install -g oatts
    +
    +

    Then use in your command line

    > oatts generate --help
     
       Usage: generate [options]
    @@ -93,7 +105,10 @@ 

    Module

    Install via npm

    > oatts generate -s ./path/to/openapi.yaml -w ./output/dir > ls ./output/dir pet-test.js pet-{petId}-uploadImage-test.js user-test.js -. . .

    Using the result

    The resulting test files are built using the mocha testing framework and chakram API testing framework. Thus, you will need both of these dependencies installed in order to run your newly generated tests.

    +. . . + +

    Using the result

    +

    The resulting test files are built using the mocha testing framework and chakram API testing framework. Thus, you will need both of these dependencies installed in order to run your newly generated tests.

    After installing these, you can run the tests with mocha:

    # start your API server to test against!!
     > mocha --recursive <test directory>
    @@ -108,9 +123,13 @@ 

    Module

    Install via npm

    ✓ should respond 200 for "Success" - 2 passing (82ms)

    Custom Values

    Custom values can be supplied through both the command line and a JSON file. The in-line, command line supplied JSON will take precedent.

    + 2 passing (82ms) + +

    Custom Values

    +

    Custom values can be supplied through both the command line and a JSON file. The in-line, command line supplied JSON will take precedent.

    An example custom values JSON file can be found here.

    -

    Custom Templates

    Custom templates can be supplied via the templates option. The directory pointed to by the option must contain 4 Handlebars templates named the same way as those found in ./templates.

    +

    Custom Templates

    +

    Custom templates can be supplied via the templates option. The directory pointed to by the option must contain 4 Handlebars templates named the same way as those found in ./templates.

    • topLevel.handlebars: the top level template for a single test file
    • pathLevel.handlebars: the path level template, usually the beginning of a test suite for a specific path
    • @@ -119,7 +138,8 @@

      Custom Templates

      Custom templates can be supplied via the templ

    The data available to be used in the templates is specified in the ProcessedSpec type.

    There are also a few helpers available to be used in the Handlebars templates, which can be found in the templateHelpers documentation namespace. Use the default templates as examples of how to use them.

    -

    Options

    The following options can be passed to the generation function, some/all are exposed in the accompanying CLI:

    +

    Options

    +

    The following options can be passed to the generation function, some/all are exposed in the accompanying CLI:

    @@ -169,14 +189,14 @@

    Options

    The following options can be passed to the generation functio

    - + - + @@ -215,14 +235,49 @@

    Options

    The following options can be passed to the generation functio

    + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    consumes --consumes -coperation.consumes[0] | | spec.conumes[0]operation.consumes[0] | | spec.conumes[0] false Consumes header to use in a request when applicable
    produces --produces -ooperation.produces[0] | | spec.produces[0]operation.produces[0] | | spec.produces[0] false Produces header to use in a request when applicable
    false comma separated list of status codes to explicity generate tests for
    jsonRefsn/afalse(See JsonRefs~JsonRefsOptions)
    customFormatsn/afalseThe key/value pair of custom formats (The keys are the format name and the values are async functions. See ZSchema Custom Formats)
    customFormatGeneratorsn/afalseThe key/value pair of custom format generators (The keys are the format name and the values are functions. See json-schema-mocker Custom Format)
    customValidatorsn/afalseThe custom validators. See DocumentValidationFunction
    -

    Testing

    To test this module simply use the npm script

    -
    npm test

    Discussion

    If you have a question or a topic you'd like to discuss, please feel free to open +

    Testing

    +

    To test this module simply use the npm script

    +
    npm test
    +
    +

    Discussion

    +

    If you have a question or a topic you'd like to discuss, please feel free to open a discussion on our Google Group oatts-users.

    -

    Contributing

    Contributors are welcome! Please see CONTRIBUTING.md.

    -

    Copyright

    Copyright 2018, Google Inc.

    -

    License

    See LICENSE file.

    +

    Contributing

    +

    Contributors are welcome! Please see CONTRIBUTING.md.

    +

    Copyright

    +

    Copyright 2018, Google Inc.

    +

    License

    +

    See LICENSE file.

    @@ -235,7 +290,7 @@

    License

    See LICENSE file.


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/index.js.html b/docs/index.js.html index f0da8fe..cdf6b81 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -61,7 +61,7 @@

    index.js

    var merge2 = require('./lib/util').merge2 module.exports = { - generate: generate + generate: generate } /** @@ -79,40 +79,49 @@

    index.js

    * @return {Promise<GenerationResults>} */ function generate(specPath, options) { - return sway.create({'definition': specPath}) - .then(function (api) { - if (options.customValues) { - options.customValues = JSON.parse(options.customValues); - } - - if (options.customValuesFile) { - var customFromFile = require(join(process.cwd(), options.customValuesFile)) - options.customValues = merge2(options.customValues, customFromFile) - } + return sway.create({ + 'definition': specPath, + 'jsonRefs': options && options.jsonRefs, + 'customFormats': options && options.customFormats, + 'customFormatGenerators': options && options.customFormatGenerators, + 'customValidators': options && options.customValidators + }) + .then(function (api) { + if (options.customValues) { + options.customValues = JSON.parse(options.customValues); + } + + if (options.customValuesFile) { + var customFromFile = require( + join(process.cwd(), options.customValuesFile)) + options.customValues = merge2(options.customValues, customFromFile) + } - var processed = proc(api, options) - var compiled = compile(processed, options) + var processed = proc(api, options) + var compiled = compile(processed, options) - if (options.writeTo !== undefined) { - if (!fs.existsSync(options.writeTo)) { - fs.mkdirSync(options.writeTo) - } - - compiled.forEach(function(testObj, ndx, arr) { - fs.writeFile(join(options.writeTo, testObj.filename), testObj.contents, function(err) { - if (err !== null) { - console.error(err) - } - }) - }) + if (options.writeTo !== undefined) { + if (!fs.existsSync(options.writeTo)) { + fs.mkdirSync(options.writeTo) + } + + try { + for (var i = 0; i < compiled.length; i++) { + const testObj = compiled[i]; + fs.writeFileSync(join(options.writeTo, testObj.filename), testObj.contents) } + } catch (err) { + console.log(err); + } + } - return { 'generated': compiled } - }, function (err) { - console.error(err.stack); - return err - }); -}
    + return {'generated': compiled} + }, function (err) { + console.error(err.stack); + return err + }); +} + @@ -124,7 +133,7 @@

    index.js


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/lib_compile.js.html b/docs/lib_compile.js.html index 4685fb0..cb6dc3d 100644 --- a/docs/lib_compile.js.html +++ b/docs/lib_compile.js.html @@ -58,7 +58,7 @@

    lib/compile.js

    var fs = require('fs') var path = require('path') -var DefaultTemplateDirectory = path.join( __dirname , '..', 'templates'); +var DefaultTemplateDirectory = path.join(__dirname, '..', 'templates'); var TopLevelTemplateName = 'topLevel.handlebars'; var PathTemplateName = 'pathLevel.handlebars'; var OperationTemplateName = 'operationLevel.handlebars'; @@ -72,7 +72,8 @@

    lib/compile.js

    handlebars.registerHelper('notEmptyObject', helpers.notEmptyObject) handlebars.registerHelper('json', helpers.json) -handlebars.registerHelper('isNotDefaultStatusCode', helpers.isNotDefaultStatusCode) +handlebars.registerHelper('isNotDefaultStatusCode', + helpers.isNotDefaultStatusCode) /** * GeneratedTest is the set of results from procressing a spec & compiling the test code @@ -92,24 +93,24 @@

    lib/compile.js

    * @return {compilation.GeneratedTest[]} */ function compile(processed, options) { - var tests = [] + var tests = [] - processed.tests.forEach(function(processedPath, ndx, arr) { - var pathTest = compilePathLevel(processedPath, processed, options) + processed.tests.forEach(function (processedPath, ndx, arr) { + var pathTest = compilePathLevel(processedPath, processed, options) - if (pathTest !== null) { - tests.push({ - 'filename': processedPath.name + '-test.js', - 'contents': pathTest - }) - } - }); - - if (tests.length === 0) { - return null + if (pathTest !== null) { + tests.push({ + 'filename': processedPath.name + '-test.js', + 'contents': pathTest + }) } + }); + + if (tests.length === 0) { + return null + } - return tests + return tests } /** @@ -130,39 +131,38 @@

    lib/compile.js

    * @return {compilation.GeneratedPath} */ function compilePathLevel(processedPath, processed, options) { - var tests = [] - var requireLevelCompiler = prepareTemplate( - path.join( - options.templates ? options.templates : DefaultTemplateDirectory, - TopLevelTemplateName - ) - ) + var tests = [] + var requireLevelCompiler = prepareTemplate( + path.join( + options.templates ? options.templates : DefaultTemplateDirectory, + TopLevelTemplateName + ) + ) + + var pathLevelCompiler = prepareTemplate( + path.join( + options.templates ? options.templates : DefaultTemplateDirectory, + PathTemplateName + ) + ) - var pathLevelCompiler = prepareTemplate( - path.join( - options.templates ? options.templates : DefaultTemplateDirectory, - PathTemplateName - ) - ) - + processedPath.operations.forEach(function (processedOp, ndx, arr) { + var opLevelTest = compileOperationLevel(processedOp, processedPath, + processed, options) - processedPath.operations.forEach(function(processedOp, ndx, arr) { - var opLevelTest = compileOperationLevel(processedOp, processedPath, processed, options) + if (opLevelTest !== null) { + tests.push(opLevelTest) + } + }) - if (opLevelTest !== null) { - tests.push(opLevelTest) - } - }) - - var pathLevelDescribe = pathLevelCompiler({ - 'pathLevelDescription': processedPath.pathLevelDescription, - 'tests': tests - }) + var pathLevelDescribe = pathLevelCompiler({ + 'pathLevelDescription': processedPath.pathLevelDescription, + 'tests': tests + }) - return requireLevelCompiler({'test': pathLevelDescribe}) + return requireLevelCompiler({'test': pathLevelDescribe}) } - /** * GeneratedOp is the result of procressing an operation & compiling the operation-level test code * @typedef {object} GeneratedOp @@ -183,24 +183,25 @@

    lib/compile.js

    * @return {compilation.GeneratedOp} */ function compileOperationLevel(processedOp, processedPath, processed, options) { - var tests = [] - var operationLevelCompiler = prepareTemplate( - path.join( - options.templates ? options.templates : DefaultTemplateDirectory, - OperationTemplateName - ) - ) + var tests = [] + var operationLevelCompiler = prepareTemplate( + path.join( + options.templates ? options.templates : DefaultTemplateDirectory, + OperationTemplateName + ) + ) + + processedOp.transactions.forEach(function (transaction, ndx, arr) { + var transactionTest = compileTransactionLevel(transaction, processedOp, + processedPath, processed, options) - processedOp.transactions.forEach(function(transaction, ndx, arr) { - var transactionTest = compileTransactionLevel(transaction, processedOp, processedPath, processed, options) + tests.push(transactionTest) + }) - tests.push(transactionTest) - }) - - return operationLevelCompiler({ - 'operationLevelDescription': processedOp.operationLevelDescription, - 'operationLevelTests': tests - }) + return operationLevelCompiler({ + 'operationLevelDescription': processedOp.operationLevelDescription, + 'operationLevelTests': tests + }) } /** @@ -221,15 +222,16 @@

    lib/compile.js

    * @param {object} options for use in test compilation * @return {compilation.GeneratedTransaction} */ -function compileTransactionLevel(transaction, processedOp, processedPath, processed, options) { - var transactionCompiler = prepareTemplate( - path.join( - options.templates ? options.templates : DefaultTemplateDirectory, - TransactionTemplateName - ) - ) +function compileTransactionLevel(transaction, processedOp, processedPath, + processed, options) { + var transactionCompiler = prepareTemplate( + path.join( + options.templates ? options.templates : DefaultTemplateDirectory, + TransactionTemplateName + ) + ) - return transactionCompiler(transaction) + return transactionCompiler(transaction) } /** @@ -241,8 +243,8 @@

    lib/compile.js

    * @return {function} */ function prepareTemplate(templatePath) { - var source = fs.readFileSync(templatePath, 'utf8'); - return handlebars.compile(source, {noEscape:true}) + var source = fs.readFileSync(templatePath, 'utf8'); + return handlebars.compile(source, {noEscape: true}) } @@ -256,7 +258,7 @@

    lib/compile.js


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/lib_handlebar-helpers.js.html b/docs/lib_handlebar-helpers.js.html index a3d19f1..714f6d9 100644 --- a/docs/lib_handlebar-helpers.js.html +++ b/docs/lib_handlebar-helpers.js.html @@ -58,9 +58,9 @@

    lib/handlebar-helpers.js

    */ module.exports = { - notEmptyObject: notEmptyObject, - json: json, - isNotDefaultStatusCode: isNotDefaultStatusCode + notEmptyObject: notEmptyObject, + json: json, + isNotDefaultStatusCode: isNotDefaultStatusCode } /** @@ -72,11 +72,11 @@

    lib/handlebar-helpers.js

    * @return {boolean} */ function notEmptyObject(obj) { - if (arguments.length < 1) { - throw new Error('Handlebars Helper \'notEmptyObject\' needs 1 parameter'); - } - - return obj !== undefined && Object.keys(obj).length !== 0 + if (arguments.length < 1) { + throw new Error('Handlebars Helper \'notEmptyObject\' needs 1 parameter'); + } + + return obj !== undefined && Object.keys(obj).length !== 0 } /** @@ -88,11 +88,11 @@

    lib/handlebar-helpers.js

    * @return {string} */ function json(obj) { - if (arguments.length < 1) { - throw new Error('Handlebars Helper \'json\' needs 1 parameter'); - } + if (arguments.length < 1) { + throw new Error('Handlebars Helper \'json\' needs 1 parameter'); + } - return JSON.stringify(obj) + return JSON.stringify(obj) } /** @@ -104,11 +104,12 @@

    lib/handlebar-helpers.js

    * @return {boolean} */ function isNotDefaultStatusCode(code) { - if (arguments.length < 1) { - throw new Error('Handlebars Helper \'isNotDefaultStatusCode\' needs 1 parameter'); - } + if (arguments.length < 1) { + throw new Error( + 'Handlebars Helper \'isNotDefaultStatusCode\' needs 1 parameter'); + } - return code !== 'default' + return code !== 'default' } @@ -121,7 +122,7 @@

    lib/handlebar-helpers.js


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/lib_process.js.html b/docs/lib_process.js.html index cc86d01..b78c9a5 100644 --- a/docs/lib_process.js.html +++ b/docs/lib_process.js.html @@ -84,23 +84,25 @@

    lib/process.js

    * @return {processing.ProcessedSpec} */ function process(api, options) { - var processedSpec = { - 'host': (options.host !== undefined ? options.host : (api.host !== undefined ? api.host : 'localhost:5000')), - 'scheme': (options.scheme !== undefined ? options.scheme : (api.schemes !== undefined ? api.schemes[0] : 'http')), - 'basePath': (api.basePath !== undefined ? api.basePath : ''), - 'consumes': (api.consumes !== undefined ? api.consumes : []), - 'produces': (api.produces !== undefined ? api.produces : []) - } - - var processedPaths = processPaths(api, processedSpec, options) - if (processedPaths.length == 0) { - console.log('no paths to process in spec') - return null - } - - processedSpec.tests = processedPaths - - return processedSpec + var processedSpec = { + 'host': (options.host !== undefined ? options.host : (api.host !== undefined + ? api.host : 'localhost:5000')), + 'scheme': (options.scheme !== undefined ? options.scheme : (api.schemes + !== undefined ? api.schemes[0] : 'http')), + 'basePath': (api.basePath !== undefined ? api.basePath : ''), + 'consumes': (api.consumes !== undefined ? api.consumes : []), + 'produces': (api.produces !== undefined ? api.produces : []) + } + + var processedPaths = processPaths(api, processedSpec, options) + if (processedPaths.length == 0) { + console.log('no paths to process in spec') + return null + } + + processedSpec.tests = processedPaths + + return processedSpec } /** @@ -123,46 +125,46 @@

    lib/process.js

    * @return {processing.ProcessedPath[]} */ function processPaths(api, topLevel, options) { - var data = [] - var targetPaths = []; - - if (options.paths !== undefined) { - options.paths.forEach(function(path, ndx, arr) { - targetPaths.push(api.getPath(path)) - }) - } else { - targetPaths = api.getPaths(); - } + var data = [] + var targetPaths = []; - if (options.statusCodes !== undefined) { - var tempPaths = []; - options.statusCodes.forEach(function(code, ndx, arr) { - for(var ndx = 0; ndx < targetPaths.length; ndx++) { - var opList = targetPaths[ndx].getOperations(); - for (var opNdx = 0; opNdx < opList.length; opNdx++) { - if (opList[opNdx].getResponse(code)) { - tempPaths.push(targetPaths[ndx]) - break; - } - } - } - }); - - targetPaths = tempPaths; - } - - targetPaths.forEach(function(pathObj, ndx, arr) { - var ops = processOperations(api, pathObj, topLevel, options) - if (ops.length !== 0) { - data.push({ - 'name': pathObj.path.replace(/\//g, '-').substring(1), - 'pathLevelDescription': 'tests for ' + pathObj.path, - 'operations': ops - }) - } + if (options.paths !== undefined) { + options.paths.forEach(function (path, ndx, arr) { + targetPaths.push(api.getPath(path)) }) + } else { + targetPaths = api.getPaths(); + } + + if (options.statusCodes !== undefined) { + var tempPaths = []; + options.statusCodes.forEach(function (code, ndx, arr) { + for (var ndx = 0; ndx < targetPaths.length; ndx++) { + var opList = targetPaths[ndx].getOperations(); + for (var opNdx = 0; opNdx < opList.length; opNdx++) { + if (opList[opNdx].getResponse(code)) { + tempPaths.push(targetPaths[ndx]) + break; + } + } + } + }); + + targetPaths = tempPaths; + } + + targetPaths.forEach(function (pathObj, ndx, arr) { + var ops = processOperations(api, pathObj, topLevel, options) + if (ops.length !== 0) { + data.push({ + 'name': pathObj.path.replace(/\//g, '-').substring(1), + 'pathLevelDescription': 'tests for ' + pathObj.path, + 'operations': ops + }) + } + }) - return data + return data } /** @@ -186,18 +188,19 @@

    lib/process.js

    * @return {processing.ProcessedOp[]} */ function processOperations(api, parentPath, topLevel, options) { - var ops = [] - parentPath.getOperations().forEach(function(op, ndx, arr) { - var transactions = processTransactions(api, op, parentPath, topLevel, options) - if (transactions.length !== 0) { - ops.push({ - 'operationLevelDescription': 'tests for ' + op.method, - 'transactions': transactions - }) - } - }) + var ops = [] + parentPath.getOperations().forEach(function (op, ndx, arr) { + var transactions = processTransactions(api, op, parentPath, topLevel, + options) + if (transactions.length !== 0) { + ops.push({ + 'operationLevelDescription': 'tests for ' + op.method, + 'transactions': transactions + }) + } + }) - return ops + return ops } /** @@ -214,7 +217,7 @@

    lib/process.js

    * @property {object} formData the request form data params * @property {ExpectedResponse} expected the expected response to use for test validation * @property {boolean} hasSamples flag indicating if the expected response includes sample values - * @property {string[]} consumes based on globally defined conumes or operation defined types + * @property {string[]} consumes based on globally defined conumes or operation defined types */ /** @@ -230,39 +233,44 @@

    lib/process.js

    * @return {processing.ProcessedTransaction[]} */ function processTransactions(api, parentOp, parentPath, topLevel, options) { - var transactions = [] + var transactions = [] - parentOp.getResponses().forEach(function(res, ndx, arr) { - if (options.statusCodes && options.statusCodes.indexOf(res.statusCode) === -1) { - // skip unwanted status codes - return; - } - - var params = processParams(res.statusCode, parentOp, parentPath, options) - - var expected = processResponse(res, parentOp.method, parentPath.path, options) - var hasValue = options.samples - if (expected.custom) { - delete expected.custom - hasValue = true - } + parentOp.getResponses().forEach(function (res, ndx, arr) { + if (options.statusCodes && options.statusCodes.indexOf(res.statusCode) + === -1) { + // skip unwanted status codes + return; + } + + var params = processParams(res.statusCode, parentOp, parentPath, options) - transactions.push({ - 'testLevelDescription': util.format('should respond %s for "%s"', res.statusCode, replaceNewlines(res.description)), - 'scheme': topLevel.scheme, - 'host': topLevel.host, - 'path': (topLevel.basePath + params.path), - 'op': parentOp.method, - 'body': params.body, - 'query': params.query, - 'formData': params.formData, - 'expected': expected, - 'hasValue': hasValue, - 'headers': merge2(params.headers, processHeaders(res.statusCode, parentOp, parentPath, topLevel, options)) - }) + var expected = processResponse(res, parentOp.method, parentPath.path, + options) + var hasValue = options.samples + if (expected.custom) { + delete expected.custom + hasValue = true + } + + transactions.push({ + 'testLevelDescription': util.format('should respond %s for "%s"', + res.statusCode, replaceNewlines(res.description)), + 'scheme': topLevel.scheme, + 'host': topLevel.host, + 'path': (topLevel.basePath + params.path), + 'op': parentOp.method, + 'body': params.body, + 'query': params.query, + 'formData': params.formData, + 'expected': expected, + 'hasValue': hasValue, + 'headers': merge2(params.headers, + processHeaders(res.statusCode, parentOp, parentPath, topLevel, + options)) }) + }) - return transactions + return transactions } /** @@ -287,40 +295,53 @@

    lib/process.js

    * @return {processing.ProcessedParams} */ function processParams(code, op, path, options) { - var params = { - 'body' : {}, - 'query': {}, - 'formData': {}, - 'path': path.path, - 'headers': {} + var params = { + 'body': {}, + 'query': {}, + 'formData': {}, + 'path': path.path, + 'headers': {} + } + + op.getParameters().forEach(function (param, ndx, arr) { + var customValue = lookupCustomValue(param.name, param.in, code, op.method, + path.path, options) + // Skip optional parameter with specifically nulled custom value + if (customValue === null && (! param.required)) { + return } - op.getParameters().forEach(function(param, ndx, arr) { - var customValue = lookupCustomValue(param.name, param.in, code, op.method, path.path, options) - - if (param.in === 'body') { - params.body = customValue !== undefined ? customValue : param.getSample() - } else if (param.in === 'query') { - params.query[param.name] = customValue !== undefined ? customValue : param.getSample() - } else if (param.in === 'formData') { - try { - params.formData[param.name] = customValue !== undefined ? customValue : param.getSample() - } catch(e) { // this will be because of formData property of type 'file' - params.formData[param.name] = '{fileUpload}' - } - } else if (param.in === 'path') { - params.path = pathify(params.path, param, customValue) - } else if (param.in === 'header') { - params.headers[param.name] = customValue !== undefined ? customValue : param.getSample() - } - }) - - path.getParameters().forEach(function(param, ndx, arr) { - var customValue = lookupCustomValue(param.name, param.in, code, op.method, path.path, options) - params.path = pathify(params.path, param, customValue) - }) + if (param.in === 'body') { + params.body = customValue !== undefined ? customValue : param.getSample() + } else if (param.in === 'query') { + params.query[param.name] = customValue !== undefined ? customValue + : param.getSample() + } else if (param.in === 'formData') { + try { + params.formData[param.name] = customValue !== undefined ? customValue + : param.getSample() + } catch (e) { // this will be because of formData property of type 'file' + params.formData[param.name] = '{fileUpload}' + } + } else if (param.in === 'path') { + params.path = pathify(params.path, param, customValue) + } else if (param.in === 'header') { + params.headers[param.name] = customValue !== undefined ? customValue + : param.getSample() + } + }) + + path.getParameters().forEach(function (param, ndx, arr) { + var customValue = lookupCustomValue(param.name, param.in, code, op.method, + path.path, options) + // Skip optional parameter with specifically nulled custom value + if (customValue === null && (! param.required)) { + return + } + params.path = pathify(params.path, param, customValue) + }) - return params + return params } /** @@ -337,25 +358,25 @@

    lib/process.js

    * @return {any} */ function lookupCustomValue(name, location, code, op, path, options) { - var levels = [path, op, code] - var val = undefined; - - if (options.customValues) { - var curr = options.customValues - - var ndx = 0 - do { - if (curr[location]) { - if (curr[location][name] !== undefined) { - val = curr[location][name] - } - } - - curr = curr[levels[ndx]] - } while(ndx++ < levels.length && curr) - } + var levels = [path, op, code] + var val = undefined; - return val + if (options.customValues) { + var curr = options.customValues + + var ndx = 0 + do { + if (curr[location]) { + if (curr[location][name] !== undefined) { + val = curr[location][name] + } + } + + curr = curr[levels[ndx]] + } while (ndx++ < levels.length && curr) + } + + return val } /** @@ -371,65 +392,67 @@

    lib/process.js

    * @return {object} */ function processHeaders(responseCode, op, path, top, options) { - var headers = {}; - var levels = [path.path, op.method, responseCode] - - // check custom request values for any desired headers; cascading merge - if (options.customValues) { - var curr = options.customValues - - var ndx = 0 - do { - if (curr['header'] !== undefined) { - headers = merge2(curr['header'], headers) - } - - curr = curr[levels[ndx]] - } while(ndx++ < levels.length && curr !== undefined) - } - - // handle consumes - if (options.consumes) { // a specific content-type was targeted - if (op.definitionFullyResolved.consumes) { - // this operation overrides global consumes, and does contain the requested content-type - if (op.definitionFullyResolved.consumes.indexOf(options.consumes) != -1) { - headers['Content-Type'] = options.consumes; - } else { // we will just use the first one because this doesn't match - headers['Content-Type'] = op.definitionFullyResolved.consumes[0]; - } - - } else if (top.consumes.length > 0 && top.consumes.indexOf(options.consumes) != -1) { - headers['Content-Type'] = options.consumes; - } else if (top.consumes.length === 1) { // unless there is a singular different global consumes, just use that one - headers['Content-Type'] = top.consumes[0]; - } - } else if (op.definitionFullyResolved.consumes) { // we will just use the first one if none are specified + var headers = {}; + var levels = [path.path, op.method, responseCode] + + // handle consumes + if (options.consumes) { // a specific content-type was targeted + if (op.definitionFullyResolved.consumes) { + // this operation overrides global consumes, and does contain the requested content-type + if (op.definitionFullyResolved.consumes.indexOf(options.consumes) != -1) { + headers['Content-Type'] = options.consumes; + } else { // we will just use the first one because this doesn't match headers['Content-Type'] = op.definitionFullyResolved.consumes[0]; - } else if (top.consumes.length > 0) { - headers['Content-Type'] = top.consumes[0]; - } + } - // handle produces - if (options.produces) { - if (op.definitionFullyResolved.produces) { - if (op.definitionFullyResolved.produces.indexOf(options.produces) != -1) { - headers['Accept'] = options.produces; - } else { - headers['Accept'] = op.definitionFullyResolved.produces[0]; - } - - } else if (top.produces.length > 0 && top.produces.indexOf(options.produces) != -1) { - headers['Accept'] = options.produces; - } else if (top.produces.length === 1) { - headers['Accept'] = top.produces[0]; - } - } else if (op.definitionFullyResolved.produces) { - headers['Accept'] = op.definitionFullyResolved.produces[0]; - } else if (top.produces.length > 0) { - headers['Accept'] = top.produces[0]; + } else if (top.consumes.length > 0 && top.consumes.indexOf(options.consumes) + != -1) { + headers['Content-Type'] = options.consumes; + } else if (top.consumes.length === 1) { // unless there is a singular different global consumes, just use that one + headers['Content-Type'] = top.consumes[0]; } + } else if (op.definitionFullyResolved.consumes) { // we will just use the first one if none are specified + headers['Content-Type'] = op.definitionFullyResolved.consumes[0]; + } else if (top.consumes.length > 0) { + headers['Content-Type'] = top.consumes[0]; + } + + // handle produces + if (options.produces) { + if (op.definitionFullyResolved.produces) { + if (op.definitionFullyResolved.produces.indexOf(options.produces) != -1) { + headers['Accept'] = options.produces; + } else { + headers['Accept'] = op.definitionFullyResolved.produces[0]; + } - return headers + } else if (top.produces.length > 0 && top.produces.indexOf(options.produces) + != -1) { + headers['Accept'] = options.produces; + } else if (top.produces.length === 1) { + headers['Accept'] = top.produces[0]; + } + } else if (op.definitionFullyResolved.produces) { + headers['Accept'] = op.definitionFullyResolved.produces[0]; + } else if (top.produces.length > 0) { + headers['Accept'] = top.produces[0]; + } + + // check custom request values for any desired headers; cascading merge + if (options.customValues) { + var curr = options.customValues + + var ndx = 0 + do { + if (curr['header'] !== undefined) { + headers = merge2(curr['header'], headers) + } + + curr = curr[levels[ndx]] + } while (ndx++ < levels.length && curr !== undefined) + } + + return headers } /** @@ -453,29 +476,30 @@

    lib/process.js

    * @return {processing.ExpectedResponse} */ function processResponse(res, op, path, options) { - var expected = { - 'statusCode': res.statusCode, - 'custom': false - } - - if (options.customValues - && options.customValues[path] - && options.customValues[path][op] - && options.customValues[path][op][res.statusCode] - && options.customValues[path][op][res.statusCode].response) { - - expected['res'] = options.customValues[path][op][res.statusCode].response - expected.custom = true; - - return expected - } + var expected = { + 'statusCode': res.statusCode, + 'custom': false + } + + if (options.customValues + && options.customValues[path] + && options.customValues[path][op] + && options.customValues[path][op][res.statusCode] + && options.customValues[path][op][res.statusCode].response) { + + expected['res'] = options.customValues[path][op][res.statusCode].response + expected.custom = true; - var res = options.samples ? res.getSample() : res.definitionFullyResolved.schema - if (res !== undefined) { - expected['res'] = res - } - return expected + } + + var res = options.samples ? res.getSample() + : res.definitionFullyResolved.schema + if (res !== undefined) { + expected['res'] = res + } + + return expected } /** @@ -489,16 +513,16 @@

    lib/process.js

    * @return {string} */ function pathify(path, param, value) { - var regex = new RegExp('(\/.*){'+param.name+'}(\/.*)*', 'g') - var sample = value !== undefined ? value : param.getSample() - if (isNumberType(param.definition.type)) { - // prevent negative sample values for numbers - while (sample < 0) { - sample = param.getSample() - } + var regex = new RegExp('(\/.*){' + param.name + '}(\/.*)*', 'g') + var sample = value !== undefined ? value : param.getSample() + if (isNumberType(param.definition.type)) { + // prevent negative sample values for numbers + while (sample < 0) { + sample = param.getSample() } + } - return path.replace(regex, '$1'+sample+'$2'); + return path.replace(regex, '$1' + sample + '$2').replace(/ /g, ''); } /** @@ -510,7 +534,8 @@

    lib/process.js

    * @return {boolean} */ function isNumberType(type) { - return (type === 'integer' || type === 'float' || type === 'long' || type === 'double') + return (type === 'integer' || type === 'float' || type === 'long' || type + === 'double') } /** @@ -522,14 +547,14 @@

    lib/process.js

    * @return {string} */ function determineQueryType(param) { - var val = param.name; // default to the name if all else fails - - if (param.definitionFullyResolved.type === 'array') { - val = param.definitionFullyResolved.items.type + '[]' - } else { - val = param.definitionFullyResolved.type - } - return '{' + val + '}' + var val = param.name; // default to the name if all else fails + + if (param.definitionFullyResolved.type === 'array') { + val = param.definitionFullyResolved.items.type + '[]' + } else { + val = param.definitionFullyResolved.type + } + return '{' + val + '}' } /** @@ -541,7 +566,7 @@

    lib/process.js

    * @return {string} */ function replaceNewlines(str) { - return str.replace(/\r?\n|\r/g, " ") + return str.replace(/\r?\n|\r/g, " ") } @@ -555,7 +580,7 @@

    lib/process.js


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/lib_util.js.html b/docs/lib_util.js.html index ee031b1..b1661a1 100644 --- a/docs/lib_util.js.html +++ b/docs/lib_util.js.html @@ -54,31 +54,31 @@

    lib/util.js

    'use strict'; module.exports = { - merge2: merge2 + merge2: merge2 } /** * merges two objects, with obj1 taking override precedent * @function merge2 - * @instance + * @instance * @param {object} obj1 dominant object to merge with * @param {object} obj2 subordinate object to merge with * @return {object} */ function merge2(obj1, obj2) { - if (!obj1) { - return obj2; - } else if (!obj2) { - return obj1; - } - - var result = obj2 + if (!obj1) { + return obj2; + } else if (!obj2) { + return obj1; + } - Object.keys(obj1).forEach(function(key, ndx, arr) { - result[key] = obj1[key]; - }); + var result = obj2 - return result + Object.keys(obj1).forEach(function (key, ndx, arr) { + result[key] = obj1[key]; + }); + + return result } @@ -91,7 +91,7 @@

    lib/util.js


    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/processing.html b/docs/processing.html index 1e1741f..9e86b19 100644 --- a/docs/processing.html +++ b/docs/processing.html @@ -132,7 +132,7 @@

    det
    Source:
    @@ -283,7 +283,7 @@

    isNumberT
    Source:
    @@ -434,7 +434,7 @@

    look
    Source:
    @@ -700,7 +700,7 @@

    pathifySource:
    @@ -1074,7 +1074,7 @@

    process
    Source:
    @@ -1317,7 +1317,7 @@

    proc
    Source:
    @@ -1537,7 +1537,7 @@

    processP
    Source:
    @@ -1757,7 +1757,7 @@

    processPa
    Source:
    @@ -1954,7 +1954,7 @@

    proces
    Source:
    @@ -2174,7 +2174,7 @@

    pr
    Source:
    @@ -2417,7 +2417,7 @@

    replac
    Source:
    @@ -2570,7 +2570,7 @@

    ExpectedResponse

    Source:
    @@ -2741,7 +2741,7 @@

    ProcessedParams

    Source:
    @@ -2935,7 +2935,7 @@

    ProcessedPath

    Source:
    @@ -3323,7 +3323,7 @@

    ProcessedTransaction

    Source:
    @@ -3678,7 +3678,7 @@

    ProcessedOp

    Source:
    @@ -3829,7 +3829,7 @@
    Type:

    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.
    diff --git a/docs/templateHelpers.html b/docs/templateHelpers.html index e469c8c..4c03bfd 100644 --- a/docs/templateHelpers.html +++ b/docs/templateHelpers.html @@ -589,7 +589,7 @@
    Returns:

    - Documentation generated by JSDoc 3.5.5 on Fri May 18 2018 10:10:23 GMT-0700 (PDT) using the docdash theme. + Documentation generated by JSDoc 3.6.2 on Tue May 28 2019 13:54:41 GMT-0700 (Pacific Daylight Time) using the docdash theme.