Beispiel #1
0
 def test_escape(self):
     data = "\t\n\r !\"#$%&'()*+,-./0123456789:;<=>?@"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
     escaped_normal = "%09%0A%0D%20!%22%23%24%25%26'()*%2B%2C-.%2F"\
         "0123456789%3A%3B%3C%3D%3E%3F%40"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     escaped_max = "%09%0A%0D%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D"\
         "%2E%2F0123456789%3A%3B%3C%3D%3E%3F%40"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E%5F%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E"
     escaped_min = "%09%0A%0D%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ[%5C]%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     escaped_min_2396 = "%09%0A%0D%20!%22%23$%25&'()*+,-./0123456789:;%3C"\
         "=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     self.compare_strings(escaped_normal, uri.escape_data(data),
                          "Normal escaping")
     self.compare_strings(
         escaped_max, uri.escape_data(data,
                                      lambda x: not uri.is_alphanum(x)),
         "Max escaping")
     self.compare_strings(escaped_min,
                          uri.escape_data(data,
                                          lambda x: False), "Min escaping")
     self.compare_strings(
         escaped_min_2396,
         uri.escape_data(data,
                         lambda x: False,
                         allowed_test=uri.is_allowed_2396), "Min escaping")
     self.compare_strings(
         data,
         uri.unescape_data(uri.escape_data(data)).decode('ascii'),
         "Round-trip escaping")
     try:
         uri.escape_data(ul('Caf\xe9'))
         self.fail("non-ASCII character for escaping")
     except UnicodeEncodeError:
         pass
     # byte strings can contain any data, no UTF-8 encoding is done
     self.assertTrue(uri.escape_data(b'Caf\xe9') == "Caf%E9")
Beispiel #2
0
 def test_escape(self):
     data = "\t\n\r !\"#$%&'()*+,-./0123456789:;<=>?@"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
     escaped_normal = "%09%0A%0D%20!%22%23%24%25%26'()*%2B%2C-.%2F"\
         "0123456789%3A%3B%3C%3D%3E%3F%40"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     escaped_max = "%09%0A%0D%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D"\
         "%2E%2F0123456789%3A%3B%3C%3D%3E%3F%40"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E%5F%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E"
     escaped_min = "%09%0A%0D%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@"\
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ[%5C]%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     escaped_min_2396 = "%09%0A%0D%20!%22%23$%25&'()*+,-./0123456789:;%3C"\
         "=%3E?@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60"\
         "abcdefghijklmnopqrstuvwxyz%7B%7C%7D~"
     self.compare_strings(
         escaped_normal, uri.escape_data(data), "Normal escaping")
     self.compare_strings(escaped_max, uri.escape_data(data,
                          lambda x: not uri.is_alphanum(x)),
                          "Max escaping")
     self.compare_strings(
         escaped_min, uri.escape_data(data, lambda x: False),
         "Min escaping")
     self.compare_strings(
         escaped_min_2396, uri.escape_data(
             data, lambda x: False, allowed_test=uri.is_allowed_2396),
         "Min escaping")
     self.compare_strings(
         data, uri.unescape_data(uri.escape_data(data)).decode('ascii'),
         "Round-trip escaping")
     try:
         uri.escape_data(ul('Caf\xe9'))
         self.fail("non-ASCII character for escaping")
     except UnicodeEncodeError:
         pass
     # byte strings can contain any data, no UTF-8 encoding is done
     self.assertTrue(uri.escape_data(b'Caf\xe9') == "Caf%E9")
