Skip to content

Commit bbe899d

Browse files
authored
Minor tidy; Slightly improve test coverage (#21)
1 parent 11a30c3 commit bbe899d

File tree

2 files changed

+39
-49
lines changed

2 files changed

+39
-49
lines changed

lib/resValidator.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,13 @@ function getOAPath(req, openApiSpec) {
119119
throw new Error(`No server matching '${pathname}' path defined in OpenAPI spec`);
120120
}
121121
const pathnameWithoutServerUrl = removeServerURLFromPath(pathname, serverUrl);
122-
if (openApiSpec.paths.hasOwnProperty(pathnameWithoutServerUrl)) {
123-
return pathnameWithoutServerUrl;
124-
}
125122
const OAPath = findOAPathMatchingPathname(pathnameWithoutServerUrl, openApiSpec);
126123
return OAPath || pathnameWithoutServerUrl;
127124
}
128125

129126
function findOAPathMatchingPathname(pathname, openApiSpec) {
130127
const OAPaths = Object.keys(openApiSpec.paths);
131-
const OAPathsWithPathParams = OAPaths.filter(path => path.includes('{'));
132-
const OAPath = OAPathsWithPathParams.find(OAPath => {
128+
const OAPath = OAPaths.find(OAPath => {
133129
const pathInColonForm = OAPath.replace(/{/g, ':').replace(/}/g, ''); // converts all {foo} to :foo
134130
const pathParser = new PathParser(pathInColonForm);
135131
const pathParamsInPathname = pathParser.test(pathname);
@@ -148,4 +144,4 @@ function getServerUrlUsed(openApiPathWithServerUrl, servers) {
148144

149145
function removeServerURLFromPath(openApiPathWithServerUrl, serverUrlUsed) {
150146
return openApiPathWithServerUrl.replace(serverUrlUsed, '');
151-
}
147+
}

test/unit/assertions/usingServer.test.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,49 @@ const path = require('path');
2020
const chaiResponseValidator = require('../../..');
2121

2222
const pathToApiSpec = path.resolve('test/exampleOpenApiFiles/valid/openapi3.yml');
23-
23+
chai.use(chaiResponseValidator(pathToApiSpec));
2424
const { expect } = chai;
2525

26+
describe('Using an OA3 spec that defines server paths', function () {
27+
describe('res.req.path matches a defined sever path', function () {
28+
const differentServer = '/remote';
29+
const res = {
30+
status: 200,
31+
req: {
32+
method: 'GET',
33+
path: `${differentServer}/test/responseBody/schemaDef`,
34+
},
35+
body: 'valid body (string)',
36+
};
2637

27-
describe('Server tests', function () {
28-
before(function () {
29-
chai.use(chaiResponseValidator(pathToApiSpec));
30-
});
31-
describe('using a different server', function () {
32-
describe('the server path is defined', function () {
33-
const differentServer = '/remote';
34-
const res = {
35-
status: 200,
36-
req: {
37-
method: 'GET',
38-
path: `${differentServer}/test/responseBody/schemaDef`,
39-
},
40-
body: 'valid body (string)',
41-
};
42-
43-
it('passes', function () {
44-
expect(res).to.satisfyApiSpec;
45-
});
38+
it('passes', function () {
39+
expect(res).to.satisfyApiSpec;
40+
});
4641

47-
it('fails when using .not', function () {
48-
const assertion = () => expect(res).to.not.satisfyApiSpec;
49-
expect(assertion).to.throw('');
50-
});
42+
it('fails when using .not', function () {
43+
const assertion = () => expect(res).to.not.satisfyApiSpec;
44+
expect(assertion).to.throw('');
5145
});
52-
describe('the server path is NOT defined', function () {
53-
const differentServer = '/missing';
54-
const res = {
55-
status: 200,
56-
req: {
57-
method: 'GET',
58-
path: `${differentServer}/test/responseBody/schemaDef`,
59-
},
60-
body: 'valid body (string)',
61-
};
46+
});
47+
describe('res.req.path does not match a defined sever path', function () {
48+
const differentServer = '/missing';
49+
const res = {
50+
status: 200,
51+
req: {
52+
method: 'GET',
53+
path: `${differentServer}/test/responseBody/schemaDef`,
54+
},
55+
body: 'valid body (string)',
56+
};
6257

63-
it('fails', function () {
64-
const assertion = () => expect(res).to.satisfyApiSpec;
65-
expect(assertion).to.throw('No server matching \'/missing/test/responseBody/schemaDef\' path defined in OpenAPI spec');
66-
});
58+
it('fails', function () {
59+
const assertion = () => expect(res).to.satisfyApiSpec;
60+
expect(assertion).to.throw('No server matching \'/missing/test/responseBody/schemaDef\' path defined in OpenAPI spec');
61+
});
6762

68-
it('fails when using .not', function () {
69-
const assertion = () => expect(res).to.not.satisfyApiSpec;
70-
expect(assertion).to.throw('No server matching \'/missing/test/responseBody/schemaDef\' path defined in OpenAPI spec');
71-
});
63+
it('fails when using .not', function () {
64+
const assertion = () => expect(res).to.not.satisfyApiSpec;
65+
expect(assertion).to.throw('No server matching \'/missing/test/responseBody/schemaDef\' path defined in OpenAPI spec');
7266
});
7367
});
74-
});
68+
});

0 commit comments

Comments
 (0)