Back to index

python3.2  3.2.2
Public Member Functions
test.test_urllib.UnquotingTests Class Reference

List of all members.

Public Member Functions

def test_unquoting
def test_unquoting_badpercent
def test_unquoting_mixed_case
def test_unquoting_parts
def test_unquoting_plus
def test_unquote_to_bytes
def test_unquote_with_unicode

Detailed Description

Tests for unquote() and unquote_plus()

See the doc string for quoting_Tests for details on quoting and such.

Definition at line 601 of file test_urllib.py.


Member Function Documentation

Definition at line 702 of file test_urllib.py.

00702 
00703     def test_unquote_to_bytes(self):
00704         given = 'br%C3%BCckner_sapporo_20050930.doc'
00705         expect = b'br\xc3\xbcckner_sapporo_20050930.doc'
00706         result = urllib.parse.unquote_to_bytes(given)
00707         self.assertEqual(expect, result,
00708                          "using unquote_to_bytes(): %r != %r"
00709                          % (expect, result))
00710         # Test on a string with unescaped non-ASCII characters
00711         # (Technically an invalid URI; expect those characters to be UTF-8
00712         # encoded).
00713         result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")
00714         expect = b'\xe6\xbc\xa2\xc3\xbc'    # UTF-8 for "\u6f22\u00fc"
00715         self.assertEqual(expect, result,
00716                          "using unquote_to_bytes(): %r != %r"
00717                          % (expect, result))
00718         # Test with a bytes as input
00719         given = b'%A2%D8ab%FF'
00720         expect = b'\xa2\xd8ab\xff'
00721         result = urllib.parse.unquote_to_bytes(given)
00722         self.assertEqual(expect, result,
00723                          "using unquote_to_bytes(): %r != %r"
00724                          % (expect, result))
00725         # Test with a bytes as input, with unescaped non-ASCII bytes
00726         # (Technically an invalid URI; expect those bytes to be preserved)
00727         given = b'%A2\xd8ab%FF'
00728         expect = b'\xa2\xd8ab\xff'
00729         result = urllib.parse.unquote_to_bytes(given)
00730         self.assertEqual(expect, result,
00731                          "using unquote_to_bytes(): %r != %r"
00732                          % (expect, result))

Here is the call graph for this function:

Definition at line 733 of file test_urllib.py.

00733 
00734     def test_unquote_with_unicode(self):
00735         # Characters in the Latin-1 range, encoded with UTF-8
00736         given = 'br%C3%BCckner_sapporo_20050930.doc'
00737         expect = 'br\u00fcckner_sapporo_20050930.doc'
00738         result = urllib.parse.unquote(given)
00739         self.assertEqual(expect, result,
00740                          "using unquote(): %r != %r" % (expect, result))
00741         # Characters in the Latin-1 range, encoded with None (default)
00742         result = urllib.parse.unquote(given, encoding=None, errors=None)
00743         self.assertEqual(expect, result,
00744                          "using unquote(): %r != %r" % (expect, result))
00745 
00746         # Characters in the Latin-1 range, encoded with Latin-1
00747         result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',
00748                                       encoding="latin-1")
00749         expect = 'br\u00fcckner_sapporo_20050930.doc'
00750         self.assertEqual(expect, result,
00751                          "using unquote(): %r != %r" % (expect, result))
00752 
00753         # Characters in BMP, encoded with UTF-8
00754         given = "%E6%BC%A2%E5%AD%97"
00755         expect = "\u6f22\u5b57"             # "Kanji"
00756         result = urllib.parse.unquote(given)
00757         self.assertEqual(expect, result,
00758                          "using unquote(): %r != %r" % (expect, result))
00759 
00760         # Decode with UTF-8, invalid sequence
00761         given = "%F3%B1"
00762         expect = "\ufffd"                   # Replacement character
00763         result = urllib.parse.unquote(given)
00764         self.assertEqual(expect, result,
00765                          "using unquote(): %r != %r" % (expect, result))
00766 
00767         # Decode with UTF-8, invalid sequence, replace errors
00768         result = urllib.parse.unquote(given, errors="replace")
00769         self.assertEqual(expect, result,
00770                          "using unquote(): %r != %r" % (expect, result))
00771 
00772         # Decode with UTF-8, invalid sequence, ignoring errors
00773         given = "%F3%B1"
00774         expect = ""
00775         result = urllib.parse.unquote(given, errors="ignore")
00776         self.assertEqual(expect, result,
00777                          "using unquote(): %r != %r" % (expect, result))
00778 
00779         # A mix of non-ASCII and percent-encoded characters, UTF-8
00780         result = urllib.parse.unquote("\u6f22%C3%BC")
00781         expect = '\u6f22\u00fc'
00782         self.assertEqual(expect, result,
00783                          "using unquote(): %r != %r" % (expect, result))
00784 
00785         # A mix of non-ASCII and percent-encoded characters, Latin-1
00786         # (Note, the string contains non-Latin-1-representable characters)
00787         result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")
00788         expect = '\u6f22\u00fc'
00789         self.assertEqual(expect, result,
00790                          "using unquote(): %r != %r" % (expect, result))

