Skip to content

Commit

Permalink
test_url_query: Add tests for double-decoding of query values
Browse files Browse the repository at this point in the history
Add test for the problem we're trying to solve in encoding and decoding
query values. (aio-libsGH-210)

Also drop FIXME for the case of `a` vs `a=` in query string, as we don't
have plans to differentiate beetween them for the time being.
  • Loading branch information
besfahbod committed Aug 15, 2018
1 parent f128d31 commit ec69bfe
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions tests/test_url_query.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List, Tuple # noqa: F401
from urllib.parse import urlencode
from urllib.parse import urlencode, quote

import pytest
from multidict import MultiDict, MultiDictProxy
Expand All @@ -12,8 +12,6 @@
# ========================================

URLS_WITH_BASIC_QUERY_VALUES = [

# Empty strings, keys and values
(
URL('http://example.com'),
MultiDict(),
Expand All @@ -26,8 +24,9 @@
URL('http://example.com?a='),
MultiDict([('a', '')]),
),
] # type: List[Tuple[URL, MultiDict]]

# ASCII chars
URLS_WITH_ASCII_QUERY_VALUES = [
(
# TODO: Double check if key is expected as `a b` or `a+b`.
URL('http://example.com?a+b=c+d'),
Expand All @@ -41,8 +40,10 @@
URL('http://example.com?a=1&b=2&a=3'),
MultiDict([('a', '1'), ('b', '2'), ('a', '3')]),
),
] # type: List[Tuple[URL, MultiDict]]

# Non-ASCI BMP chars
URLS_WITH_NON_ASCII_QUERY_VALUES = [
# BMP chars
(
URL('http://example.com?ключ=знач'),
MultiDict({'ключ': 'знач'}),
Expand All @@ -62,7 +63,9 @@

@pytest.mark.parametrize(
'original_url, expected_query',
URLS_WITH_BASIC_QUERY_VALUES,
URLS_WITH_BASIC_QUERY_VALUES
+ URLS_WITH_ASCII_QUERY_VALUES
+ URLS_WITH_NON_ASCII_QUERY_VALUES,
)
def test_query_basic_parsing(original_url, expected_query):
assert isinstance(original_url.query, MultiDictProxy)
Expand All @@ -71,13 +74,11 @@ def test_query_basic_parsing(original_url, expected_query):

@pytest.mark.parametrize(
'original_url, expected_query',
URLS_WITH_BASIC_QUERY_VALUES,
URLS_WITH_ASCII_QUERY_VALUES + URLS_WITH_NON_ASCII_QUERY_VALUES,
)
def test_query_basic_update_query(original_url, expected_query):
new_url = original_url.update_query({})
# FIXME: `?a` becomes `?a=` right now. Maybe support `None` values?
# assert new_url == original_url
assert new_url is not None
assert new_url == original_url


def test_query_dont_unqoute_twice():
Expand Down Expand Up @@ -167,3 +168,34 @@ def test_query_from_empty_update_query(

# FIXME: Broken because of asymmetric query encoding
# assert new_url == original_url


# ========================================
# Setting and getting query values
# ========================================

@pytest.mark.parametrize('original_url', [
URL('http://example.com'),
])
def test_query_set_encoded_url_as_value(original_url):
new_url_1 = original_url.update_query({'foo': '𝕦𝕟𝕚'})
assert '𝕦𝕟𝕚' == new_url_1.query['foo']

new_url_1_str = str(new_url_1)
assert (
'http://example.com/?foo=%F0%9D%95%A6%F0%9D%95%9F%F0%9D%95%9A'
) == new_url_1_str

new_url_2 = original_url.update_query({'bar': new_url_1_str})
# FIXME: Double-decoding query value
# assert new_url_1_str == new_url_2.query['bar']
assert 'http://example.com/?foo=𝕦𝕟𝕚' == new_url_2.query['bar']

new_url_2_str = str(new_url_2)
# FIXME: Double-decoding query value
# assert (
# 'http://example.com/?bar=http%3A//example.com/%3Ffoo%3D%25F0%259D%2595%25A6%25F0%259D%2595%259F%25F0%259D%2595%259A'
# ) == new_url_2_str
assert (
'http://example.com/?bar=http://example.com/?foo%3D%F0%9D%95%A6%F0%9D%95%9F%F0%9D%95%9A'
) == new_url_2_str

0 comments on commit ec69bfe

Please sign in to comment.