cpython: Lib/test/test_urllib.py annotate (original) (raw)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1 """Regresssion tests for urllib"""

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

2

48334

3 import urllib.parse

4 import urllib.request

69047

5 import urllib.error

47318

6 import http.client

47635

f31e32a2013cPatch for issue 2848, mostly by Humberto Diogenes, with a couple of

Barry Warsaw barry@python.org

diff changeset

7 import email.message

42445

8 import io

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

9 import unittest

47201

10 from test import support

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

11 import os

69348

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

12 import sys

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

13 import tempfile

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

14

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

15 def hexescape(char):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

16 """Escape char as RFC 2396 specifies"""

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

17 hex_repr = hex(ord(char))[2:].upper()

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

18 if len(hex_repr) == 1:

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

19 hex_repr = "0%s" % hex_repr

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

20 return "%" + hex_repr

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

21

48334

22 # Shortcut for testing FancyURLopener

23 _urlopener = None

24 def urlopen(url, data=None, proxies=None):

25 """urlopen(url [, data]) -> open file-like object"""

26 global _urlopener

27 if proxies is not None:

28 opener = urllib.request.FancyURLopener(proxies=proxies)

29 elif not _urlopener:

30 opener = urllib.request.FancyURLopener()

31 _urlopener = opener

32 else:

33 opener = _urlopener

34 if data is None:

35 return opener.open(url)

36 else:

37 return opener.open(url, data)

38

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

39 class urlopen_FileTests(unittest.TestCase):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

40 """Test urlopen() opening a temporary file.

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

41

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

42 Try to test as much functionality as possible so as to cut down on reliance

32439

43 on connecting to the Net for testing.

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

44

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

45 """

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

46

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

47 def setUp(self):

48334

48 # Create a temp file to use for testing

49 self.text = bytes("test_urllib: %s\n" % self.__class__.__name__,

50 "ascii")

51 f = open(support.TESTFN, 'wb')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

52 try:

48334

53 f.write(self.text)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

54 finally:

48334

55 f.close()

47201

56 self.pathname = support.TESTFN

48334

57 self.returned_obj = urlopen("file:%s" % self.pathname)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

58

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

