Skip to content

Commit

Permalink
fix: channels with name '/' fail on validation (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg authored Nov 13, 2020
1 parent f14db92 commit d371017
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ utils.parseUrlVariables = str => {
*/
utils.parseUrlQueryParameters = str => {
if (typeof str !== 'string') return;
const channelName = str;
const url = new URL(`http://${channelName}`);

return url.search;
return str.match(/\?((.*=.*)(&?))/g);
};

/**
Expand Down
76 changes: 76 additions & 0 deletions test/customValidators_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ describe('validateChannel()', function () {
expect(validateChannels(parsedInput, inputString, input)).to.equal(true);
});

it('should successfully validate channel name is just a single slash (/)', async function () {
const inputString = `{
"channels": {
"/": {
}
}
}`;
const parsedInput = JSON.parse(inputString);

expect(validateChannels(parsedInput, inputString, input)).to.equal(true);
});

it('should throw error that the provided channel name is invalid', async function () {
const inputString = `{
"channels": {
Expand Down Expand Up @@ -375,6 +387,70 @@ describe('validateChannel()', function () {
}
});

it('should throw error that the provided channel name is invalid when channel name is just a single slash (/)', async function () {
const inputString = `{
"channels": {
"/?foo=1": {
}
}
}`;
const parsedInput = JSON.parse(inputString);

try {
validateChannels(parsedInput, inputString, input);
} catch (e) {
expect(e.type).to.equal('https://github.com/asyncapi/parser-js/validation-errors');
expect(e.title).to.equal('Channel validation failed');
expect(e.parsedJSON).to.deep.equal(parsedInput);
expect(e.validationErrors).to.deep.equal([
{
title: '/?foo=1 channel contains invalid name with url query parameters: ?foo=1',
location: {
endColumn: 11,
endLine: 4,
endOffset: 52,
jsonPointer: '/channels/~1?foo=1',
startColumn: 21,
startLine: 3,
startOffset: 41
}
}
]);
}
});

it('should throw error that channel has invalid name with two query params', async function () {
const inputString = `{
"channels": {
"/user/signedup?foo=1&bar=0": {
}
}
}`;
const parsedInput = JSON.parse(inputString);

try {
validateChannels(parsedInput, inputString, input);
} catch (e) {
expect(e.type).to.equal('https://github.com/asyncapi/parser-js/validation-errors');
expect(e.title).to.equal('Channel validation failed');
expect(e.parsedJSON).to.deep.equal(parsedInput);
expect(e.validationErrors).to.deep.equal([
{
title: '/user/signedup?foo=1&bar=0 channel contains invalid name with url query parameters: ?foo=1&bar=0',
location: {
endColumn: 9,
endLine: 4,
endOffset: 65,
jsonPointer: '/channels/~1user~1signedup?foo=1&bar=0',
startColumn: 38,
startLine: 3,
startOffset: 56
}
}
]);
}
});

it('should throw error that one of the provided channel name is invalid', async function () {
const inputString = `{
"channels": {
Expand Down

0 comments on commit d371017

Please sign in to comment.