Back to index

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

List of all members.

Public Member Functions

def test_never_quote
def test_default_safe
def test_safe
def test_default_quoting
def test_quoting_space
def test_quoting_plus
def test_quote_bytes
def test_quote_with_unicode
def test_quote_plus_with_unicode

Detailed Description

Tests for urllib.quote() and urllib.quote_plus()

According to RFC 2396 (Uniform Resource Identifiers), to escape a
character you write it as '%' + <2 character US-ASCII hex value>.
The Python code of ``'%' + hex(ord(<character>))[2:]`` escapes a
character properly. Case does not matter on the hex letters.

The various character sets specified are:

Reserved characters : ";/?:@&=+$,"
    Have special meaning in URIs and must be escaped if not being used for
    their special meaning
Data characters : letters, digits, and "-_.!~*'()"
    Unreserved and do not need to be escaped; can be, though, if desired
Control characters : 0x00 - 0x1F, 0x7F
    Have no use in URIs so must be escaped
space : 0x20
    Must be escaped
Delimiters : '<>#%"'
    Must be escaped
Unwise : "{}|\^[]`"
    Must be escaped

Definition at line 397 of file test_urllib.py.


Member Function Documentation

Definition at line 469 of file test_urllib.py.

00469 
00470     def test_default_quoting(self):
00471         # Make sure all characters that should be quoted are by default sans
00472         # space (separate test for that).
00473         should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F
00474         should_quote.append('<>#%"{}|\^[]`')
00475         should_quote.append(chr(127)) # For 0x7F
00476         should_quote = ''.join(should_quote)
00477         for char in should_quote:
00478             result = urllib.parse.quote(char)
00479             self.assertEqual(hexescape(char), result,
00480                              "using quote(): "
00481                              "%s should be escaped to %s, not %s" %
00482                              (char, hexescape(char), result))
00483             result = urllib.parse.quote_plus(char)
00484             self.assertEqual(hexescape(char), result,
00485                              "using quote_plus(): "
00486                              "%s should be escapes to %s, not %s" %
00487                              (char, hexescape(char), result))
00488         del should_quote
00489         partial_quote = "ab[]cd"
00490         expected = "ab%5B%5Dcd"
00491         result = urllib.parse.quote(partial_quote)
00492         self.assertEqual(expected, result,
00493                          "using quote(): %r != %r" % (expected, result))
00494         self.assertEqual(expected, result,
00495                          "using quote_plus(): %r != %r" % (expected, result))

Here is the call graph for this function:

Definition at line 436 of file test_urllib.py.

00436 
00437     def test_default_safe(self):
00438         # Test '/' is default value for 'safe' parameter
00439         self.assertEqual(urllib.parse.quote.__defaults__[0], '/')

Here is the call graph for this function:

Definition at line 423 of file test_urllib.py.

00423 
00424     def test_never_quote(self):
00425         # Make sure quote() does not quote letters, digits, and "_,.-"
00426         do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",
00427                                  "abcdefghijklmnopqrstuvwxyz",
00428                                  "0123456789",
00429                                  "_.-"])
00430         result = urllib.parse.quote(do_not_quote)
00431         self.assertEqual(do_not_quote, result,
00432                          "using quote(): %r != %r" % (do_not_quote, result))
00433         result = urllib.parse.quote_plus(do_not_quote)
00434         self.assertEqual(do_not_quote, result,
00435                         "using quote_plus(): %r != %r" % (do_not_quote, result))

Here is the call graph for this function:

Definition at line 527 of file test_urllib.py.

00527 
00528     def test_quote_bytes(self):
00529         # Bytes should quote directly to percent-encoded values
00530         given = b"\xa2\xd8ab\xff"
00531         expect = "%A2%D8ab%FF"
00532         result = urllib.parse.quote(given)
00533         self.assertEqual(expect, result,
00534                          "using quote(): %r != %r" % (expect, result))
00535         # Encoding argument should raise type error on bytes input
00536         self.assertRaises(TypeError, urllib.parse.quote, given,
00537                             encoding="latin-1")
00538         # quote_from_bytes should work the same
00539         result = urllib.parse.quote_from_bytes(given)
00540         self.assertEqual(expect, result,
00541                          "using quote_from_bytes(): %r != %r"
00542                          % (expect, result))

Here is the call graph for this function:

Definition at line 585 of file test_urllib.py.

00585 
00586     def test_quote_plus_with_unicode(self):
00587         # Encoding (latin-1) test for quote_plus
00588         given = "\xa2\xd8 \xff"
00589         expect = "%A2%D8+%FF"
00590         result = urllib.parse.quote_plus(given, encoding="latin-1")
00591         self.assertEqual(expect, result,
00592                          "using quote_plus(): %r != %r" % (expect, result))
00593         # Errors test for quote_plus
00594         given = "ab\u6f22\u5b57 cd"
00595         expect = "ab%3F%3F+cd"
00596         result = urllib.parse.quote_plus(given, encoding="latin-1",
00597                                          errors="replace")
00598         self.assertEqual(expect, result,
00599                          "using quote_plus(): %r != %r" % (expect, result))
00600 

Here is the call graph for this function:

Definition at line 543 of file test_urllib.py.

