Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set basePath to empty string #17

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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