Back to index

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

List of all members.

Public Member Functions

def help_inputtype
def test_using_mapping
def test_using_sequence
def test_quoting
def test_doseq
def test_empty_sequence
def test_nonstring_values
def test_nonstring_seq_values
def test_urlencode_encoding
def test_urlencode_encoding_doseq
def test_urlencode_bytes
def test_urlencode_encoding_safe_parameter

Detailed Description

Tests for urlencode()

Definition at line 791 of file test_urllib.py.


Member Function Documentation

def test.test_urllib.urlencode_Tests.help_inputtype (   self,
  given,
  test_type 
)
Helper method for testing different input types.

'given' must lead to only the pairs:
    * 1st, 1
    * 2nd, 2
    * 3rd, 3

Test cannot assume anything about order.  Docs make no guarantee and
have possible dictionary input.

Definition at line 794 of file test_urllib.py.

00794 
00795     def help_inputtype(self, given, test_type):
00796         """Helper method for testing different input types.
00797 
00798         'given' must lead to only the pairs:
00799             * 1st, 1
00800             * 2nd, 2
00801             * 3rd, 3
00802 
00803         Test cannot assume anything about order.  Docs make no guarantee and
00804         have possible dictionary input.
00805 
00806         """
00807         expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]
00808         result = urllib.parse.urlencode(given)
00809         for expected in expect_somewhere:
00810             self.assertIn(expected, result,
00811                          "testing %s: %s not found in %s" %
00812                          (test_type, expected, result))
00813         self.assertEqual(result.count('&'), 2,
00814                          "testing %s: expected 2 '&'s; got %s" %
00815                          (test_type, result.count('&')))
00816         amp_location = result.index('&')
00817         on_amp_left = result[amp_location - 1]
00818         on_amp_right = result[amp_location + 1]
00819         self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),
00820                      "testing %s: '&' not located in proper place in %s" %
00821                      (test_type, result))
00822         self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps
00823                          "testing %s: "
00824                          "unexpected number of characters: %s != %s" %
00825                          (test_type, len(result), (5 * 3) + 2))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 847 of file test_urllib.py.

00847 
00848     def test_doseq(self):
00849         # Test that passing True for 'doseq' parameter works correctly
00850         given = {'sequence':['1', '2', '3']}
00851         expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))
00852         result = urllib.parse.urlencode(given)
00853         self.assertEqual(expect, result)
00854         result = urllib.parse.urlencode(given, True)
00855         for value in given["sequence"]:
00856             expect = "sequence=%s" % value
00857             self.assertIn(expect, result)
00858         self.assertEqual(result.count('&'), 2,
00859                          "Expected 2 '&'s, got %s" % result.count('&'))

Here is the call graph for this function:

Definition at line 860 of file test_urllib.py.

00860 
00861     def test_empty_sequence(self):
00862         self.assertEqual("", urllib.parse.urlencode({}))
00863         self.assertEqual("", urllib.parse.urlencode([]))

Here is the call graph for this function:

Definition at line 868 of file test_urllib.py.

00868 
00869     def test_nonstring_seq_values(self):
00870         self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))
00871         self.assertEqual("a=None&a=a",
00872                          urllib.parse.urlencode({"a": [None, "a"]}, True))
00873         self.assertEqual("a=a&a=b",
00874                          urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))

Here is the call graph for this function:

Definition at line 864 of file test_urllib.py.

00864 
00865     def test_nonstring_values(self):
00866         self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))
00867         self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))

Here is the call graph for this function:

Definition at line 836 of file test_urllib.py.

00836 
00837     def test_quoting(self):
00838         # Make sure keys and values are quoted using quote_plus()
00839         given = {"&":"="}
00840         expect = "%s=%s" % (hexescape('&'), hexescape('='))
00841         result = urllib.parse.urlencode(given)
00842         self.assertEqual(expect, result)
00843         given = {"key name":"A bunch of pluses"}
00844         expect = "key+name=A+bunch+of+pluses"
00845         result = urllib.parse.urlencode(given)
00846         self.assertEqual(expect, result)

Here is the call graph for this function:

Definition at line 931 of file test_urllib.py.

00931 
00932     def test_urlencode_bytes(self):
00933         given = ((b'\xa0\x24', b'\xc1\x24'),)
00934         expect = '%A0%24=%C1%24'
00935         result = urllib.parse.urlencode(given)
00936         self.assertEqual(expect, result)
00937         result = urllib.parse.urlencode(given, True)
00938         self.assertEqual(expect, result)
00939 
00940         # Sequence of values
00941         given = ((b'\xa0\x24', (42, b'\xc1\x24')),)
00942         expect = '%A0%24=42&%A0%24=%C1%24'
00943         result = urllib.parse.urlencode(given, True)
00944         self.assertEqual(expect, result)

Here is the call graph for this function:

Definition at line 875 of file test_urllib.py.