00543 
00544     def test_quote_with_unicode(self):
00545         # Characters in Latin-1 range, encoded by default in UTF-8
00546         given = "\xa2\xd8ab\xff"
00547         expect = "%C2%A2%C3%98ab%C3%BF"
00548         result = urllib.parse.quote(given)
00549         self.assertEqual(expect, result,
00550                          "using quote(): %r != %r" % (expect, result))
00551         # Characters in Latin-1 range, encoded by with None (default)
00552         result = urllib.parse.quote(given, encoding=None, errors=None)
00553         self.assertEqual(expect, result,
00554                          "using quote(): %r != %r" % (expect, result))
00555         # Characters in Latin-1 range, encoded with Latin-1
00556         given = "\xa2\xd8ab\xff"
00557         expect = "%A2%D8ab%FF"
00558         result = urllib.parse.quote(given, encoding="latin-1")
00559         self.assertEqual(expect, result,
00560                          "using quote(): %r != %r" % (expect, result))
00561         # Characters in BMP, encoded by default in UTF-8
00562         given = "\u6f22\u5b57"              # "Kanji"
00563         expect = "%E6%BC%A2%E5%AD%97"
00564         result = urllib.parse.quote(given)
00565         self.assertEqual(expect, result,
00566                          "using quote(): %r != %r" % (expect, result))
00567         # Characters in BMP, encoded with Latin-1
00568         given = "\u6f22\u5b57"
00569         self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,
00570                                     encoding="latin-1")
00571         # Characters in BMP, encoded with Latin-1, with replace error handling
00572         given = "\u6f22\u5b57"
00573         expect = "%3F%3F"                   # "??"
00574         result = urllib.parse.quote(given, encoding="latin-1",
00575                                     errors="replace")
00576         self.assertEqual(expect, result,
00577                          "using quote(): %r != %r" % (expect, result))
00578         # Characters in BMP, Latin-1, with xmlcharref error handling
00579         given = "\u6f22\u5b57"
00580         expect = "%26%2328450%3B%26%2323383%3B"     # "&#28450;&#23383;"
00581         result = urllib.parse.quote(given, encoding="latin-1",
00582                                     errors="xmlcharrefreplace")
00583         self.assertEqual(expect, result,
00584                          "using quote(): %r != %r" % (expect, result))

Here is the call graph for this function:

Definition at line 515 of file test_urllib.py.

00515 
00516     def test_quoting_plus(self):
00517         self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),
00518                          'alpha%2Bbeta+gamma')
00519         self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),
00520                          'alpha+beta+gamma')
00521         # Test with bytes
00522         self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),
00523                          'alpha%2Bbeta+gamma')
00524         # Test with safe bytes
00525         self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),
00526                          'alpha+beta+gamma')

Here is the call graph for this function:

Definition at line 496 of file test_urllib.py.

00496 
00497     def test_quoting_space(self):
00498         # Make sure quote() and quote_plus() handle spaces as specified in
00499         # their unique way
00500         result = urllib.parse.quote(' ')
00501         self.assertEqual(result, hexescape(' '),
00502                          "using quote(): %r != %r" % (result, hexescape(' ')))
00503         result = urllib.parse.quote_plus(' ')
00504         self.assertEqual(result, '+',
00505                          "using quote_plus(): %r != +" % result)
00506         given = "a b cd e f"
00507         expect = given.replace(' ', hexescape(' '))
00508         result = urllib.parse.quote(given)
00509         self.assertEqual(expect, result,
00510                          "using quote(): %r != %r" % (expect, result))
00511         expect = given.replace(' ', '+')
00512         result = urllib.parse.quote_plus(given)
00513         self.assertEqual(expect, result,
00514                          "using quote_plus(): %r != %r" % (expect, result))

Here is the call graph for this function:

Definition at line 440 of file test_urllib.py.

00440 
00441     def test_safe(self):
00442         # Test setting 'safe' parameter does what it should do
00443         quote_by_default = "<>"
00444         result = urllib.parse.quote(quote_by_default, safe=quote_by_default)
00445         self.assertEqual(quote_by_default, result,
00446                          "using quote(): %r != %r" % (quote_by_default, result))
00447         result = urllib.parse.quote_plus(quote_by_default,
00448                                          safe=quote_by_default)
00449         self.assertEqual(quote_by_default, result,
00450                          "using quote_plus(): %r != %r" %
00451                          (quote_by_default, result))
00452         # Safe expressed as bytes rather than str
00453         result = urllib.parse.quote(quote_by_default, safe=b"<>")
00454         self.assertEqual(quote_by_default, result,
00455                          "using quote(): %r != %r" % (quote_by_default, result))
00456         # "Safe" non-ASCII characters should have no effect
00457         # (Since URIs are not allowed to have non-ASCII characters)
00458         result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")
00459         expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
00460         self.assertEqual(expect, result,
00461                          "using quote(): %r != %r" %
00462                          (expect, result))
00463         # Same as above, but using a bytes rather than str
00464         result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")
00465         expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")
00466         self.assertEqual(expect, result,
00467                          "using quote(): %r != %r" %
00468                          (expect, result))

Here is the call graph for this function:


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