Skip to content

Commit

Permalink
Merge pull request #17 from ElnSorokina/empty-basePath
Browse files Browse the repository at this point in the history
Allow to set basePath to empty string
  • Loading branch information
leelaprasadv authored Nov 16, 2023
2 parents 596c8aa + 740eee9 commit 25ef8b1
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
65 changes: 65 additions & 0 deletions tests/emptyBasePath.test.js
Original file line number Diff line number Diff line change
@@ -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();
51 changes: 51 additions & 0 deletions tests/testObjects/emptyBasePath.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 25ef8b1

Please sign in to comment.