Skip to content

Commit

Permalink
fix: Issue with Parameters Passed in the URL(usebruno#2124) (usebruno…
Browse files Browse the repository at this point in the history
…#2139)

* fix: Issue with Parameters Passed in the URL(usebruno#2124)

The '=' should be allowed within query parameter value. While first equals sign separates parameter name from its value, any subsequent occurrences are valid and should not be discarded.
The '#' in URL always indicates the start of URI Fragment component, and should not be treated as part of any parameter value.

* chore: gracefully fail when URLSearchParams throws error

---------

Co-authored-by: Anoop M D <[email protected]>
  • Loading branch information
pietrygamat and helloanoop authored Aug 29, 2024
1 parent 36ef38b commit 93080de
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/bruno-app/src/utils/url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ const hasLength = (str) => {
};

export const parseQueryParams = (query) => {
if (!query || !query.length) {
try {
if (!query || !query.length) {
return [];
}

return Array.from(new URLSearchParams(query.split('#')[0]).entries())
.map(([name, value]) => ({ name, value }));
} catch (error) {
console.error('Error parsing query params:', error);
return [];
}

let params = query.split('&').map((param) => {
let [name, value = ''] = param.split('=');
return { name, value };
});

return filter(params, (p) => hasLength(p.name));
};

export const parsePathParams = (url) => {
Expand Down
17 changes: 17 additions & 0 deletions packages/bruno-app/src/utils/url/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ describe('Url Utils - parseQueryParams', () => {
{ name: 'b', value: '2' }
]);
});

it('should parse query with "=" character - case 9', () => {
const params = parseQueryParams('a=1&b={color=red,size=large}&c=3');
expect(params).toEqual([
{ name: 'a', value: '1' },
{ name: 'b', value: '{color=red,size=large}' },
{ name: 'c', value: '3' }
]);
});

it('should parse query with fragment - case 10', () => {
const params = parseQueryParams('a=1&b=2#I-AM-FRAGMENT');
expect(params).toEqual([
{ name: 'a', value: '1' },
{ name: 'b', value: '2' }
]);
});
});

describe('Url Utils - parsePathParams', () => {
Expand Down

0 comments on commit 93080de

Please sign in to comment.