diff --git a/src/lib/core.js b/src/lib/core.js index aec49b3..712129d 100644 --- a/src/lib/core.js +++ b/src/lib/core.js @@ -90,7 +90,7 @@ function getBasePath(apiDefinition){ if (apiDefinition.hasOwnProperty("openapi") && apiDefinition.servers && apiDefinition.servers[0].url) { apiDefinition.basePath = apiDefinition.servers[0].url; } - return config.basePath || apiDefinition.basePath; + return config.basePath !== undefined ? config.basePath : apiDefinition.basePath; } /** diff --git a/tests/emptyBasePath.test.js b/tests/emptyBasePath.test.js new file mode 100644 index 0000000..f776dcd --- /dev/null +++ b/tests/emptyBasePath.test.js @@ -0,0 +1,65 @@ +const test = require('uvu').test; +const assert = require('uvu/assert'); +const pactum = require('pactum'); +const reporter = pactum.reporter; +const mock = pactum.mock; +const request = pactum.request; +const handler = pactum.handler; + +const psc = require('../src/index'); + +test.before(() => { + psc.swaggerYamlPath = './tests/testObjects/emptyBasePath.yaml'; + psc.reportFile = 'emptyBasePath.json' + reporter.add(psc); + request.setBaseUrl('http://localhost:9393'); + handler.addInteractionHandler('get all ninjas', () => { + return { + request: { + method: 'GET', + path: '/getallninjas' + }, + response: { + status: 200 + } + } + }); + return mock.start(); +}); + +test.after(() => { + return mock.stop(); +}); + +test('spec passed', async () => { + await pactum.spec() + .useInteraction('get all ninjas') + .get('/getallninjas') + .expectStatus(200); +}); + +test('run reporter', async () => { + await reporter.end(); +}); + +test('validate json reporter', async () => { + const report = require('../reports/emptyBasePath.json'); + console.log(JSON.stringify(report, null, 2)); + assert.equal(Object.keys(report).length, 7); + assert.equal(report.hasOwnProperty("basePath"), true) + assert.equal(report.hasOwnProperty("coverage"), true) + assert.equal(report.hasOwnProperty("coveredApiCount"), true) + assert.equal(report.hasOwnProperty("missedApiCount"), true) + assert.equal(report.hasOwnProperty("totalApiCount"), true) + assert.equal(report.hasOwnProperty("coveredApiList"), true) + assert.equal(report.hasOwnProperty("missedApiList"), true) + assert.equal(report.coverage, 0.5); + assert.equal(report.coveredApiCount, 1); + assert.equal(report.missedApiCount, 1); + assert.equal(report.totalApiCount, 2); + assert.equal(report.coveredApiList.length, 1); + assert.equal(report.missedApiList.length, 1); + assert.equal(report.basePath, ''); // should return config basePath if not specified +}); + +test.run(); diff --git a/tests/testObjects/emptyBasePath.yaml b/tests/testObjects/emptyBasePath.yaml new file mode 100644 index 0000000..73a5757 --- /dev/null +++ b/tests/testObjects/emptyBasePath.yaml @@ -0,0 +1,51 @@ +openapi: 3.0.0 +info: + title: Test API server + version: 1.0.0 +servers: +- url: / +paths: + /health: + get: + tags: + - HealthCheck + description: Health check + operationId: getHealth + responses: + 200: + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/Health' + text/html: + schema: + $ref: '#/components/schemas/Health' + x-swagger-router-controller: health.controller + /getallninjas: + get: + tags: + - Ninjas + description: Get All Ninjas + operationId: getAllNinjas + responses: + 200: + description: Success + content: + application/json: + schema: + $ref: '#/components/schemas/Health' + text/html: + schema: + $ref: '#/components/schemas/Health' + x-swagger-router-controller: test.controller +components: + schemas: + Health: + type: object + properties: + message: + type: string + description: Health Response + example: + message: OK