Esempio n. 1
0
 def assert_decode_error(query_string, err):
     try:
         decode_query_string(query_string)
     except ValueError as exc:
         self.assertEqual(str(exc), err)
     else:
         self.fail()
Esempio n. 2
0
    def test_decode_query_string_degenerate(self): # pylint: disable=invalid-name

        def assert_decode_error(query_string, err):
            try:
                decode_query_string(query_string)
            except ValueError as exc:
                self.assertEqual(str(exc), err)
            else:
                self.fail()

        # Key with no equal - assume empty string
        query_string = 'a=7&b'
        assert_decode_error(query_string, "Invalid key/value pair 'b'")

        # Empty string key
        query_string = 'a=7&=b'
        obj = {'a': '7', '': 'b'}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string key and value
        query_string = 'a=7&='
        obj = {'a': '7', '': ''}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string key and value with space
        query_string = 'a=7& = '
        obj = {'a': '7', ' ': ' '}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string key with no equal
        query_string = 'a=7&'
        obj = {'a': '7'}
        self.assertEqual(decode_query_string(query_string), obj)

        # Multiple empty string key with no equal
        query_string = 'a&b'
        assert_decode_error(query_string, "Invalid key/value pair 'a'")

        # Multiple empty string key with no equal
        query_string = 'a&b&c'
        assert_decode_error(query_string, "Invalid key/value pair 'a'")

        # Multiple empty string key/value
        query_string = '&'
        obj = {}
        self.assertEqual(decode_query_string(query_string), obj)

        # Multiple empty string key/value
        query_string = '&&'
        obj = {}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string sub-key
        query_string = 'a.=5'
        obj = {'a': {'': '5'}}
        self.assertEqual(decode_query_string(query_string), obj)

        # Duplicate keys
        query_string = 'abc=21&ab=19&abc=17'
        assert_decode_error(query_string, "Duplicate key 'abc=17'")

        # Duplicate index
        query_string = 'a.0=0&a.1=1&a.0=2'
        assert_decode_error(query_string, "Duplicate key 'a.0=2'")

        # Index too large
        query_string = 'a.0=0&a.1=1&a.3=3'
        assert_decode_error(query_string, "Invalid key/value pair 'a.3=3'")

        # Negative index
        query_string = 'a.0=0&a.1=1&a.-3=3'
        assert_decode_error(query_string, "Invalid key/value pair 'a.-3=3'")

        # First list, then dict
        query_string = 'a.0=0&a.b=0'
        assert_decode_error(query_string, "Invalid key/value pair 'a.b=0'")
Esempio n. 3
0
    def test_decode_query_string(self):

        # Complex dict
        query_string = '_a=7&a=7&b.c=%2Bx%20y%20%2B%20z&b.d.0=2&b.d.1=-4&b.d.2=6'
        obj = {'a': '7', '_a': '7', 'b': {'c': '+x y + z', 'd': ['2', '-4', '6']}}
        self.assertEqual(decode_query_string(query_string), obj)

        # Array of dicts
        query_string = 'foo.0.bar=17&foo.0.thud=blue&foo.1.boo=bear'
        obj = {'foo': [{'bar': '17', 'thud': 'blue'}, {'boo': 'bear'}]}
        self.assertEqual(decode_query_string(query_string), obj)

        # Top-level array
        query_string = '0=1&1=2&2=3'
        obj = ['1', '2', '3'] # pylint: disable=redefined-variable-type
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty query string
        query_string = ''
        obj = {}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string value
        query_string = 'b='
        obj = {'b': ''}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string value at end
        query_string = 'a=7&b='
        obj = {'a': '7', 'b': ''}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string value at start
        query_string = 'b=&a=7'
        obj = {'a': '7', 'b': ''}
        self.assertEqual(decode_query_string(query_string), obj)

        # Empty string value in middle
        query_string = 'a=7&b=&c=9'
        obj = {'a': '7', 'b': '', 'c': '9'}
        self.assertEqual(decode_query_string(query_string), obj)

        # Decode keys and values
        query_string = 'a%2eb.c=7%20+%207%20%3d%2014'
        obj = {'a.b': {'c': '7 + 7 = 14'}}
        self.assertEqual(decode_query_string(query_string), obj)

        # Decode unicode string
        query_string = 'a=abc%EA%80%80&b.0=c&b.1=d'
        obj = {'a': 'abc' + chr(40960), 'b': ['c', 'd']}
        self.assertEqual(decode_query_string(query_string), obj)

        # Keys and values with special characters
        query_string = 'a%26b%3Dc%2ed=a%26b%3Dc.d'
        obj = {'a&b=c.d': 'a&b=c.d'}
        self.assertEqual(decode_query_string(query_string), obj)

        # Non-initial-zero array-looking index
        query_string = 'a.1=0'
        obj = {'a': {'1': '0'}}
        self.assertEqual(decode_query_string(query_string), obj)

        # Dictionary first, then array-looking zero index
        query_string = 'a.b=0&a.0=0'
        obj = {'a': {'b': '0', '0': '0'}}
        self.assertEqual(decode_query_string(query_string), obj)