Skip to content

Commit

Permalink
bugfix/request-url-query-split-error-when-contains-no-equal (#782)
Browse files Browse the repository at this point in the history
* Fix request url query split error when query contains no equal

* Add unit test

* remove useless code

* update unit test
  • Loading branch information
yumiguan authored Aug 30, 2023
1 parent 0074844 commit 3a53db4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lyrebird/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,28 @@ def render(data, enable_tojson=True):


def get_query_array(url):
# query string to array
# like a=1&b=2 ==> [a, 1, b, 2]
# query string to array, example:
# a=1&b=2 ==> ['a', '1', 'b', '2']
# a=&b=2 ==> ['a', '', 'b', '2']
# a&b=2 ==> ['a', '', 'b', '2']
query_array = []
qs_index = url.find('?')
if qs_index >= 0:
query_string = url[qs_index+1:]
query_array = re.split('\\&|\\=', query_string)
if qs_index < 0:
return query_array

query_string = url[qs_index+1:]
if not query_string:
return query_array

for k_v in query_string.split('&'):
if not k_v:
continue

k_v_item = k_v.split('=')
if len(k_v_item) >= 2:
query_array.extend(k_v_item[:2])
else:
query_array.extend([k_v, ''])
return query_array


Expand Down
22 changes: 22 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,25 @@ def test_case_insenstive_dict():
test_dict = utils.CaseInsensitiveDict({'content-type':'LBTests'})
assert test_dict.get('Content-Type') == 'LBTests'
assert test_dict.get('content-type') == 'LBTests'


def test_get_query_array():
url_no_query = 'http://www.meituan.com'
url_no_query_with_question_mark = 'http://www.meituan.com?'
target_res = []
assert utils.get_query_array(url_no_query) == target_res
assert utils.get_query_array(url_no_query_with_question_mark) == target_res

url_query_normal = 'http://www.meituan.com?a=1&b=2'
url_query_multiple_equals_mark = 'http://www.meituan.com?a=1&b=2=3'
url_query_key_value_blank = 'http://www.meituan.com?a=1&b=2&'
target_res = ['a', '1', 'b', '2']
assert utils.get_query_array(url_query_normal) == target_res
assert utils.get_query_array(url_query_multiple_equals_mark) == target_res
assert utils.get_query_array(url_query_key_value_blank) == target_res

url_query_value_blank_no_equals_mark = 'http://www.meituan.com?a=1&b'
url_query_value_blank_one_equals_mark = 'http://www.meituan.com?a=1&b='
target_res = ['a', '1', 'b', '']
assert utils.get_query_array(url_query_value_blank_no_equals_mark) == target_res
assert utils.get_query_array(url_query_value_blank_one_equals_mark) == target_res

0 comments on commit 3a53db4

Please sign in to comment.