59 def tearDown(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

60 """Shut down the open object"""

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

61 self.returned_obj.close()

47201

62 os.remove(support.TESTFN)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

63

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

64 def test_interface(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

65 # Make sure object returned by urlopen() has the specified methods

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

66 for attr in ("read", "readline", "readlines", "fileno",

44828

67 "close", "info", "geturl", "getcode", "__iter__"):

55593

68 self.assertTrue(hasattr(self.returned_obj, attr),

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

69 "object returned by urlopen() lacks %s attribute" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

70 attr)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

71

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

72 def test_read(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

73 self.assertEqual(self.text, self.returned_obj.read())

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

74

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

75 def test_readline(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

76 self.assertEqual(self.text, self.returned_obj.readline())

42170

77 self.assertEqual(b'', self.returned_obj.readline(),

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

78 "calling readline() after exhausting the file did not"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

79 " return an empty string")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

80

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

81 def test_readlines(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

82 lines_list = self.returned_obj.readlines()

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

83 self.assertEqual(len(lines_list), 1,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

84 "readlines() returned the wrong number of lines")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

85 self.assertEqual(lines_list[0], self.text,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

86 "readlines() returned improper text")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

87

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

88 def test_fileno(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

89 file_num = self.returned_obj.fileno()

58473

90 self.assertIsInstance(file_num, int, "fileno() did not return an int")

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

91 self.assertEqual(os.read(file_num, len(self.text)), self.text,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

92 "Reading on the file descriptor returned by fileno() "

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

93 "did not return the expected text")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

94

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

95 def test_close(self):

68675

edc3d3b07435Closes issue11563 - test_urllibnet ResourceWarning. Patch by Jeff McNeil.

Senthil Kumaran orsenthil@gmail.com

diff changeset

96 # Test close() by calling it here and then having it be called again

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

97 # by the tearDown() method for the test

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

98 self.returned_obj.close()

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

99

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

100 def test_info(self):

58473

101 self.assertIsInstance(self.returned_obj.info(), email.message.Message)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

102

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

103 def test_geturl(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

104 self.assertEqual(self.returned_obj.geturl(), self.pathname)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

105

44828

106 def test_getcode(self):

64010

656fe6292f9fMerged revisions 83212,83829,83833,83838-83839,83878,84019,84025,84028,84032,84036 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

107 self.assertIsNone(self.returned_obj.getcode())

44828

108

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

109 def test_iter(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

110 # Test iterator

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

111 # Don't need to count number of iterations since test would fail the

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

112 # instant it returned anything beyond the first line from the

70996

c0a68b948f5dIssue #4608: urllib.request.urlopen does not return an iterable object

Raymond Hettinger python@rcn.com

diff changeset

113 # comparison.

c0a68b948f5dIssue #4608: urllib.request.urlopen does not return an iterable object

Raymond Hettinger python@rcn.com

diff changeset

114 # Use the iterator in the usual implicit way to test for ticket #4608.

c0a68b948f5dIssue #4608: urllib.request.urlopen does not return an iterable object

Raymond Hettinger python@rcn.com

diff changeset

115 for line in self.returned_obj:

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

116 self.assertEqual(line, self.text)

14297

117

49274

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

118 class ProxyTests(unittest.TestCase):

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

119

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

120 def setUp(self):

53492

121 # Records changes to env vars

122 self.env = support.EnvironmentVarGuard()

58218

89a9dd8c628dMerged revisions 77209,77229,77359-77360,77371 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

123 # Delete all proxy related env vars

65325

124 for k in list(os.environ):

125 if 'proxy' in k.lower():

58218

89a9dd8c628dMerged revisions 77209,77229,77359-77360,77371 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

126 self.env.unset(k)

49274

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

127

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

128 def tearDown(self):

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

129 # Restore all proxy related env vars

53492

130 self.env.__exit__()

131 del self.env

49274

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

132

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

133 def test_getproxies_environment_keep_no_proxies(self):

53492

134 self.env.set('NO_PROXY', 'localhost')

135 proxies = urllib.request.getproxies_environment()

136 # getproxies_environment use lowered case truncated (no '_proxy') keys

64010

656fe6292f9fMerged revisions 83212,83829,83833,83838-83839,83878,84019,84025,84028,84032,84036 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

137 self.assertEqual('localhost', proxies['no'])

71751

1d4bd059a9b6Fix closes issue12698 - make the no_proxy environment variable handling a bit lenient (accomodate spaces in between the items)

Senthil Kumaran senthil@uthcode.com

diff changeset

138 # List of no_proxies with space.

1d4bd059a9b6Fix closes issue12698 - make the no_proxy environment variable handling a bit lenient (accomodate spaces in between the items)

Senthil Kumaran senthil@uthcode.com

diff changeset

139 self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')

1d4bd059a9b6Fix closes issue12698 - make the no_proxy environment variable handling a bit lenient (accomodate spaces in between the items)

Senthil Kumaran senthil@uthcode.com

diff changeset

140 self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com'))

49274

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

141

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

142 class urlopen_HttpTests(unittest.TestCase):

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

143 """Test urlopen() opening a fake http connection."""

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

144

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

145 def fakehttp(self, fakedata):

42445

146 class FakeSocket(io.BytesIO):

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

147 io_refs = 1

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

148 def sendall(self, str): pass

51459

149 def makefile(self, *args, **kwds):

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

150 self.io_refs += 1

51459

151 return self

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

152 def read(self, amt=None):

42445

153 if self.closed: return b""

154 return io.BytesIO.read(self, amt)

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

155 def readline(self, length=None):

42445

156 if self.closed: return b""

157 return io.BytesIO.readline(self, length)

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

158 def close(self):

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

159 self.io_refs -= 1

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

160 if self.io_refs == 0:

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

161 io.BytesIO.close(self)

47318

162 class FakeHTTPConnection(http.client.HTTPConnection):

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

163 def connect(self):

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

164 self.sock = FakeSocket(fakedata)

47318

165 self._connection_class = http.client.HTTPConnection

166 http.client.HTTPConnection = FakeHTTPConnection

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

167

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

168 def unfakehttp(self):

47318

169 http.client.HTTPConnection = self._connection_class

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

170

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

171 def check_read(self, ver):

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

172 self.fakehttp(b"HTTP/" + ver + b" 200 OK\r\n\r\nHello!")

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

173 try:

48334

174 fp = urlopen("http://python.org/")

42445

175 self.assertEqual(fp.readline(), b"Hello!")

176 self.assertEqual(fp.readline(), b"")

44828

177 self.assertEqual(fp.geturl(), 'http://python.org/')

178 self.assertEqual(fp.getcode(), 200)

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

179 finally:

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

180 self.unfakehttp()

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

181

69284

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

182 def test_url_fragment(self):

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

183 # Issue #11703: geturl() omits fragments in the original URL.

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

184 url = 'http://docs.python.org/library/urllib.html#OK'

69285

185 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")

69284

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

186 try:

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

187 fp = urllib.request.urlopen(url)

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

188 self.assertEqual(fp.geturl(), url)

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

189 finally:

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

190 self.unfakehttp()

3f240a1cd245Fix Issue11703 - urllib2.geturl() does not return correct url when the original url contains #fragment. Patch Contribution by Santoso Wijaya.

Senthil Kumaran orsenthil@gmail.com

diff changeset

191

68675

edc3d3b07435Closes issue11563 - test_urllibnet ResourceWarning. Patch by Jeff McNeil.

Senthil Kumaran orsenthil@gmail.com

diff changeset

192 def test_willclose(self):

edc3d3b07435Closes issue11563 - test_urllibnet ResourceWarning. Patch by Jeff McNeil.

Senthil Kumaran orsenthil@gmail.com

diff changeset

193 self.fakehttp(b"HTTP/1.1 200 OK\r\n\r\nHello!")

68688

5c83a375a23eCall unfakehttp in order to close connection while opening the connection through a fakehttp object.

Senthil Kumaran orsenthil@gmail.com

diff changeset

194 try:

5c83a375a23eCall unfakehttp in order to close connection while opening the connection through a fakehttp object.

Senthil Kumaran orsenthil@gmail.com

diff changeset

195 resp = urlopen("http://www.python.org")

5c83a375a23eCall unfakehttp in order to close connection while opening the connection through a fakehttp object.

Senthil Kumaran orsenthil@gmail.com

diff changeset

196 self.assertTrue(resp.fp.will_close)

5c83a375a23eCall unfakehttp in order to close connection while opening the connection through a fakehttp object.

Senthil Kumaran orsenthil@gmail.com

diff changeset

197 finally:

5c83a375a23eCall unfakehttp in order to close connection while opening the connection through a fakehttp object.

Senthil Kumaran orsenthil@gmail.com

diff changeset

198 self.unfakehttp()

68675

edc3d3b07435Closes issue11563 - test_urllibnet ResourceWarning. Patch by Jeff McNeil.

Senthil Kumaran orsenthil@gmail.com

diff changeset

199

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

200 def test_read_0_9(self):

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

201 # "0.9" response accepted (but not "simple responses" without

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

202 # a status line)

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

203 self.check_read(b"0.9")

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

204

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

205 def test_read_1_0(self):

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

206 self.check_read(b"1.0")

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

207

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

208 def test_read_1_1(self):

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

209 self.check_read(b"1.1")

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

210

44428

211 def test_read_bogus(self):

212 # urlopen() should raise IOError for many error codes.

213 self.fakehttp(b'''HTTP/1.1 401 Authentication Required

214 Date: Wed, 02 Jan 2008 03:03:54 GMT

215 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e

216 Connection: close

217 Content-Type: text/html; charset=iso-8859-1

218 ''')

219 try:

48334

220 self.assertRaises(IOError, urlopen, "http://python.org/")

44428

221 finally:

222 self.unfakehttp()

223

69047

224 def test_invalid_redirect(self):

225 # urlopen() should raise IOError for many error codes.

226 self.fakehttp(b'''HTTP/1.1 302 Found

227 Date: Wed, 02 Jan 2008 03:03:54 GMT

228 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e

229 Location: file://guidocomputer.athome.com:/python/license

230 Connection: close

231 Content-Type: text/html; charset=iso-8859-1

232 ''')

233 try:

234 self.assertRaises(urllib.error.HTTPError, urlopen,

235 "http://python.org/")

236 finally:

237 self.unfakehttp()

238

41554

239 def test_empty_socket(self):

42445

240 # urlopen() raises IOError if the underlying socket does not send any

241 # data. (#1680230)

44428

242 self.fakehttp(b'')

41554

243 try:

48334

244 self.assertRaises(IOError, urlopen, "http://something")

41554

245 finally:

246 self.unfakehttp()

247

63432

248 def test_userpass_inurl(self):

66988

b6d3fe70802fIssue #10711: Remove HTTP 0.9 support from http.client. The `strict`

Antoine Pitrou solipsis@pitrou.net

diff changeset

249 self.fakehttp(b"HTTP/1.0 200 OK\r\n\r\nHello!")

63432

250 try:

251 fp = urlopen("http://user:pass@python.org/")

252 self.assertEqual(fp.readline(), b"Hello!")

253 self.assertEqual(fp.readline(), b"")

254 self.assertEqual(fp.geturl(), 'http://user:pass@python.org/')

255 self.assertEqual(fp.getcode(), 200)

256 finally:

257 self.unfakehttp()

258

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

259 class urlretrieve_FileTests(unittest.TestCase):

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

260 """Test urllib.urlretrieve() on local files"""

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

261

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

262 def setUp(self):

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

263 # Create a list of temporary files. Each item in the list is a file

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

264 # name (absolute path or relative to the current working directory).

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

265 # All files in this list will be deleted in the tearDown method. Note,

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

266 # this only helps to makes sure temporary files get deleted, but it

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

267 # does nothing about trying to close files that may still be open. It

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

268 # is the responsibility of the developer to properly close files even

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

269 # when exceptional conditions occur.

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

270 self.tempFiles = []

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

271

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

272 # Create a temporary file.

47201

273 self.registerFileForCleanUp(support.TESTFN)

42170

274 self.text = b'testing urllib.urlretrieve'

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

275 try:

47201

276 FILE = open(support.TESTFN, 'wb')

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

277 FILE.write(self.text)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

278 FILE.close()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

279 finally:

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

280 try: FILE.close()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

281 except: pass

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

282

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

283 def tearDown(self):

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

284 # Delete the temporary files.

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

285 for each in self.tempFiles:

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

286 try: os.remove(each)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

287 except: pass

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

288

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

289 def constructLocalFileUrl(self, filePath):

63791

7608532f933aIssue #9425: skip tests if a filename is not encodable

Victor Stinner victor.stinner@haypocalc.com

diff changeset

290 filePath = os.path.abspath(filePath)

7608532f933aIssue #9425: skip tests if a filename is not encodable

Victor Stinner victor.stinner@haypocalc.com

diff changeset

291 try:

68142

292 filePath.encode("utf-8")

63791

7608532f933aIssue #9425: skip tests if a filename is not encodable

Victor Stinner victor.stinner@haypocalc.com

diff changeset

293 except UnicodeEncodeError:

7608532f933aIssue #9425: skip tests if a filename is not encodable

Victor Stinner victor.stinner@haypocalc.com

diff changeset

294 raise unittest.SkipTest("filePath is not encodable to utf8")

7608532f933aIssue #9425: skip tests if a filename is not encodable

Victor Stinner victor.stinner@haypocalc.com

diff changeset

295 return "file://%s" % urllib.request.pathname2url(filePath)

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

296

42964

297 def createNewTempFile(self, data=b""):

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

298 """Creates a new temporary file containing the specified data,

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

299 registers the file for deletion during the test fixture tear down, and

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

300 returns the absolute path of the file."""

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

301

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

302 newFd, newFilePath = tempfile.mkstemp()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

303 try:

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

304 self.registerFileForCleanUp(newFilePath)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

305 newFile = os.fdopen(newFd, "wb")

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

306 newFile.write(data)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

307 newFile.close()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

308 finally:

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

309 try: newFile.close()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

310 except: pass

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

311 return newFilePath

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

312

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

313 def registerFileForCleanUp(self, fileName):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

314 self.tempFiles.append(fileName)

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

315

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

316 def test_basic(self):

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

317 # Make sure that a local file just gets its own location returned and

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

318 # a headers value is returned.

48334

319 result = urllib.request.urlretrieve("file:%s" % support.TESTFN)

47201

320 self.assertEqual(result[0], support.TESTFN)

58473

321 self.assertIsInstance(result[1], email.message.Message,

322 "did not get a email.message.Message instance "

323 "as second returned value")

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

324

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

325 def test_copy(self):

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

326 # Test that setting the filename argument works.

47201

327 second_temp = "%s.2" % support.TESTFN

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

328 self.registerFileForCleanUp(second_temp)

48334

329 result = urllib.request.urlretrieve(self.constructLocalFileUrl(

47201

330 support.TESTFN), second_temp)

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

331 self.assertEqual(second_temp, result[0])

55593

332 self.assertTrue(os.path.exists(second_temp), "copy of the file was not "

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

333 "made")

39647

dceea41f4a6dAnna Ravenscroft identified many occurrences of "file" used to open a file

Alex Martelli aleaxit@gmail.com

diff changeset

334 FILE = open(second_temp, 'rb')

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

335 try:

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

336 text = FILE.read()

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

337 FILE.close()

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

338 finally:

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

339 try: FILE.close()

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

340 except: pass

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

341 self.assertEqual(self.text, text)

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

342

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

343 def test_reporthook(self):

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

344 # Make sure that the reporthook works.

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

345 def hooktester(count, block_size, total_size, count_holder=[0]):

58473

346 self.assertIsInstance(count, int)

347 self.assertIsInstance(block_size, int)

348 self.assertIsInstance(total_size, int)

28876

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

349 self.assertEqual(count, count_holder[0])

9eb5be937990Added tests for urlretrieve. Also made sure urlopen tests cleaned up properly after themselves.

Brett Cannon bcannon@gmail.com

diff changeset

350 count_holder[0] = count_holder[0] + 1

47201

351 second_temp = "%s.2" % support.TESTFN

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

352 self.registerFileForCleanUp(second_temp)

48334

353 urllib.request.urlretrieve(

354 self.constructLocalFileUrl(support.TESTFN),

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

355 second_temp, hooktester)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

356

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

357 def test_reporthook_0_bytes(self):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

358 # Test on zero length file. Should call reporthook only 1 time.

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

359 report = []

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

360 def hooktester(count, block_size, total_size, _report=report):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

361 _report.append((count, block_size, total_size))

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

362 srcFileName = self.createNewTempFile()

48334

363 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),

47201

364 support.TESTFN, hooktester)

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

365 self.assertEqual(len(report), 1)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

366 self.assertEqual(report[0][2], 0)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

367

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

368 def test_reporthook_5_bytes(self):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

369 # Test on 5 byte file. Should call reporthook only 2 times (once when

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

370 # the "network connection" is established and once when the block is

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

371 # read). Since the block size is 8192 bytes, only one block read is

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

372 # required to read the entire file.

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

373 report = []

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

374 def hooktester(count, block_size, total_size, _report=report):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

375 _report.append((count, block_size, total_size))

42964

376 srcFileName = self.createNewTempFile(b"x" * 5)

48334

377 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),

47201

378 support.TESTFN, hooktester)

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

379 self.assertEqual(len(report), 2)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

380 self.assertEqual(report[0][1], 8192)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

381 self.assertEqual(report[0][2], 5)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

382

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

383 def test_reporthook_8193_bytes(self):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

384 # Test on 8193 byte file. Should call reporthook only 3 times (once

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

385 # when the "network connection" is established, once for the next 8192

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

386 # bytes, and once for the last byte).

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

387 report = []

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

388 def hooktester(count, block_size, total_size, _report=report):

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

389 _report.append((count, block_size, total_size))

42964

390 srcFileName = self.createNewTempFile(b"x" * 8193)

48334

391 urllib.request.urlretrieve(self.constructLocalFileUrl(srcFileName),

47201

392 support.TESTFN, hooktester)

35457

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

393 self.assertEqual(len(report), 3)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

394 self.assertEqual(report[0][1], 8192)

9d41afa63b79backport patch [ 810023 ] Fix for off-by-one bug in urllib.URLopener.retrieve

Georg Brandl georg@python.org

diff changeset

395 self.assertEqual(report[0][2], 8193)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

396

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

397 class QuotingTests(unittest.TestCase):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

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

29087

399

48334

400 According to RFC 2396 (Uniform Resource Identifiers), to escape a

401 character you write it as '%' + <2 character US-ASCII hex value>.

402 The Python code of ``'%' + hex(ord())[2:]`` escapes a

403 character properly. Case does not matter on the hex letters.

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

404

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

405 The various character sets specified are:

29087

406

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

407 Reserved characters : ";/?:@&=+$,"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

408 Have special meaning in URIs and must be escaped if not being used for

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

409 their special meaning

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

410 Data characters : letters, digits, and "-_.!~*'()"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

411 Unreserved and do not need to be escaped; can be, though, if desired

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

412 Control characters : 0x00 - 0x1F, 0x7F

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

413 Have no use in URIs so must be escaped

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

414 space : 0x20

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

415 Must be escaped

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

416 Delimiters : '<>#%"'

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

417 Must be escaped

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

418 Unwise : "{}|\^[]`"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

419 Must be escaped

29087

420

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

421 """

14297

422

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

423 def test_never_quote(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

424 # Make sure quote() does not quote letters, digits, and "_,.-"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

425 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ",

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

426 "abcdefghijklmnopqrstuvwxyz",

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

427 "0123456789",

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

428 "_.-"])

48334

429 result = urllib.parse.quote(do_not_quote)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

430 self.assertEqual(do_not_quote, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

431 "using quote(): %r != %r" % (do_not_quote, result))

48334

432 result = urllib.parse.quote_plus(do_not_quote)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

433 self.assertEqual(do_not_quote, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

434 "using quote_plus(): %r != %r" % (do_not_quote, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

435

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

436 def test_default_safe(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

437 # Test '/' is default value for 'safe' parameter

48334

438 self.assertEqual(urllib.parse.quote.__defaults__[0], '/')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

439

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

440 def test_safe(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

441 # Test setting 'safe' parameter does what it should do

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

442 quote_by_default = "<>"

48334

443 result = urllib.parse.quote(quote_by_default, safe=quote_by_default)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

444 self.assertEqual(quote_by_default, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

445 "using quote(): %r != %r" % (quote_by_default, result))

52343

446 result = urllib.parse.quote_plus(quote_by_default,

447 safe=quote_by_default)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

448 self.assertEqual(quote_by_default, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

449 "using quote_plus(): %r != %r" %

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

450 (quote_by_default, result))

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

451 # Safe expressed as bytes rather than str

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

452 result = urllib.parse.quote(quote_by_default, safe=b"<>")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

453 self.assertEqual(quote_by_default, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

454 "using quote(): %r != %r" % (quote_by_default, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

455 # "Safe" non-ASCII characters should have no effect

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

456 # (Since URIs are not allowed to have non-ASCII characters)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

457 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="\xfc")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

458 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

459 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

460 "using quote(): %r != %r" %

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

461 (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

462 # Same as above, but using a bytes rather than str

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

463 result = urllib.parse.quote("a\xfcb", encoding="latin-1", safe=b"\xfc")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

464 expect = urllib.parse.quote("a\xfcb", encoding="latin-1", safe="")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

465 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

466 "using quote(): %r != %r" %

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

467 (expect, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

468

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

469 def test_default_quoting(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

470 # Make sure all characters that should be quoted are by default sans

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

471 # space (separate test for that).

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

472 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

473 should_quote.append('<>#%"{}|\^[]`')

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

474 should_quote.append(chr(127)) # For 0x7F

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

475 should_quote = ''.join(should_quote)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

476 for char in should_quote:

48334

477 result = urllib.parse.quote(char)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

478 self.assertEqual(hexescape(char), result,

52343

479 "using quote(): "

480 "%s should be escaped to %s, not %s" %

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

481 (char, hexescape(char), result))

48334

482 result = urllib.parse.quote_plus(char)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

483 self.assertEqual(hexescape(char), result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

484 "using quote_plus(): "

29087

485 "%s should be escapes to %s, not %s" %

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

486 (char, hexescape(char), result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

487 del should_quote

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

488 partial_quote = "ab[]cd"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

489 expected = "ab%5B%5Dcd"

48334

490 result = urllib.parse.quote(partial_quote)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

491 self.assertEqual(expected, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

492 "using quote(): %r != %r" % (expected, result))

72370

493 result = urllib.parse.quote_plus(partial_quote)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

494 self.assertEqual(expected, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

495 "using quote_plus(): %r != %r" % (expected, result))

14297

496

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

497 def test_quoting_space(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

498 # Make sure quote() and quote_plus() handle spaces as specified in

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

499 # their unique way

48334

500 result = urllib.parse.quote(' ')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

501 self.assertEqual(result, hexescape(' '),

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

502 "using quote(): %r != %r" % (result, hexescape(' ')))

48334

503 result = urllib.parse.quote_plus(' ')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

504 self.assertEqual(result, '+',

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

505 "using quote_plus(): %r != +" % result)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

506 given = "a b cd e f"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

507 expect = given.replace(' ', hexescape(' '))

48334

508 result = urllib.parse.quote(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

509 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

510 "using quote(): %r != %r" % (expect, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

511 expect = given.replace(' ', '+')

48334

512 result = urllib.parse.quote_plus(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

513 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

514 "using quote_plus(): %r != %r" % (expect, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

515

35530

516 def test_quoting_plus(self):

48334

517 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma'),

35530

518 'alpha%2Bbeta+gamma')

48334

519 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', '+'),

35530

520 'alpha+beta+gamma')

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

521 # Test with bytes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

522 self.assertEqual(urllib.parse.quote_plus(b'alpha+beta gamma'),

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

523 'alpha%2Bbeta+gamma')

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

524 # Test with safe bytes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

525 self.assertEqual(urllib.parse.quote_plus('alpha+beta gamma', b'+'),

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

526 'alpha+beta+gamma')

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

527

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

528 def test_quote_bytes(self):

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

529 # Bytes should quote directly to percent-encoded values

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

530 given = b"\xa2\xd8ab\xff"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

531 expect = "%A2%D8ab%FF"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

532 result = urllib.parse.quote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

533 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

534 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

535 # Encoding argument should raise type error on bytes input

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

536 self.assertRaises(TypeError, urllib.parse.quote, given,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

537 encoding="latin-1")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

538 # quote_from_bytes should work the same

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

539 result = urllib.parse.quote_from_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

540 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

541 "using quote_from_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

542 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

543

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

544 def test_quote_with_unicode(self):

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

545 # Characters in Latin-1 range, encoded by default in UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

546 given = "\xa2\xd8ab\xff"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

547 expect = "%C2%A2%C3%98ab%C3%BF"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

548 result = urllib.parse.quote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

549 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

550 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

551 # Characters in Latin-1 range, encoded by with None (default)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

552 result = urllib.parse.quote(given, encoding=None, errors=None)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

553 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

554 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

555 # Characters in Latin-1 range, encoded with Latin-1

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

556 given = "\xa2\xd8ab\xff"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

557 expect = "%A2%D8ab%FF"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

558 result = urllib.parse.quote(given, encoding="latin-1")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

559 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

560 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

561 # Characters in BMP, encoded by default in UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

562 given = "\u6f22\u5b57" # "Kanji"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

563 expect = "%E6%BC%A2%E5%AD%97"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

564 result = urllib.parse.quote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

565 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

566 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

567 # Characters in BMP, encoded with Latin-1

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

568 given = "\u6f22\u5b57"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

569 self.assertRaises(UnicodeEncodeError, urllib.parse.quote, given,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

570 encoding="latin-1")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

571 # Characters in BMP, encoded with Latin-1, with replace error handling

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

572 given = "\u6f22\u5b57"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

573 expect = "%3F%3F" # "??"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

574 result = urllib.parse.quote(given, encoding="latin-1",

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

575 errors="replace")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

576 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

577 "using quote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

578 # Characters in BMP, Latin-1, with xmlcharref error handling

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

579 given = "\u6f22\u5b57"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

580 expect = "%26%2328450%3B%26%2323383%3B" # "漢字"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

581 result = urllib.parse.quote(given, encoding="latin-1",

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

582 errors="xmlcharrefreplace")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

583 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

584 "using quote(): %r != %r" % (expect, result))

35530

585

54356

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

586 def test_quote_plus_with_unicode(self):

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

587 # Encoding (latin-1) test for quote_plus

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

588 given = "\xa2\xd8 \xff"

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

589 expect = "%A2%D8+%FF"

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

590 result = urllib.parse.quote_plus(given, encoding="latin-1")

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

591 self.assertEqual(expect, result,

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

592 "using quote_plus(): %r != %r" % (expect, result))

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

593 # Errors test for quote_plus

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

594 given = "ab\u6f22\u5b57 cd"

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

595 expect = "ab%3F%3F+cd"

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

596 result = urllib.parse.quote_plus(given, encoding="latin-1",

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

597 errors="replace")

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

598 self.assertEqual(expect, result,

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

599 "using quote_plus(): %r != %r" % (expect, result))

2721be7be365#6118: dont ignore encoding arguments for arguments with spaces in quote_plus().

Georg Brandl georg@python.org

diff changeset

600

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

601

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

602 class UnquotingTests(unittest.TestCase):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

603 """Tests for unquote() and unquote_plus()

29087

604

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

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

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

606

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

607 """

14551

28df4f45967fRemove "," from the list of always_safe characters. It is a reserved

Jeremy Hylton jeremy@alum.mit.edu

diff changeset

608

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

609 def test_unquoting(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

610 # Make sure unquoting of all ASCII values works

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

611 escape_list = []

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

612 for num in range(128):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

613 given = hexescape(chr(num))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

614 expect = chr(num)

48334

615 result = urllib.parse.unquote(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

616 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

617 "using unquote(): %r != %r" % (expect, result))

48334

618 result = urllib.parse.unquote_plus(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

619 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

620 "using unquote_plus(): %r != %r" %

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

621 (expect, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

622 escape_list.append(given)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

623 escape_string = ''.join(escape_list)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

624 del escape_list

48334

625 result = urllib.parse.unquote(escape_string)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

626 self.assertEqual(result.count('%'), 1,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

627 "using unquote(): not all characters escaped: "

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

628 "%s" % result)

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

629 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, None)

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

630 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, ())

64012

631 with support.check_warnings(('', BytesWarning), quiet=True):

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

632 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote, b'')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

633

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

634 def test_unquoting_badpercent(self):

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

635 # Test unquoting on bad percent-escapes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

636 given = '%xab'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

637 expect = given

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

638 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

639 self.assertEqual(expect, result, "using unquote(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

640 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

641 given = '%x'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

642 expect = given

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

643 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

644 self.assertEqual(expect, result, "using unquote(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

645 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

646 given = '%'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

647 expect = given

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

648 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

649 self.assertEqual(expect, result, "using unquote(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

650 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

651 # unquote_to_bytes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

652 given = '%xab'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

653 expect = bytes(given, 'ascii')

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

654 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

655 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

656 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

657 given = '%x'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

658 expect = bytes(given, 'ascii')

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

659 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

660 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

661 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

662 given = '%'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

663 expect = bytes(given, 'ascii')

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

664 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

665 self.assertEqual(expect, result, "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

666 % (expect, result))

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

667 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, None)

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

668 self.assertRaises((TypeError, AttributeError), urllib.parse.unquote_to_bytes, ())

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

669

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

670 def test_unquoting_mixed_case(self):

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

671 # Test unquoting on mixed-case hex digits in the percent-escapes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

672 given = '%Ab%eA'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

673 expect = b'\xab\xea'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

674 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

675 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

676 "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

677 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

678

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

679 def test_unquoting_parts(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

680 # Make sure unquoting works when have non-quoted characters

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

681 # interspersed

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

682 given = 'ab%sd' % hexescape('c')

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

683 expect = "abcd"

48334

684 result = urllib.parse.unquote(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

685 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

686 "using quote(): %r != %r" % (expect, result))

48334

687 result = urllib.parse.unquote_plus(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

688 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

689 "using unquote_plus(): %r != %r" % (expect, result))

29087

690

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

691 def test_unquoting_plus(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

692 # Test difference between unquote() and unquote_plus()

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

693 given = "are+there+spaces..."

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

694 expect = given

48334

695 result = urllib.parse.unquote(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

696 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

697 "using unquote(): %r != %r" % (expect, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

698 expect = given.replace('+', ' ')

48334

699 result = urllib.parse.unquote_plus(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

700 self.assertEqual(expect, result,

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

701 "using unquote_plus(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

702

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

703 def test_unquote_to_bytes(self):

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

704 given = 'br%C3%BCckner_sapporo_20050930.doc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

705 expect = b'br\xc3\xbcckner_sapporo_20050930.doc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

706 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

707 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

708 "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

709 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

710 # Test on a string with unescaped non-ASCII characters

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

711 # (Technically an invalid URI; expect those characters to be UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

712 # encoded).

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

713 result = urllib.parse.unquote_to_bytes("\u6f22%C3%BC")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

714 expect = b'\xe6\xbc\xa2\xc3\xbc' # UTF-8 for "\u6f22\u00fc"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

715 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

716 "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

717 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

718 # Test with a bytes as input

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

719 given = b'%A2%D8ab%FF'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

720 expect = b'\xa2\xd8ab\xff'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

721 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

722 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

723 "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

724 % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

725 # Test with a bytes as input, with unescaped non-ASCII bytes

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

726 # (Technically an invalid URI; expect those bytes to be preserved)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

727 given = b'%A2\xd8ab%FF'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

728 expect = b'\xa2\xd8ab\xff'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

729 result = urllib.parse.unquote_to_bytes(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

730 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

731 "using unquote_to_bytes(): %r != %r"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

732 % (expect, result))

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

733

35723

734 def test_unquote_with_unicode(self):

48755

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

735 # Characters in the Latin-1 range, encoded with UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

736 given = 'br%C3%BCckner_sapporo_20050930.doc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

737 expect = 'br\u00fcckner_sapporo_20050930.doc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

738 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

739 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

740 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

741 # Characters in the Latin-1 range, encoded with None (default)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

742 result = urllib.parse.unquote(given, encoding=None, errors=None)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

743 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

744 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

745

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

746 # Characters in the Latin-1 range, encoded with Latin-1

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

747 result = urllib.parse.unquote('br%FCckner_sapporo_20050930.doc',

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

748 encoding="latin-1")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

749 expect = 'br\u00fcckner_sapporo_20050930.doc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

750 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

751 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

752

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

753 # Characters in BMP, encoded with UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

754 given = "%E6%BC%A2%E5%AD%97"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

755 expect = "\u6f22\u5b57" # "Kanji"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

756 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

757 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

758 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

759

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

760 # Decode with UTF-8, invalid sequence

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

761 given = "%F3%B1"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

762 expect = "\ufffd" # Replacement character

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

763 result = urllib.parse.unquote(given)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

764 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

765 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

766

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

767 # Decode with UTF-8, invalid sequence, replace errors

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

768 result = urllib.parse.unquote(given, errors="replace")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

769 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

770 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

771

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

772 # Decode with UTF-8, invalid sequence, ignoring errors

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

773 given = "%F3%B1"

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

774 expect = ""

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

775 result = urllib.parse.unquote(given, errors="ignore")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

776 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

777 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

778

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

779 # A mix of non-ASCII and percent-encoded characters, UTF-8

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

780 result = urllib.parse.unquote("\u6f22%C3%BC")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

781 expect = '\u6f22\u00fc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

782 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

783 "using unquote(): %r != %r" % (expect, result))

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

784

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

785 # A mix of non-ASCII and percent-encoded characters, Latin-1

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

786 # (Note, the string contains non-Latin-1-representable characters)

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

787 result = urllib.parse.unquote("\u6f22%FC", encoding="latin-1")

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

788 expect = '\u6f22\u00fc'

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

789 self.assertEqual(expect, result,

428d384ed626- Issue #3300: make urllib.parse.[un]quote() default to UTF-8.

Guido van Rossum guido@python.org

diff changeset

790 "using unquote(): %r != %r" % (expect, result))

35723

791

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

792 class urlencode_Tests(unittest.TestCase):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

793 """Tests for urlencode()"""

14551

28df4f45967fRemove "," from the list of always_safe characters. It is a reserved

Jeremy Hylton jeremy@alum.mit.edu

diff changeset

794

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

795 def help_inputtype(self, given, test_type):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

796 """Helper method for testing different input types.

29087

797

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

798 'given' must lead to only the pairs:

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

799 * 1st, 1

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

800 * 2nd, 2

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

801 * 3rd, 3

29087

802

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

803 Test cannot assume anything about order. Docs make no guarantee and

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

804 have possible dictionary input.

29087

805

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

806 """

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

807 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"]

48334

808 result = urllib.parse.urlencode(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

809 for expected in expect_somewhere:

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

810 self.assertIn(expected, result,

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

811 "testing %s: %s not found in %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

812 (test_type, expected, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

813 self.assertEqual(result.count('&'), 2,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

814 "testing %s: expected 2 '&'s; got %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

815 (test_type, result.count('&')))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

816 amp_location = result.index('&')

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

817 on_amp_left = result[amp_location - 1]

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

818 on_amp_right = result[amp_location + 1]

55593

819 self.assertTrue(on_amp_left.isdigit() and on_amp_right.isdigit(),

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

820 "testing %s: '&' not located in proper place in %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

821 (test_type, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

822 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

823 "testing %s: "

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

824 "unexpected number of characters: %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

825 (test_type, len(result), (5 * 3) + 2))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

826

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

827 def test_using_mapping(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

828 # Test passing in a mapping object as an argument.

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

829 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'},

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

830 "using dict as input type")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

831

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

832 def test_using_sequence(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

833 # Test passing in a sequence of two-item sequences as an argument.

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

834 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')],

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

835 "using sequence of two-item tuples as input")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

836

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

837 def test_quoting(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

838 # Make sure keys and values are quoted using quote_plus()

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

839 given = {"&":"="}

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

840 expect = "%s=%s" % (hexescape('&'), hexescape('='))

48334

841 result = urllib.parse.urlencode(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

842 self.assertEqual(expect, result)

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

843 given = {"key name":"A bunch of pluses"}

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

844 expect = "key+name=A+bunch+of+pluses"

48334

845 result = urllib.parse.urlencode(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

846 self.assertEqual(expect, result)

14551

28df4f45967fRemove "," from the list of always_safe characters. It is a reserved

Jeremy Hylton jeremy@alum.mit.edu

diff changeset

847

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

848 def test_doseq(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

849 # Test that passing True for 'doseq' parameter works correctly

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

850 given = {'sequence':['1', '2', '3']}

48334

851 expect = "sequence=%s" % urllib.parse.quote_plus(str(['1', '2', '3']))

852 result = urllib.parse.urlencode(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

853 self.assertEqual(expect, result)

48334

854 result = urllib.parse.urlencode(given, True)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

855 for value in given["sequence"]:

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

856 expect = "sequence=%s" % value

64013

1e7dcfc04061Merged revisions 76719,81270-81272,83294,83319,84038-84039 via svnmerge from

Florent Xicluna florent.xicluna@gmail.com

diff changeset

857 self.assertIn(expect, result)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

858 self.assertEqual(result.count('&'), 2,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

859 "Expected 2 '&'s, got %s" % result.count('&'))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

860

52343

861 def test_empty_sequence(self):

862 self.assertEqual("", urllib.parse.urlencode({}))

863 self.assertEqual("", urllib.parse.urlencode([]))

864

865 def test_nonstring_values(self):

866 self.assertEqual("a=1", urllib.parse.urlencode({"a": 1}))

867 self.assertEqual("a=None", urllib.parse.urlencode({"a": None}))

868

869 def test_nonstring_seq_values(self):

870 self.assertEqual("a=1&a=2", urllib.parse.urlencode({"a": [1, 2]}, True))

871 self.assertEqual("a=None&a=a",

872 urllib.parse.urlencode({"a": [None, "a"]}, True))

873 self.assertEqual("a=a&a=b",

874 urllib.parse.urlencode({"a": {"a": 1, "b": 1}}, True))

875

62749

876 def test_urlencode_encoding(self):

877 # ASCII encoding. Expect %3F with errors="replace'

878 given = (('\u00a0', '\u00c1'),)

879 expect = '%3F=%3F'

880 result = urllib.parse.urlencode(given, encoding="ASCII", errors="replace")

881 self.assertEqual(expect, result)

882

883 # Default is UTF-8 encoding.

884 given = (('\u00a0', '\u00c1'),)

885 expect = '%C2%A0=%C3%81'

886 result = urllib.parse.urlencode(given)

887 self.assertEqual(expect, result)

888

889 # Latin-1 encoding.

890 given = (('\u00a0', '\u00c1'),)

891 expect = '%A0=%C1'

892 result = urllib.parse.urlencode(given, encoding="latin-1")

893 self.assertEqual(expect, result)

894

895 def test_urlencode_encoding_doseq(self):

896 # ASCII Encoding. Expect %3F with errors="replace'

897 given = (('\u00a0', '\u00c1'),)

898 expect = '%3F=%3F'

899 result = urllib.parse.urlencode(given, doseq=True,

900 encoding="ASCII", errors="replace")

901 self.assertEqual(expect, result)

902

903 # ASCII Encoding. On a sequence of values.

904 given = (("\u00a0", (1, "\u00c1")),)

905 expect = '%3F=1&%3F=%3F'

906 result = urllib.parse.urlencode(given, True,

907 encoding="ASCII", errors="replace")

908 self.assertEqual(expect, result)

909

910 # Utf-8

911 given = (("\u00a0", "\u00c1"),)

912 expect = '%C2%A0=%C3%81'

913 result = urllib.parse.urlencode(given, True)

914 self.assertEqual(expect, result)

915

916 given = (("\u00a0", (42, "\u00c1")),)

917 expect = '%C2%A0=42&%C2%A0=%C3%81'

918 result = urllib.parse.urlencode(given, True)

919 self.assertEqual(expect, result)

920

921 # latin-1

922 given = (("\u00a0", "\u00c1"),)

923 expect = '%A0=%C1'

924 result = urllib.parse.urlencode(given, True, encoding="latin-1")

925 self.assertEqual(expect, result)

926

927 given = (("\u00a0", (42, "\u00c1")),)

928 expect = '%A0=42&%A0=%C1'

929 result = urllib.parse.urlencode(given, True, encoding="latin-1")

930 self.assertEqual(expect, result)

931

932 def test_urlencode_bytes(self):

933 given = ((b'\xa0\x24', b'\xc1\x24'),)

934 expect = '%A0%24=%C1%24'

935 result = urllib.parse.urlencode(given)

936 self.assertEqual(expect, result)

937 result = urllib.parse.urlencode(given, True)

938 self.assertEqual(expect, result)

939

940 # Sequence of values

941 given = ((b'\xa0\x24', (42, b'\xc1\x24')),)

942 expect = '%A0%24=42&%A0%24=%C1%24'

943 result = urllib.parse.urlencode(given, True)

944 self.assertEqual(expect, result)

945

946 def test_urlencode_encoding_safe_parameter(self):

947

948 # Send '$' (\x24) as safe character

949 # Default utf-8 encoding

950

951 given = ((b'\xa0\x24', b'\xc1\x24'),)

952 result = urllib.parse.urlencode(given, safe=":$")

953 expect = '%A0$=%C1$'

954 self.assertEqual(expect, result)

955

956 given = ((b'\xa0\x24', b'\xc1\x24'),)

957 result = urllib.parse.urlencode(given, doseq=True, safe=":$")

958 expect = '%A0$=%C1$'

959 self.assertEqual(expect, result)

960

961 # Safe parameter in sequence

962 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)

963 expect = '%A0$=%C1$&%A0$=13&%A0$=42'

964 result = urllib.parse.urlencode(given, True, safe=":$")

965 self.assertEqual(expect, result)

966

967 # Test all above in latin-1 encoding

968

969 given = ((b'\xa0\x24', b'\xc1\x24'),)

970 result = urllib.parse.urlencode(given, safe=":$",

971 encoding="latin-1")

972 expect = '%A0$=%C1$'

973 self.assertEqual(expect, result)

974

975 given = ((b'\xa0\x24', b'\xc1\x24'),)

976 expect = '%A0$=%C1$'

977 result = urllib.parse.urlencode(given, doseq=True, safe=":$",

978 encoding="latin-1")

979

980 given = ((b'\xa0\x24', (b'\xc1\x24', 0xd, 42)),)

981 expect = '%A0$=%C1$&%A0$=13&%A0$=42'

982 result = urllib.parse.urlencode(given, True, safe=":$",

983 encoding="latin-1")

984 self.assertEqual(expect, result)

985

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

986 class Pathname_Tests(unittest.TestCase):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

987 """Test pathname2url() and url2pathname()"""

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

988

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

989 def test_basic(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

990 # Make sure simple tests pass

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

991 expected_path = os.path.join("parts", "of", "a", "path")

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

992 expected_url = "parts/of/a/path"

48334

993 result = urllib.request.pathname2url(expected_path)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

994 self.assertEqual(expected_url, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

995 "pathname2url() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

996 (result, expected_url))

48334

997 result = urllib.request.url2pathname(expected_url)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

998 self.assertEqual(expected_path, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

999 "url2pathame() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1000 (result, expected_path))

14551

28df4f45967fRemove "," from the list of always_safe characters. It is a reserved

Jeremy Hylton jeremy@alum.mit.edu

diff changeset

1001

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1002 def test_quoting(self):

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1003 # Test automatic quoting and unquoting works for pathnam2url() and

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1004 # url2pathname() respectively

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1005 given = os.path.join("needs", "quot=ing", "here")

48334

1006 expect = "needs/%s/here" % urllib.parse.quote("quot=ing")

1007 result = urllib.request.pathname2url(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1008 self.assertEqual(expect, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1009 "pathname2url() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1010 (expect, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1011 expect = given

48334

1012 result = urllib.request.url2pathname(result)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1013 self.assertEqual(expect, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1014 "url2pathname() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1015 (expect, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1016 given = os.path.join("make sure", "using_quote")

48334

1017 expect = "%s/using_quote" % urllib.parse.quote("make sure")

1018 result = urllib.request.pathname2url(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1019 self.assertEqual(expect, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1020 "pathname2url() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1021 (expect, result))

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1022 given = "make+sure/using_unquote"

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1023 expect = os.path.join("make+sure", "using_unquote")

48334

1024 result = urllib.request.url2pathname(given)

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1025 self.assertEqual(expect, result,

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1026 "url2pathname() failed; %s != %s" %

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1027 (expect, result))

29087

1028

69348

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1029 @unittest.skipUnless(sys.platform == 'win32',

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1030 'test specific to the urllib.url2path function.')

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1031 def test_ntpath(self):

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1032 given = ('/C:/', '///C:/', '/C|//')

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1033 expect = 'C:\\'

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1034 for url in given:

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1035 result = urllib.request.url2pathname(url)

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1036 self.assertEqual(expect, result,

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1037 'urllib.request..url2pathname() failed; %s != %s' %

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1038 (expect, result))

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1039 given = '///C|/path'

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1040 expect = 'C:\\path'

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1041 result = urllib.request.url2pathname(given)

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1042 self.assertEqual(expect, result,

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1043 'urllib.request.url2pathname() failed; %s != %s' %

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1044 (expect, result))

de0da2759c8cFix Issue11474 - fix url2pathname() handling of '/C|/' on Windows

Senthil Kumaran orsenthil@gmail.com

diff changeset

1045

52478

1046 class Utility_Tests(unittest.TestCase):

1047 """Testcase to test the various utility functions in the urllib."""

1048

1049 def test_splitpasswd(self):

1050 """Some of password examples are not sensible, but it is added to

1051 confirming to RFC2617 and addressing issue4675.

1052 """

1053 self.assertEqual(('user', 'ab'),urllib.parse.splitpasswd('user:ab'))

1054 self.assertEqual(('user', 'a\nb'),urllib.parse.splitpasswd('user:a\nb'))

1055 self.assertEqual(('user', 'a\tb'),urllib.parse.splitpasswd('user:a\tb'))

1056 self.assertEqual(('user', 'a\rb'),urllib.parse.splitpasswd('user:a\rb'))

1057 self.assertEqual(('user', 'a\fb'),urllib.parse.splitpasswd('user:a\fb'))

1058 self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))

1059 self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))

1060

72695

805a0a1e3c2bIssue13104 - Fix urllib.request.thishost() utility function.

Senthil Kumaran senthil@uthcode.com

diff changeset

1061 def test_thishost(self):

805a0a1e3c2bIssue13104 - Fix urllib.request.thishost() utility function.

Senthil Kumaran senthil@uthcode.com

diff changeset

1062 """Test the urllib.request.thishost utility function returns a tuple"""

805a0a1e3c2bIssue13104 - Fix urllib.request.thishost() utility function.

Senthil Kumaran senthil@uthcode.com

diff changeset

1063 self.assertIsInstance(urllib.request.thishost(), tuple)

805a0a1e3c2bIssue13104 - Fix urllib.request.thishost() utility function.

Senthil Kumaran senthil@uthcode.com

diff changeset

1064

53805

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1065

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1066 class URLopener_Tests(unittest.TestCase):

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1067 """Testcase to test the open method of URLopener class."""

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1068

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1069 def test_quoted_open(self):

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1070 class DummyURLopener(urllib.request.URLopener):

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1071 def open_spam(self, url):

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1072 return url

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1073

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1074 self.assertEqual(DummyURLopener().open(

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1075 'spam://example/ /'),'//example/%20/')

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1076

58970

1077 # test the safe characters are not quoted by urlopen

1078 self.assertEqual(DummyURLopener().open(

1079 "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),

1080 "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")

1081

41969

1082 # Just commented them out.

1083 # Can't really tell why keep failing in windows and sparc.

68585

1084 # Everywhere else they work ok, but on those machines, sometimes

41969

1085 # fail in one of the tests, sometimes in other. I have a linux, and

1086 # the tests go ok.

1087 # If anybody has one of the problematic enviroments, please help!

1088 # . Facundo

1089 #

1090 # def server(evt):

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1091 # import socket, time

41969

1092 # serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

1093 # serv.settimeout(3)

1094 # serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

1095 # serv.bind(("", 9093))

1096 # serv.listen(5)

1097 # try:

1098 # conn, addr = serv.accept()

1099 # conn.send("1 Hola mundo\n")

1100 # cantdata = 0

1101 # while cantdata < 13:

1102 # data = conn.recv(13-cantdata)

1103 # cantdata += len(data)

1104 # time.sleep(.3)

1105 # conn.send("2 No more lines\n")

1106 # conn.close()

1107 # except socket.timeout:

1108 # pass

1109 # finally:

1110 # serv.close()

1111 # evt.set()

1112 #

1113 # class FTPWrapperTests(unittest.TestCase):

1114 #

1115 # def setUp(self):

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1116 # import ftplib, time, threading

41969

1117 # ftplib.FTP.port = 9093

1118 # self.evt = threading.Event()

1119 # threading.Thread(target=server, args=(self.evt,)).start()

1120 # time.sleep(.1)

1121 #

1122 # def tearDown(self):

1123 # self.evt.wait()

1124 #

1125 # def testBasic(self):

1126 # # connects

1127 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1128 # ftp.close()

41969

1129 #

1130 # def testTimeoutNone(self):

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1131 # # global default timeout is ignored

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1132 # import socket

55593

1133 # self.assertTrue(socket.getdefaulttimeout() is None)

41969

1134 # socket.setdefaulttimeout(30)

1135 # try:

1136 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])

1137 # finally:

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1138 # socket.setdefaulttimeout(None)

41969

1139 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30)

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1140 # ftp.close()

41969

1141 #

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1142 # def testTimeoutDefault(self):

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1143 # # global default timeout is used

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1144 # import socket

55593

1145 # self.assertTrue(socket.getdefaulttimeout() is None)

47559

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1146 # socket.setdefaulttimeout(30)

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1147 # try:

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1148 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1149 # finally:

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1150 # socket.setdefaulttimeout(None)

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1151 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30)

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1152 # ftp.close()

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1153 #

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1154 # def testTimeoutValue(self):

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1155 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1156 # timeout=30)

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1157 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30)

2925b568aaccMerged revisions 63562,63570,63728,63734,63784,63788,63802,63817,63827,63839,63887,63975,63998 via svnmerge from

Georg Brandl georg@python.org

diff changeset

1158 # ftp.close()

41969

1159

72945

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1160 class RequestTests(unittest.TestCase):

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1161 """Unit tests for urllib.request.Request."""

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1162

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1163 def test_default_values(self):

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1164 Request = urllib.request.Request

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1165 request = Request("http://www.python.org")

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1166 self.assertEqual(request.get_method(), 'GET')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1167 request = Request("http://www.python.org", {})

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1168 self.assertEqual(request.get_method(), 'POST')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1169

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1170 def test_with_method_arg(self):

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1171 Request = urllib.request.Request

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1172 request = Request("http://www.python.org", method='HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1173 self.assertEqual(request.method, 'HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1174 self.assertEqual(request.get_method(), 'HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1175 request = Request("http://www.python.org", {}, method='HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1176 self.assertEqual(request.method, 'HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1177 self.assertEqual(request.get_method(), 'HEAD')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1178 request = Request("http://www.python.org", method='GET')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1179 self.assertEqual(request.get_method(), 'GET')

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1180 request.method = 'HEAD'

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1181 self.assertEqual(request.get_method(), 'HEAD')

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1182

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1183

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1184 def test_main():

47201

1185 support.run_unittest(

28917

1186 urlopen_FileTests,

32252

a72bc4257336Fix a bug that robotparser starves memory when the server responses

Hye-Shik Chang hyeshik@gmail.com

diff changeset

1187 urlopen_HttpTests,

28917

1188 urlretrieve_FileTests,

49274

1d5c3bcf35adMerged revisions 66508,66510,66512-66513,66523-66526,66529-66530,66532,66535,66538,66544,66546 via svnmerge from

Benjamin Peterson benjamin@python.org

diff changeset

1189 ProxyTests,

28917

1190 QuotingTests,

1191 UnquotingTests,

1192 urlencode_Tests,

41969

1193 Pathname_Tests,

52478

1194 Utility_Tests,

53805

bbe2aa431692Fix for issue1153027, making Py3k changes similar to fix in issue918368.

Senthil Kumaran orsenthil@gmail.com

diff changeset

1195 URLopener_Tests,

41969

1196 #FTPWrapperTests,

72945

0a0aafaa9bf5Fix closes issue 1673007 urllib.request to support HEAD requests with a new method arg.

Senthil Kumaran senthil@uthcode.com

diff changeset

1197 RequestTests,

28917

1198 )

16071

1199

16325

1200

1201

28821

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1202 if __name__ == '__main__':

64cb657b0338Complete rewrite of module. Only has tests using temporary files; net tests

Brett Cannon bcannon@gmail.com

diff changeset

1203 test_main()