mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 01:14:23 +01:00
* fix: Issue with Parameters Passed in the URL(#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 <anoop.md1421@gmail.com>
This commit is contained in:
parent
36ef38be6a
commit
93080de2a8
@ -18,16 +18,17 @@ const hasLength = (str) => {
|
||||
};
|
||||
|
||||
export const parseQueryParams = (query) => {
|
||||
try {
|
||||
if (!query || !query.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let params = query.split('&').map((param) => {
|
||||
let [name, value = ''] = param.split('=');
|
||||
return { name, value };
|
||||
});
|
||||
|
||||
return filter(params, (p) => hasLength(p.name));
|
||||
return Array.from(new URLSearchParams(query.split('#')[0]).entries())
|
||||
.map(([name, value]) => ({ name, value }));
|
||||
} catch (error) {
|
||||
console.error('Error parsing query params:', error);
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
export const parsePathParams = (url) => {
|
||||
|
@ -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', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user