Beispiel #3
0
    def test_basics(self):
        """Tests for basic character classes.

        alpha = lowalpha | upalpha
        lowalpha = "a" | ... | "z"
        upalpha  = "A" | ... | "Z"
        digit = "0" | ... | "9"
        alphanum = alpha | digit
        reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
        unreserved  = alphanum | mark
        mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
        """
        # UPALPHA = <any US-ASCII uppercase letter "A".."Z">
        upalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_upalpha(c) == (c in upalpha),
                "is_upalpha(chr(%i))" % i)
        lowalpha = "abcdefghijklmnopqrstuvwxyz"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_lowalpha(c) == (c in lowalpha),
                            "is_lowalpha(chr(%i))" % i)
        alpha = upalpha + lowalpha
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_alpha(c) == (c in alpha),
                            "is_alpha(chr(%i))" % i)
        digit = "0123456789"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_digit(c) == (c in digit),
                            "is_digit(chr(%i))" % i)
        alphanum = alpha + digit
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_alphanum(c) == (c in alphanum),
                            "is_alphanum(chr(%i))" % i)
        reserved = ";/?:@&=+$,"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_reserved_2396(c) == (c in reserved),
                            "is_reserved_2396(chr(%i))" % i)
        reserved = ";/?:@&=+$,[]"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_reserved(c) == (c in reserved),
                            "is_reserved(chr(%i))" % i)
        mark = "-_.!~*'()"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_mark(c) == (c in mark),
                            "is_mark(chr(%i))" % i)
        unreserved = alphanum + mark
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_unreserved(c) == (c in unreserved),
                            "is_unreserved(chr(%i))" % i)
        control = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"\
            "\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C"\
            "\x1D\x1E\x1F\x7F"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_control(c) == (c in control), "is_control(chr(%i))" % i)
        space = " "
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_space(c) == (c in space),
                            "is_space(chr(%i))" % i)
        delims = "<>#%\""
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_delims(c) == (c in delims), "is_delims(chr(%i))" % i)
        unwise_2396 = "{}|\\^[]`"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_unwise_2396(c) == (c in unwise_2396),
                "is_unwise_2396(chr(%i))" % i)
        unwise = "{}|\\^`"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_unwise(c) == (c in unwise), "is_unwise(chr(%i))" % i)
        authority_reserved = ";:@?/"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_authority_reserved(c) == (
                c in authority_reserved), "is_authority_reserved(chr(%i))" % i)
        path_segment_reserved = "/;=?"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_path_segment_reserved(c) ==
                            (c in path_segment_reserved),
                            "is_path_segment_reserved(chr(%i))" % i)
        query_reserved = ";/?:@&=+,$"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_query_reserved(c) == (c in query_reserved),
                            "is_query_reserved(chr(%i))" % i)
Beispiel #4
0
    def test_basics(self):
        """Tests for basic character classes.

        alpha = lowalpha | upalpha
        lowalpha = "a" | ... | "z"
        upalpha  = "A" | ... | "Z"
        digit = "0" | ... | "9"
        alphanum = alpha | digit
        reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" | "$" | ","
        unreserved  = alphanum | mark
        mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
        """
        # UPALPHA = <any US-ASCII uppercase letter "A".."Z">
        upalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_upalpha(c) == (c in upalpha),
                "is_upalpha(chr(%i))" % i)
        lowalpha = "abcdefghijklmnopqrstuvwxyz"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_lowalpha(c) == (c in lowalpha),
                            "is_lowalpha(chr(%i))" % i)
        alpha = upalpha + lowalpha
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_alpha(c) == (c in alpha),
                            "is_alpha(chr(%i))" % i)
        digit = "0123456789"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_digit(c) == (c in digit),
                            "is_digit(chr(%i))" % i)
        alphanum = alpha + digit
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_alphanum(c) == (c in alphanum),
                            "is_alphanum(chr(%i))" % i)
        reserved = ";/?:@&=+$,"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_reserved_2396(c) == (c in reserved),
                            "is_reserved_2396(chr(%i))" % i)
        reserved = ";/?:@&=+$,[]"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_reserved(c) == (c in reserved),
                            "is_reserved(chr(%i))" % i)
        mark = "-_.!~*'()"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_mark(c) == (c in mark),
                            "is_mark(chr(%i))" % i)
        unreserved = alphanum + mark
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_unreserved(c) == (c in unreserved),
                            "is_unreserved(chr(%i))" % i)
        control = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D"\
            "\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C"\
            "\x1D\x1E\x1F\x7F"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_control(c) == (c in control), "is_control(chr(%i))" % i)
        space = " "
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_space(c) == (c in space),
                            "is_space(chr(%i))" % i)
        delims = "<>#%\""
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_delims(c) == (c in delims), "is_delims(chr(%i))" % i)
        unwise_2396 = "{}|\\^[]`"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_unwise_2396(c) == (c in unwise_2396),
                "is_unwise_2396(chr(%i))" % i)
        unwise = "{}|\\^`"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(
                uri.is_unwise(c) == (c in unwise), "is_unwise(chr(%i))" % i)
        authority_reserved = ";:@?/"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_authority_reserved(c) == (
                c in authority_reserved), "is_authority_reserved(chr(%i))" % i)
        path_segment_reserved = "/;=?"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_path_segment_reserved(c) ==
                            (c in path_segment_reserved),
                            "is_path_segment_reserved(chr(%i))" % i)
        query_reserved = ";/?:@&=+,$"
        for i in range3(0, 256):
            c = character(i)
            self.assertTrue(uri.is_query_reserved(c) == (c in query_reserved),
                            "is_query_reserved(chr(%i))" % i)