def parse_arguments(self): """ Parses the arguments, using the currently defined arguments string (in the request). The parsing of the arguments is based in the default get arguments parsing. """ # retrieves the attribute fields list attribute_fields_list = self.arguments.split("&") # iterates over all the attribute fields for attribute_field in attribute_fields_list: # splits the attribute field in the equals operator # and retrieves the length of the result for processing attribute_field_splitted = attribute_field.split("=", 1) attribute_field_splitted_length = len(attribute_field_splitted) # in case the attribute field splitted length is invalid, # must continue the loop (invalid value) if attribute_field_splitted_length == 0 or attribute_field_splitted_length > 2: continue # in case the attribute field splitted length is two, this # refers a valid (normal) key and value attribute if attribute_field_splitted_length == 2: # retrieves the attribute name and the attribute value, # from the attribute field splitted, then "unquotes" the # attribute value from the url encoding attribute_name, attribute_value = attribute_field_splitted attribute_value = colony.unquote_plus(attribute_value) # in case the attribute field splitted length is one, this refers # a valid single key attribute (with an unset value) elif attribute_field_splitted_length == 1: # retrieves the attribute name, from the attribute field splitted # and sets the value as invalid (not set) attribute_name, = attribute_field_splitted attribute_value = None # "unquotes" the attribute name from the url encoding and sets # the attribute for the current name in the current instance attribute_name = colony.unquote_plus(attribute_name) self.__setattributes__(attribute_name, attribute_value)
def test_unquote_plus(self): """ Plus version testing of the unquoting operation, this test is analogous to the previous test. """ result = colony.unquote_plus("Hello+World") self.assertEqual(result, "Hello World") result = colony.unquote_plus("Ol%C3%A1+Mundo") self.assertEqual(result, "Olá Mundo") result = colony.unquote_plus("%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C") self.assertEqual(result, "你好世界") result = colony.unquote_plus("Hello+World%GG") self.assertEqual(result, "Hello World%GG") result = colony.unquote_plus("你好世界", strict = False) self.assertEqual(result, "你好世界") result = colony.unquote_plus(b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c", strict = False) self.assertEqual(result, "你好世界") result = colony.unquote_plus(colony.legacy.u("你好世界"), strict = False) self.assertEqual(result, "你好世界") self.assert_raises( UnicodeDecodeError, colony.unquote_plus, b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c", strict = True ) self.assert_raises( UnicodeEncodeError, colony.unquote_plus, colony.legacy.u("你好世界"), strict = True )