From d084e3f405c467c3a4e83502df0798d4a336355a Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Fri, 27 Oct 2023 13:43:53 +0100 Subject: [PATCH] ok, fix this now --- src/__tests__/request-utils.test.ts | 33 ++++++++++++++++++++++++++++- src/utils/request-utils.ts | 18 +++++++++------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/__tests__/request-utils.test.ts b/src/__tests__/request-utils.test.ts index 5c9e487ee..9472e4e91 100644 --- a/src/__tests__/request-utils.test.ts +++ b/src/__tests__/request-utils.test.ts @@ -1,4 +1,4 @@ -import { _HTTPBuildQuery, _isUrlMatchingRegex } from '../utils/request-utils' +import { _getQueryParam, _HTTPBuildQuery, _isUrlMatchingRegex } from '../utils/request-utils' describe('request utils', () => { describe('_HTTPBuildQuery', () => { @@ -10,6 +10,37 @@ describe('request utils', () => { expect(_HTTPBuildQuery(formData)).toEqual(expected) }) }) + + describe('_getQueryParam', () => { + test.each([ + ['gets query param', '?name=something', 'name', 'something'], + [ + 'gets first query param when multiple matches', + '?name=something&name=another&name=third', + 'name', + 'something', + ], + ['gets query param where there is no key in the URL', '?=something', 'name', ''], + ['gets query param where there is no value in the URL', '?name=', 'name', ''], + ['gets query param where there is no value or = in the URL', '?name', 'name', ''], + ['ignores params after the hash', '?name=something#hash?invalid=here', 'invalid', ''], + ['ignores params after the hash even with no valid query params', '#hash?invalid=here', 'invalid', ''], + ['decodes query param spaces', '?name=something%20encoded', 'name', 'something encoded'], + ['decodes query param ampersand', '?name=something%26encoded', 'name', 'something&encoded'], + ['decodes query param with +', '?name=something+encoded', 'name', 'something encoded'], + ['ignores invalid encoding', '?name=something%2Gencoded', 'name', 'something%2Gencoded'], + ['gets query param with trailing slash', '/?name=something', 'name', 'something'], + ['gets first query param with multiple params', '/?name=something&test=123', 'name', 'something'], + ['gets second query param with multiple params', '/?name=something&test=123', 'test', '123'], + ['gets param when no match', '', 'name', ''], + ['gets param when no match with trailing slash', '/', 'name', ''], + ['gets param when no match and there are params', '/?test=123', 'name', ''], + ['gets param when no match and there are params with trailing slash', '/?test=123', 'name', ''], + ])('%s', (_name, url, param, expected) => { + expect(_getQueryParam(`https://example.com${url}`, param)).toEqual(expected) + }) + }) + describe('_isUrlMatchingRegex', () => { test.each([ ['match query params', 'https://example.com?name=something', '(\\?|\\&)(name.*)\\=([^&]+)', true], diff --git a/src/utils/request-utils.ts b/src/utils/request-utils.ts index a140fbcad..0c64475ef 100644 --- a/src/utils/request-utils.ts +++ b/src/utils/request-utils.ts @@ -1,6 +1,6 @@ import { _each, _isValidRegex, logger } from './' -import { _isNull, _isString, _isUndefined } from './type-utils' +import { _isArray, _isUndefined } from './type-utils' const localDomains = ['localhost', '127.0.0.1'] @@ -29,13 +29,15 @@ export const _HTTPBuildQuery = function (formdata: Record, arg_sepa } export const _getQueryParam = function (url: string, param: string): string { - // Expects a raw URL - - const cleanParam = param.replace(/[[]/, '\\[').replace(/[\]]/, '\\]') - const regexS = '[\\?&]' + cleanParam + '=([^&#]*)' - const regex = new RegExp(regexS) - const results = regex.exec(url) - if (_isNull(results) || (results && !_isString(results[1]) && (results[1] as any).length)) { + const withoutHash: string = url.split('#')[0] || '' + const queryParams: string = withoutHash.split('?')[1] || '' + const results = + queryParams + .split('&') + .map((part) => part.split('=')) + .find(([key]) => key === param) || [] + + if (!_isArray(results) || results.length < 2) { return '' } else { let result = results[1]