Skip to content

Commit

Permalink
fix: bug where variable-casing in servers and urls would break path m…
Browse files Browse the repository at this point in the history
…atching (#456)

* fix: bug where variable-casing in servers and urls would break path matching

* chore: i can write clear sentences, yes

* chore: making a test case clearer what its testing
  • Loading branch information
erunion authored Jun 30, 2021
1 parent 9e3b418 commit 2858078
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
50 changes: 50 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,56 @@ describe('#findOperation()', () => {
});

describe('quirks', () => {
it('should return a match if a defined server has camelcasing, but the uri is all lower', () => {
const oas = new Oas({
servers: [{ url: 'https://api.EXAMPLE.com/' }],
paths: {
'/anything': {
get: {
responses: { 200: { description: 'OK' } },
},
},
},
});

const uri = 'https://api.example.com/anything';
const method = 'get';

const res = oas.findOperation(uri, method);
expect(res.url).toStrictEqual({
origin: 'https://api.EXAMPLE.com',
path: '/anything',
nonNormalizedPath: '/anything',
slugs: {},
method: 'GET',
});
});

it("should return a match if the uri has variable casing but the defined server doesn't", () => {
const oas = new Oas({
servers: [{ url: 'https://api.example.com/' }],
paths: {
'/anything': {
get: {
responses: { 200: { description: 'OK' } },
},
},
},
});

const uri = 'https://api.EXAMPLE.com/anything';
const method = 'get';

const res = oas.findOperation(uri, method);
expect(res.url).toStrictEqual({
origin: 'https://api.example.com',
path: '/anything',
nonNormalizedPath: '/anything',
slugs: {},
method: 'GET',
});
});

it('should return result if path contains non-variabled colons', () => {
const oas = new Oas(pathVariableQuirks);
const uri = 'https://api.example.com/people/GWID:3';
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class Oas {

findOperationMatches(url) {
const { origin, hostname } = new URL(url);
const originRegExp = new RegExp(origin);
const originRegExp = new RegExp(origin, 'i');
const { servers, paths } = this;

let pathName;
Expand Down Expand Up @@ -391,7 +391,7 @@ class Oas {
url: this.replaceUrl(matchedServer.url, matchedServer.variables || {}),
};

[, pathName] = url.split(targetServer.url);
[, pathName] = url.split(new RegExp(targetServer.url, 'i'));
}

if (pathName === undefined) return undefined;
Expand Down

0 comments on commit 2858078

Please sign in to comment.