Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 1 | # Minimal test of the quote function |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 2 | from test_support import verify, verbose |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 3 | import urllib |
| 4 | |
| 5 | chars = 'abcdefghijklmnopqrstuvwxyz'\ |
| 6 | '\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356' \ |
| 7 | '\357\360\361\362\363\364\365\366\370\371\372\373\374\375\376\377' \ |
| 8 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \ |
| 9 | '\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317' \ |
| 10 | '\320\321\322\323\324\325\326\330\331\332\333\334\335\336' |
| 11 | |
Tim Peters | cc58363 | 2001-01-19 07:00:08 +0000 | [diff] [blame] | 12 | expected = 'abcdefghijklmnopqrstuvwxyz' \ |
| 13 | '%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE' \ |
| 14 | '%EF%F0%F1%F2%F3%F4%F5%F6%F8%F9%FA%FB%FC%FD%FE%FF' \ |
| 15 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' \ |
| 16 | '%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF' \ |
| 17 | '%D0%D1%D2%D3%D4%D5%D6%D8%D9%DA%DB%DC%DD%DE' |
Jeremy Hylton | 6102e29 | 2000-08-31 15:48:10 +0000 | [diff] [blame] | 18 | |
| 19 | test = urllib.quote(chars) |
Tim Peters | 1a8a53d | 2001-01-19 06:06:37 +0000 | [diff] [blame] | 20 | verify(test == expected, "urllib.quote problem 1") |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 21 | test2 = urllib.unquote(expected) |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 22 | verify(test2 == chars) |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 23 | |
| 24 | in1 = "abc/def" |
| 25 | out1_1 = "abc/def" |
Tim Peters | cc58363 | 2001-01-19 07:00:08 +0000 | [diff] [blame] | 26 | out1_2 = "abc%2Fdef" |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 27 | |
Tim Peters | 1a8a53d | 2001-01-19 06:06:37 +0000 | [diff] [blame] | 28 | verify(urllib.quote(in1) == out1_1, "urllib.quote problem 2") |
| 29 | verify(urllib.quote(in1, '') == out1_2, "urllib.quote problem 3") |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 30 | |
| 31 | in2 = "abc?def" |
Tim Peters | cc58363 | 2001-01-19 07:00:08 +0000 | [diff] [blame] | 32 | out2_1 = "abc%3Fdef" |
Jeremy Hylton | 7ae51bf | 2000-09-14 16:59:07 +0000 | [diff] [blame] | 33 | out2_2 = "abc?def" |
| 34 | |
Tim Peters | 1a8a53d | 2001-01-19 06:06:37 +0000 | [diff] [blame] | 35 | verify(urllib.quote(in2) == out2_1, "urllib.quote problem 4") |
| 36 | verify(urllib.quote(in2, '?') == out2_2, "urllib.quote problem 5") |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 37 | |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 38 | |
| 39 | |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 40 | in3 = {"p1":"v1","p2":"v2"} |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 41 | in3list = [("p1", "v1"), ("p2","v2")] |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 42 | exp3_1 = "p2=v2&p1=v1" |
| 43 | exp3_2 = "p1=v1&p2=v2" |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 44 | # dict input, only string values |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 45 | act3 = urllib.urlencode(in3) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 46 | verify(act3==exp3_1 or act3==exp3_2, "urllib.urlencode problem 1 dict") |
| 47 | # list input, only string values |
| 48 | act3list = urllib.urlencode(in3list) |
| 49 | verify(act3list==exp3_2, "urllib.urlencode problem 1 list") |
| 50 | |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 51 | |
| 52 | in4 = {"p1":["v1","v2"]} |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 53 | in4list = [("p1", ["v1","v2"])] |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 54 | exp4 = "p1=v1&p1=v2" |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 55 | # dict input, list values, doseq==1 |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 56 | act4 = urllib.urlencode(in4,doseq=1) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 57 | verify(act4==exp4, "urllib.urlencode problem 2 dict") |
| 58 | # list input, list values, doseq==1 |
| 59 | act4list = urllib.urlencode(in4,doseq=1) |
| 60 | verify(act4list==exp4, "urllib.urlencode problem 2 list") |
| 61 | |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 62 | |
| 63 | in5 = in4 |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 64 | in5list = in4list |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 65 | exp5 = "p1=%5B%27v1%27%2C+%27v2%27%5D" |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 66 | exp5list = "p1=%5B%27v1%27%2C+%27v2%27%5D" |
| 67 | # dict input, list variables, doseq=0 |
Skip Montanaro | e78b92a | 2001-01-20 20:22:30 +0000 | [diff] [blame] | 68 | act5 = urllib.urlencode(in5) |
Skip Montanaro | 080c997 | 2001-01-28 21:12:22 +0000 | [diff] [blame^] | 69 | verify(act5==exp5, "urllib.urlencode problem 3 dict") |
| 70 | # list input, list variables, doseq=0 |
| 71 | act5list = urllib.urlencode(in5list) |
| 72 | verify(act5list==exp5list, "urllib.urlencode problem 3 list") |
| 73 | |
| 74 | |
| 75 | in6 = {"p1":"v1","p2":"v2"} |
| 76 | in6list = [("p1", "v1"), ("p2","v2")] |
| 77 | exp6_1 = "p2=v2&p1=v1" |
| 78 | exp6_2 = "p1=v1&p2=v2" |
| 79 | # dict input, only string values, doseq==1 |
| 80 | act6 = urllib.urlencode(in6,doseq=1) |
| 81 | verify(act6==exp6_1 or act6==exp6_2, "urllib.urlencode problem 4 dict") |
| 82 | # list input, only string values |
| 83 | act6list = urllib.urlencode(in6list,doseq=1) |
| 84 | verify(act6list==exp6_2, "urllib.urlencode problem 4 list") |
| 85 | |
| 86 | |
| 87 | in7 = "p1=v1&p2=v2" |
| 88 | try: |
| 89 | act7 = urllib.urlencode(in7) |
| 90 | print "urllib.urlencode problem 5 string" |
| 91 | except TypeError: |
| 92 | pass |
| 93 | |
| 94 | |
| 95 | import UserDict |
| 96 | in8 = UserDict.UserDict() |
| 97 | in8["p1"] = "v1" |
| 98 | in8["p2"] = ["v1", "v2"] |
| 99 | exp8_1 = "p1=v1&p2=v1&p2=v2" |
| 100 | exp8_2 = "p2=v1&p2=v2&p1=v1" |
| 101 | act8 = urllib.urlencode(in8,doseq=1) |
| 102 | verify(act8==exp8_1 or act8==exp8_2, "urllib.urlencode problem 6 UserDict") |
| 103 | |
| 104 | |
| 105 | import UserString |
| 106 | in9 = UserString.UserString("") |
| 107 | exp9 = "" |
| 108 | act9 = urllib.urlencode(in9,doseq=1) |
| 109 | verify(act9==exp9, "urllib.urlencode problem 7 UserString") |