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

fix: channels with name / fail on validation #196

Merged
merged 1 commit into from
Nov 13, 2020
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
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