00875 
00876     def test_urlencode_encoding(self):
00877         # ASCII encoding. Expect %3F with errors="replace'
00878         given = (('\u00a0', '\u00c1'),)
00879         expect = '%3F=%3F'
00880         result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")
00881         self.assertEqual(expect, result)
00882 
00883         # Default is UTF-8 encoding.
00884         given = (('\u00a0', '\u00c1'),)
00885         expect = '%C2%A0=%C3%81'
00886         result = urllib.parse.urlencode(given)
00887         self.assertEqual(expect, result)
00888 
00889         # Latin-1 encoding.
00890         given = (('\u00a0', '\u00c1'),)
00891         expect = '%A0=%C1'
00892         result = urllib.parse.urlencode(given, encoding="latin-1")
00893         self.assertEqual(expect, result)

Here is the call graph for this function:

Definition at line 894 of file test_urllib.py.

00894 
00895     def test_urlencode_encoding_doseq(self):
00896         # ASCII Encoding. Expect %3F with errors="replace'
00897         given = (('\u00a0', '\u00c1'),)
00898         expect = '%3F=%3F'
00899         result = urllib.parse.urlencode(given, doseq=True,
00900                                         encoding="ASCII", errors="replace")
00901         self.assertEqual(expect, result)
00902 
00903         # ASCII Encoding. On a sequence of values.
00904         given = (("\u00a0", (1, "\u00c1")),)
00905         expect = '%3F=1&%3F=%3F'
00906         result = urllib.parse.urlencode(given, True,
00907                                         encoding="ASCII", errors="replace")
00908         self.assertEqual(expect, result)
00909 
00910         # Utf-8
00911         given = (("\u00a0", "\u00c1"),)
00912         expect = '%C2%A0=%C3%81'
00913         result = urllib.parse.urlencode(given, True)
00914         self.assertEqual(expect, result)
00915 
00916         given = (("\u00a0", (42, "\u00c1")),)
00917         expect = '%C2%A0=42&%C2%A0=%C3%81'
00918         result = urllib.parse.urlencode(given, True)
00919         self.assertEqual(expect, result)
00920 
00921         # latin-1
00922         given = (("\u00a0", "\u00c1"),)
00923         expect = '%A0=%C1'
00924         result = urllib.parse.urlencode(given, True, encoding="latin-1")
00925         self.assertEqual(expect, result)
00926 
00927         given = (("\u00a0", (42, "\u00c1")),)
00928         expect = '%A0=42&%A0=%C1'
00929         result = urllib.parse.urlencode(given, True, encoding="latin-1")
00930         self.assertEqual(expect, result)

Here is the call graph for this function:

Definition at line 945 of file test_urllib.py.

00945 
00946     def test_urlencode_encoding_safe_parameter(self):
00947 
00948         # Send '$' (\x24) as safe character
00949         # Default utf-8 encoding
00950 
00951         given = ((b'\xa0\x24', b'\xc1\x24'),)
00952         result = urllib.parse.urlencode(given, safe=":$")
00953         expect = '%A0$=%C1$'
00954         self.assertEqual(expect, result)
00955 
00956         given = ((b'\xa0\x24', b'\xc1\x24'),)
00957         result = urllib.parse.urlencode(given, doseq=True, safe=":$")
00958         expect = '%A0$=%C1$'
00959         self.assertEqual(expect, result)
00960 
00961         # Safe parameter in sequence
00962         given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
00963         expect = '%A0$=%C1$&%A0$=13&%A0$=42'
00964         result = urllib.parse.urlencode(given, True, safe=":$")
00965         self.assertEqual(expect, result)
00966 
00967         # Test all above in latin-1 encoding
00968 
00969         given = ((b'\xa0\x24', b'\xc1\x24'),)
00970         result = urllib.parse.urlencode(given, safe=":$",
00971                                         encoding="latin-1")
00972         expect = '%A0$=%C1$'
00973         self.assertEqual(expect, result)
00974 
00975         given = ((b'\xa0\x24', b'\xc1\x24'),)
00976         expect = '%A0$=%C1$'
00977         result = urllib.parse.urlencode(given, doseq=True, safe=":$",
00978                                         encoding="latin-1")
00979 
00980         given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)
00981         expect = '%A0$=%C1$&%A0$=13&%A0$=42'
00982         result = urllib.parse.urlencode(given, True, safe=":$",
00983                                         encoding="latin-1")
00984         self.assertEqual(expect, result)

Here is the call graph for this function:

Definition at line 826 of file test_urllib.py.

00826 
00827     def test_using_mapping(self):
00828         # Test passing in a mapping object as an argument.
00829         self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},
00830                             "using dict as input type")

Here is the call graph for this function:

Definition at line 831 of file test_urllib.py.

00831 
00832     def test_using_sequence(self):
00833         # Test passing in a sequence of two-item sequences as an argument.
00834         self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],
00835                             "using sequence of two-item tuples as input")

Here is the call graph for this function:


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