Skip to content

Commit

Permalink
ok, fix this now
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Oct 27, 2023
1 parent ff97580 commit d084e3f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
33 changes: 32 additions & 1 deletion src/__tests__/request-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _HTTPBuildQuery, _isUrlMatchingRegex } from '../utils/request-utils'
import { _getQueryParam, _HTTPBuildQuery, _isUrlMatchingRegex } from '../utils/request-utils'

describe('request utils', () => {
describe('_HTTPBuildQuery', () => {
Expand All @@ -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],
Expand Down
18 changes: 10 additions & 8 deletions src/utils/request-utils.ts
Original file line number Diff line number Diff line change
@@ -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']

Expand Down Expand Up @@ -29,13 +29,15 @@ export const _HTTPBuildQuery = function (formdata: Record<string, any>, 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]
Expand Down

0 comments on commit d084e3f

Please sign in to comment.