Here is the call graph for this function:

Definition at line 608 of file test_urllib.py.

00608 
00609     def test_unquoting(self):
00610         # Make sure unquoting of all ASCII values works
00611         escape_list = []
00612         for num in range(128):
00613             given = hexescape(chr(num))
00614             expect = chr(num)
00615             result = urllib.parse.unquote(given)
00616             self.assertEqual(expect, result,
00617                              "using unquote(): %r != %r" % (expect, result))
00618             result = urllib.parse.unquote_plus(given)
00619             self.assertEqual(expect, result,
00620                              "using unquote_plus(): %r != %r" %
00621                              (expect, result))
00622             escape_list.append(given)
00623         escape_string = ''.join(escape_list)
00624         del escape_list
00625         result = urllib.parse.unquote(escape_string)
00626         self.assertEqual(result.count('%'), 1,
00627                          "using unquote(): not all characters escaped: "
00628                          "%s" % result)
00629         self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)
00630         self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())
00631         with support.check_warnings(('', BytesWarning), quiet=True):
00632             self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')

Here is the call graph for this function:

Definition at line 633 of file test_urllib.py.

00633 
00634     def test_unquoting_badpercent(self):
00635         # Test unquoting on bad percent-escapes
00636         given = '%xab'
00637         expect = given
00638         result = urllib.parse.unquote(given)
00639         self.assertEqual(expect, result, "using unquote(): %r != %r"
00640                          % (expect, result))
00641         given = '%x'
00642         expect = given
00643         result = urllib.parse.unquote(given)
00644         self.assertEqual(expect, result, "using unquote(): %r != %r"
00645                          % (expect, result))
00646         given = '%'
00647         expect = given
00648         result = urllib.parse.unquote(given)
00649         self.assertEqual(expect, result, "using unquote(): %r != %r"
00650                          % (expect, result))
00651         # unquote_to_bytes
00652         given = '%xab'
00653         expect = bytes(given, 'ascii')
00654         result = urllib.parse.unquote_to_bytes(given)
00655         self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
00656                          % (expect, result))
00657         given = '%x'
00658         expect = bytes(given, 'ascii')
00659         result = urllib.parse.unquote_to_bytes(given)
00660         self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
00661                          % (expect, result))
00662         given = '%'
00663         expect = bytes(given, 'ascii')
00664         result = urllib.parse.unquote_to_bytes(given)
00665         self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"
00666                          % (expect, result))
00667         self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)
00668         self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())

Here is the call graph for this function:

Definition at line 669 of file test_urllib.py.

00669 
00670     def test_unquoting_mixed_case(self):
00671         # Test unquoting on mixed-case hex digits in the percent-escapes
00672         given = '%Ab%eA'
00673         expect = b'\xab\xea'
00674         result = urllib.parse.unquote_to_bytes(given)
00675         self.assertEqual(expect, result,
00676                          "using unquote_to_bytes(): %r != %r"
00677                          % (expect, result))

Here is the call graph for this function:

Definition at line 678 of file test_urllib.py.

00678 
00679     def test_unquoting_parts(self):
00680         # Make sure unquoting works when have non-quoted characters
00681         # interspersed
00682         given = 'ab%sd' % hexescape('c')
00683         expect = "abcd"
00684         result = urllib.parse.unquote(given)
00685         self.assertEqual(expect, result,
00686                          "using quote(): %r != %r" % (expect, result))
00687         result = urllib.parse.unquote_plus(given)
00688         self.assertEqual(expect, result,
00689                          "using unquote_plus(): %r != %r" % (expect, result))

Here is the call graph for this function:

Definition at line 690 of file test_urllib.py.

00690 
00691     def test_unquoting_plus(self):
00692         # Test difference between unquote() and unquote_plus()
00693         given = "are+there+spaces..."
00694         expect = given
00695         result = urllib.parse.unquote(given)
00696         self.assertEqual(expect, result,
00697                          "using unquote(): %r != %r" % (expect, result))
00698         expect = given.replace('+', ' ')
00699         result = urllib.parse.unquote_plus(given)
00700         self.assertEqual(expect, result,
00701                          "using unquote_plus(): %r != %r" % (expect, result))

Here is the call graph for this function:


The documentation for this class was generated from the following file: