diff --git a/package.json b/package.json index 6f6c945..05c2be8 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "test": "mocha --recursive" }, "dependencies": { - "axios": "^0.18.1", + "axios": "^0.21.2", "expect": "^1.13.0", "js-yaml": "^3.5.4", "jsonschema": "1.1.0", diff --git a/src/apiSpecs.js b/src/apiSpecs.js index 1e6481a..5df053a 100644 --- a/src/apiSpecs.js +++ b/src/apiSpecs.js @@ -8,10 +8,13 @@ const path = require('path') const assert = require('assert') const jsonSchema = require('jsonschema') const url = require('url') -const request = require('axios') +const axios = require('axios') const swaggerSpec = require('./swaggerSpec') const expect = require('expect') +/** @type AxiosInstance */ +const request = axios.create() + const ONLY_SPEC_MARKER = '+' const ONLY_VERBOSE_SPEC_MARKER = 'v+' @@ -138,6 +141,14 @@ function getStepOperation(step, operations, primaryOp) { return operations.find(opt => opt.id === step.request.operationId) || primaryOp } +function logRequest(step, specInfo, req, res) { + if (specInfo.verbose) { + const msg = `[${step.index}]${specInfo.summary}\n` + + `${prettyJson('Request:', req)}\n${prettyJson('Response:', res)}` + console.log(msg) + } +} + function runStep(step, op, specInfo, options, acc) { if (!acc) acc = [] const req = createRequest(op, step.request, options, acc) @@ -146,23 +157,17 @@ function runStep(step, op, specInfo, options, acc) { validateRequest(req, step, op) } return request(req) + .catch(error => { + // might be expected response + if (error.response) return error.response + throw error + }) .then(res => { acc.push({ req, res }) - if (specInfo.verbose) { - const msg = `[${step.index}]${specInfo.summary}\n` + - `${prettyJson('Request:', req)}\n${prettyJson('Response:', res)}` - console.log(msg) - } - return res - }, res => { - acc.push({ req, res }) - return res + logRequest(step, specInfo, req, res) + validateResponse(req, res, step, op, options, acc) + return acc }) - .then( - res => validateResponse(req, res, step, op, options, acc), - res => validateResponse(req, res, step, op, options, acc) - ) - .then(() => acc) } function getSpecs(op, options) { @@ -422,7 +427,13 @@ function getJsonFile(jsonPath) { } function prettyJson(title, obj) { + const cleanObj = Object.assign({}, obj, { request: null }) const MAX_LINES = 400 - const lines = JSON.stringify(obj, null, 2).split('\n').slice(0, MAX_LINES).join('\n') - return `${title}\n${lines}` + try { + const lines = JSON.stringify(cleanObj, null, 2).split('\n').slice(0, MAX_LINES).join('\n') + return `${title}\n${lines}` + } catch(e) { + console.log('Failed to JSON', obj) + return `${title}\n${obj}` + } } diff --git a/yarn.lock b/yarn.lock index 87a5a16..69649d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -107,13 +107,12 @@ async@~0.9.0: version "0.9.2" resolved "https://registry.yarnpkg.com/async/-/async-0.9.2.tgz#aea74d5e61c1f899613bf64bda66d4c78f2fd17d" -axios@^0.18.1: - version "0.18.1" - resolved "https://registry.npmjs.org/axios/-/axios-0.18.1.tgz#ff3f0de2e7b5d180e757ad98000f1081b87bcea3" - integrity sha512-0BfJq4NSfQXd+SkFdrvFbG7addhYSBA2mQwISr46pD6E5iqkWg02RAs8vyTT/j0RTnoYmeXauBuSv1qKwR179g== +axios@^0.21.2: + version "0.21.4" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" + integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== dependencies: - follow-redirects "1.5.10" - is-buffer "^2.0.2" + follow-redirects "^1.14.0" babel-core@^5.8.33: version "5.8.38" @@ -442,13 +441,6 @@ debug@2.2.0, debug@~2.2.0: dependencies: ms "0.7.1" -debug@=3.1.0: - version "3.1.0" - resolved "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" - integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== - dependencies: - ms "2.0.0" - debug@^2.1.1, debug@^2.2.0: version "2.3.2" resolved "https://registry.yarnpkg.com/debug/-/debug-2.3.2.tgz#94cb466ef7d6d2c7e5245cdd6e4104f2d0d70d30" @@ -844,12 +836,10 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -follow-redirects@1.5.10: - version "1.5.10" - resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a" - integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ== - dependencies: - debug "=3.1.0" +follow-redirects@^1.14.0: + version "1.14.7" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685" + integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ== foreach@^2.0.5: version "2.0.5" @@ -1087,11 +1077,6 @@ is-buffer@^1.0.2: version "1.1.4" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.4.tgz#cfc86ccd5dc5a52fa80489111c6920c457e2d98b" -is-buffer@^2.0.2: - version "2.0.3" - resolved "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725" - integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw== - is-callable@^1.0.4, is-callable@^1.1.1, is-callable@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.3.tgz#86eb75392805ddc33af71c92a0eedf74ee7604b2" @@ -1600,11 +1585,6 @@ ms@0.7.2: version "0.7.2" resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765" -ms@2.0.0: - version "2.0.0" - resolved "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" - integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= - multer@^1.1.0: version "1.2.0" resolved "https://registry.yarnpkg.com/multer/-/multer-1.2.0.tgz#5be1a45259fb04d2753d33c7c2a1caf5224705a2"