|
1 """Regresssion tests for urllib""" |
|
2 |
|
3 import urllib |
|
4 import httplib |
|
5 import unittest |
|
6 from test import test_support |
|
7 import os |
|
8 import mimetools |
|
9 import tempfile |
|
10 import StringIO |
|
11 |
|
12 def hexescape(char): |
|
13 """Escape char as RFC 2396 specifies""" |
|
14 hex_repr = hex(ord(char))[2:].upper() |
|
15 if len(hex_repr) == 1: |
|
16 hex_repr = "0%s" % hex_repr |
|
17 return "%" + hex_repr |
|
18 |
|
19 class urlopen_FileTests(unittest.TestCase): |
|
20 """Test urlopen() opening a temporary file. |
|
21 |
|
22 Try to test as much functionality as possible so as to cut down on reliance |
|
23 on connecting to the Net for testing. |
|
24 |
|
25 """ |
|
26 |
|
27 def setUp(self): |
|
28 """Setup of a temp file to use for testing""" |
|
29 self.text = "test_urllib: %s\n" % self.__class__.__name__ |
|
30 FILE = file(test_support.TESTFN, 'wb') |
|
31 try: |
|
32 FILE.write(self.text) |
|
33 finally: |
|
34 FILE.close() |
|
35 self.pathname = test_support.TESTFN |
|
36 self.returned_obj = urllib.urlopen("file:%s" % self.pathname) |
|
37 |
|
38 def tearDown(self): |
|
39 """Shut down the open object""" |
|
40 self.returned_obj.close() |
|
41 os.remove(test_support.TESTFN) |
|
42 |
|
43 def test_interface(self): |
|
44 # Make sure object returned by urlopen() has the specified methods |
|
45 for attr in ("read", "readline", "readlines", "fileno", |
|
46 "close", "info", "geturl", "getcode", "__iter__"): |
|
47 self.assert_(hasattr(self.returned_obj, attr), |
|
48 "object returned by urlopen() lacks %s attribute" % |
|
49 attr) |
|
50 |
|
51 def test_read(self): |
|
52 self.assertEqual(self.text, self.returned_obj.read()) |
|
53 |
|
54 def test_readline(self): |
|
55 self.assertEqual(self.text, self.returned_obj.readline()) |
|
56 self.assertEqual('', self.returned_obj.readline(), |
|
57 "calling readline() after exhausting the file did not" |
|
58 " return an empty string") |
|
59 |
|
60 def test_readlines(self): |
|
61 lines_list = self.returned_obj.readlines() |
|
62 self.assertEqual(len(lines_list), 1, |
|
63 "readlines() returned the wrong number of lines") |
|
64 self.assertEqual(lines_list[0], self.text, |
|
65 "readlines() returned improper text") |
|
66 |
|
67 def test_fileno(self): |
|
68 file_num = self.returned_obj.fileno() |
|
69 self.assert_(isinstance(file_num, int), |
|
70 "fileno() did not return an int") |
|
71 self.assertEqual(os.read(file_num, len(self.text)), self.text, |
|
72 "Reading on the file descriptor returned by fileno() " |
|
73 "did not return the expected text") |
|
74 |
|
75 def test_close(self): |
|
76 # Test close() by calling it hear and then having it be called again |
|
77 # by the tearDown() method for the test |
|
78 self.returned_obj.close() |
|
79 |
|
80 def test_info(self): |
|
81 self.assert_(isinstance(self.returned_obj.info(), mimetools.Message)) |
|
82 |
|
83 def test_geturl(self): |
|
84 self.assertEqual(self.returned_obj.geturl(), self.pathname) |
|
85 |
|
86 def test_getcode(self): |
|
87 self.assertEqual(self.returned_obj.getcode(), None) |
|
88 |
|
89 def test_iter(self): |
|
90 # Test iterator |
|
91 # Don't need to count number of iterations since test would fail the |
|
92 # instant it returned anything beyond the first line from the |
|
93 # comparison |
|
94 for line in self.returned_obj.__iter__(): |
|
95 self.assertEqual(line, self.text) |
|
96 |
|
97 |
|
98 class ProxyTests(unittest.TestCase): |
|
99 |
|
100 def setUp(self): |
|
101 # Save all proxy related env vars |
|
102 self._saved_environ = dict([(k, v) for k, v in os.environ.iteritems() |
|
103 if k.lower().find('proxy') >= 0]) |
|
104 # Delete all proxy related env vars |
|
105 for k in self._saved_environ: |
|
106 del os.environ[k] |
|
107 |
|
108 def tearDown(self): |
|
109 # Restore all proxy related env vars |
|
110 for k, v in self._saved_environ.iteritems(): |
|
111 os.environ[k] = v |
|
112 |
|
113 def test_getproxies_environment_keep_no_proxies(self): |
|
114 os.environ['NO_PROXY'] = 'localhost' |
|
115 proxies = urllib.getproxies_environment() |
|
116 # getproxies_environment use lowered case truncated (no '_proxy') keys |
|
117 self.assertEquals('localhost', proxies['no']) |
|
118 |
|
119 |
|
120 class urlopen_HttpTests(unittest.TestCase): |
|
121 """Test urlopen() opening a fake http connection.""" |
|
122 |
|
123 def fakehttp(self, fakedata): |
|
124 class FakeSocket(StringIO.StringIO): |
|
125 def sendall(self, str): pass |
|
126 def makefile(self, mode, name): return self |
|
127 def read(self, amt=None): |
|
128 if self.closed: return '' |
|
129 return StringIO.StringIO.read(self, amt) |
|
130 def readline(self, length=None): |
|
131 if self.closed: return '' |
|
132 return StringIO.StringIO.readline(self, length) |
|
133 class FakeHTTPConnection(httplib.HTTPConnection): |
|
134 def connect(self): |
|
135 self.sock = FakeSocket(fakedata) |
|
136 assert httplib.HTTP._connection_class == httplib.HTTPConnection |
|
137 httplib.HTTP._connection_class = FakeHTTPConnection |
|
138 |
|
139 def unfakehttp(self): |
|
140 httplib.HTTP._connection_class = httplib.HTTPConnection |
|
141 |
|
142 def test_read(self): |
|
143 self.fakehttp('Hello!') |
|
144 try: |
|
145 fp = urllib.urlopen("http://python.org/") |
|
146 self.assertEqual(fp.readline(), 'Hello!') |
|
147 self.assertEqual(fp.readline(), '') |
|
148 self.assertEqual(fp.geturl(), 'http://python.org/') |
|
149 self.assertEqual(fp.getcode(), 200) |
|
150 finally: |
|
151 self.unfakehttp() |
|
152 |
|
153 def test_read_bogus(self): |
|
154 # urlopen() should raise IOError for many error codes. |
|
155 self.fakehttp('''HTTP/1.1 401 Authentication Required |
|
156 Date: Wed, 02 Jan 2008 03:03:54 GMT |
|
157 Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e |
|
158 Connection: close |
|
159 Content-Type: text/html; charset=iso-8859-1 |
|
160 ''') |
|
161 try: |
|
162 self.assertRaises(IOError, urllib.urlopen, "http://python.org/") |
|
163 finally: |
|
164 self.unfakehttp() |
|
165 |
|
166 def test_empty_socket(self): |
|
167 # urlopen() raises IOError if the underlying socket does not send any |
|
168 # data. (#1680230) |
|
169 self.fakehttp('') |
|
170 try: |
|
171 self.assertRaises(IOError, urllib.urlopen, 'http://something') |
|
172 finally: |
|
173 self.unfakehttp() |
|
174 |
|
175 class urlretrieve_FileTests(unittest.TestCase): |
|
176 """Test urllib.urlretrieve() on local files""" |
|
177 |
|
178 def setUp(self): |
|
179 # Create a list of temporary files. Each item in the list is a file |
|
180 # name (absolute path or relative to the current working directory). |
|
181 # All files in this list will be deleted in the tearDown method. Note, |
|
182 # this only helps to makes sure temporary files get deleted, but it |
|
183 # does nothing about trying to close files that may still be open. It |
|
184 # is the responsibility of the developer to properly close files even |
|
185 # when exceptional conditions occur. |
|
186 self.tempFiles = [] |
|
187 |
|
188 # Create a temporary file. |
|
189 self.registerFileForCleanUp(test_support.TESTFN) |
|
190 self.text = 'testing urllib.urlretrieve' |
|
191 try: |
|
192 FILE = file(test_support.TESTFN, 'wb') |
|
193 FILE.write(self.text) |
|
194 FILE.close() |
|
195 finally: |
|
196 try: FILE.close() |
|
197 except: pass |
|
198 |
|
199 def tearDown(self): |
|
200 # Delete the temporary files. |
|
201 for each in self.tempFiles: |
|
202 try: os.remove(each) |
|
203 except: pass |
|
204 |
|
205 def constructLocalFileUrl(self, filePath): |
|
206 return "file://%s" % urllib.pathname2url(os.path.abspath(filePath)) |
|
207 |
|
208 def createNewTempFile(self, data=""): |
|
209 """Creates a new temporary file containing the specified data, |
|
210 registers the file for deletion during the test fixture tear down, and |
|
211 returns the absolute path of the file.""" |
|
212 |
|
213 newFd, newFilePath = tempfile.mkstemp() |
|
214 try: |
|
215 self.registerFileForCleanUp(newFilePath) |
|
216 newFile = os.fdopen(newFd, "wb") |
|
217 newFile.write(data) |
|
218 newFile.close() |
|
219 finally: |
|
220 try: newFile.close() |
|
221 except: pass |
|
222 return newFilePath |
|
223 |
|
224 def registerFileForCleanUp(self, fileName): |
|
225 self.tempFiles.append(fileName) |
|
226 |
|
227 def test_basic(self): |
|
228 # Make sure that a local file just gets its own location returned and |
|
229 # a headers value is returned. |
|
230 result = urllib.urlretrieve("file:%s" % test_support.TESTFN) |
|
231 self.assertEqual(result[0], test_support.TESTFN) |
|
232 self.assert_(isinstance(result[1], mimetools.Message), |
|
233 "did not get a mimetools.Message instance as second " |
|
234 "returned value") |
|
235 |
|
236 def test_copy(self): |
|
237 # Test that setting the filename argument works. |
|
238 second_temp = "%s.2" % test_support.TESTFN |
|
239 self.registerFileForCleanUp(second_temp) |
|
240 result = urllib.urlretrieve(self.constructLocalFileUrl( |
|
241 test_support.TESTFN), second_temp) |
|
242 self.assertEqual(second_temp, result[0]) |
|
243 self.assert_(os.path.exists(second_temp), "copy of the file was not " |
|
244 "made") |
|
245 FILE = file(second_temp, 'rb') |
|
246 try: |
|
247 text = FILE.read() |
|
248 FILE.close() |
|
249 finally: |
|
250 try: FILE.close() |
|
251 except: pass |
|
252 self.assertEqual(self.text, text) |
|
253 |
|
254 def test_reporthook(self): |
|
255 # Make sure that the reporthook works. |
|
256 def hooktester(count, block_size, total_size, count_holder=[0]): |
|
257 self.assert_(isinstance(count, int)) |
|
258 self.assert_(isinstance(block_size, int)) |
|
259 self.assert_(isinstance(total_size, int)) |
|
260 self.assertEqual(count, count_holder[0]) |
|
261 count_holder[0] = count_holder[0] + 1 |
|
262 second_temp = "%s.2" % test_support.TESTFN |
|
263 self.registerFileForCleanUp(second_temp) |
|
264 urllib.urlretrieve(self.constructLocalFileUrl(test_support.TESTFN), |
|
265 second_temp, hooktester) |
|
266 |
|
267 def test_reporthook_0_bytes(self): |
|
268 # Test on zero length file. Should call reporthook only 1 time. |
|
269 report = [] |
|
270 def hooktester(count, block_size, total_size, _report=report): |
|
271 _report.append((count, block_size, total_size)) |
|
272 srcFileName = self.createNewTempFile() |
|
273 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
|
274 test_support.TESTFN, hooktester) |
|
275 self.assertEqual(len(report), 1) |
|
276 self.assertEqual(report[0][2], 0) |
|
277 |
|
278 def test_reporthook_5_bytes(self): |
|
279 # Test on 5 byte file. Should call reporthook only 2 times (once when |
|
280 # the "network connection" is established and once when the block is |
|
281 # read). Since the block size is 8192 bytes, only one block read is |
|
282 # required to read the entire file. |
|
283 report = [] |
|
284 def hooktester(count, block_size, total_size, _report=report): |
|
285 _report.append((count, block_size, total_size)) |
|
286 srcFileName = self.createNewTempFile("x" * 5) |
|
287 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
|
288 test_support.TESTFN, hooktester) |
|
289 self.assertEqual(len(report), 2) |
|
290 self.assertEqual(report[0][1], 8192) |
|
291 self.assertEqual(report[0][2], 5) |
|
292 |
|
293 def test_reporthook_8193_bytes(self): |
|
294 # Test on 8193 byte file. Should call reporthook only 3 times (once |
|
295 # when the "network connection" is established, once for the next 8192 |
|
296 # bytes, and once for the last byte). |
|
297 report = [] |
|
298 def hooktester(count, block_size, total_size, _report=report): |
|
299 _report.append((count, block_size, total_size)) |
|
300 srcFileName = self.createNewTempFile("x" * 8193) |
|
301 urllib.urlretrieve(self.constructLocalFileUrl(srcFileName), |
|
302 test_support.TESTFN, hooktester) |
|
303 self.assertEqual(len(report), 3) |
|
304 self.assertEqual(report[0][1], 8192) |
|
305 self.assertEqual(report[0][2], 8193) |
|
306 |
|
307 class QuotingTests(unittest.TestCase): |
|
308 """Tests for urllib.quote() and urllib.quote_plus() |
|
309 |
|
310 According to RFC 2396 ("Uniform Resource Identifiers), to escape a |
|
311 character you write it as '%' + <2 character US-ASCII hex value>. The Python |
|
312 code of ``'%' + hex(ord(<character>))[2:]`` escapes a character properly. |
|
313 Case does not matter on the hex letters. |
|
314 |
|
315 The various character sets specified are: |
|
316 |
|
317 Reserved characters : ";/?:@&=+$," |
|
318 Have special meaning in URIs and must be escaped if not being used for |
|
319 their special meaning |
|
320 Data characters : letters, digits, and "-_.!~*'()" |
|
321 Unreserved and do not need to be escaped; can be, though, if desired |
|
322 Control characters : 0x00 - 0x1F, 0x7F |
|
323 Have no use in URIs so must be escaped |
|
324 space : 0x20 |
|
325 Must be escaped |
|
326 Delimiters : '<>#%"' |
|
327 Must be escaped |
|
328 Unwise : "{}|\^[]`" |
|
329 Must be escaped |
|
330 |
|
331 """ |
|
332 |
|
333 def test_never_quote(self): |
|
334 # Make sure quote() does not quote letters, digits, and "_,.-" |
|
335 do_not_quote = '' .join(["ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
|
336 "abcdefghijklmnopqrstuvwxyz", |
|
337 "0123456789", |
|
338 "_.-"]) |
|
339 result = urllib.quote(do_not_quote) |
|
340 self.assertEqual(do_not_quote, result, |
|
341 "using quote(): %s != %s" % (do_not_quote, result)) |
|
342 result = urllib.quote_plus(do_not_quote) |
|
343 self.assertEqual(do_not_quote, result, |
|
344 "using quote_plus(): %s != %s" % (do_not_quote, result)) |
|
345 |
|
346 def test_default_safe(self): |
|
347 # Test '/' is default value for 'safe' parameter |
|
348 self.assertEqual(urllib.quote.func_defaults[0], '/') |
|
349 |
|
350 def test_safe(self): |
|
351 # Test setting 'safe' parameter does what it should do |
|
352 quote_by_default = "<>" |
|
353 result = urllib.quote(quote_by_default, safe=quote_by_default) |
|
354 self.assertEqual(quote_by_default, result, |
|
355 "using quote(): %s != %s" % (quote_by_default, result)) |
|
356 result = urllib.quote_plus(quote_by_default, safe=quote_by_default) |
|
357 self.assertEqual(quote_by_default, result, |
|
358 "using quote_plus(): %s != %s" % |
|
359 (quote_by_default, result)) |
|
360 |
|
361 def test_default_quoting(self): |
|
362 # Make sure all characters that should be quoted are by default sans |
|
363 # space (separate test for that). |
|
364 should_quote = [chr(num) for num in range(32)] # For 0x00 - 0x1F |
|
365 should_quote.append('<>#%"{}|\^[]`') |
|
366 should_quote.append(chr(127)) # For 0x7F |
|
367 should_quote = ''.join(should_quote) |
|
368 for char in should_quote: |
|
369 result = urllib.quote(char) |
|
370 self.assertEqual(hexescape(char), result, |
|
371 "using quote(): %s should be escaped to %s, not %s" % |
|
372 (char, hexescape(char), result)) |
|
373 result = urllib.quote_plus(char) |
|
374 self.assertEqual(hexescape(char), result, |
|
375 "using quote_plus(): " |
|
376 "%s should be escapes to %s, not %s" % |
|
377 (char, hexescape(char), result)) |
|
378 del should_quote |
|
379 partial_quote = "ab[]cd" |
|
380 expected = "ab%5B%5Dcd" |
|
381 result = urllib.quote(partial_quote) |
|
382 self.assertEqual(expected, result, |
|
383 "using quote(): %s != %s" % (expected, result)) |
|
384 self.assertEqual(expected, result, |
|
385 "using quote_plus(): %s != %s" % (expected, result)) |
|
386 |
|
387 def test_quoting_space(self): |
|
388 # Make sure quote() and quote_plus() handle spaces as specified in |
|
389 # their unique way |
|
390 result = urllib.quote(' ') |
|
391 self.assertEqual(result, hexescape(' '), |
|
392 "using quote(): %s != %s" % (result, hexescape(' '))) |
|
393 result = urllib.quote_plus(' ') |
|
394 self.assertEqual(result, '+', |
|
395 "using quote_plus(): %s != +" % result) |
|
396 given = "a b cd e f" |
|
397 expect = given.replace(' ', hexescape(' ')) |
|
398 result = urllib.quote(given) |
|
399 self.assertEqual(expect, result, |
|
400 "using quote(): %s != %s" % (expect, result)) |
|
401 expect = given.replace(' ', '+') |
|
402 result = urllib.quote_plus(given) |
|
403 self.assertEqual(expect, result, |
|
404 "using quote_plus(): %s != %s" % (expect, result)) |
|
405 |
|
406 def test_quoting_plus(self): |
|
407 self.assertEqual(urllib.quote_plus('alpha+beta gamma'), |
|
408 'alpha%2Bbeta+gamma') |
|
409 self.assertEqual(urllib.quote_plus('alpha+beta gamma', '+'), |
|
410 'alpha+beta+gamma') |
|
411 |
|
412 class UnquotingTests(unittest.TestCase): |
|
413 """Tests for unquote() and unquote_plus() |
|
414 |
|
415 See the doc string for quoting_Tests for details on quoting and such. |
|
416 |
|
417 """ |
|
418 |
|
419 def test_unquoting(self): |
|
420 # Make sure unquoting of all ASCII values works |
|
421 escape_list = [] |
|
422 for num in range(128): |
|
423 given = hexescape(chr(num)) |
|
424 expect = chr(num) |
|
425 result = urllib.unquote(given) |
|
426 self.assertEqual(expect, result, |
|
427 "using unquote(): %s != %s" % (expect, result)) |
|
428 result = urllib.unquote_plus(given) |
|
429 self.assertEqual(expect, result, |
|
430 "using unquote_plus(): %s != %s" % |
|
431 (expect, result)) |
|
432 escape_list.append(given) |
|
433 escape_string = ''.join(escape_list) |
|
434 del escape_list |
|
435 result = urllib.unquote(escape_string) |
|
436 self.assertEqual(result.count('%'), 1, |
|
437 "using quote(): not all characters escaped; %s" % |
|
438 result) |
|
439 result = urllib.unquote(escape_string) |
|
440 self.assertEqual(result.count('%'), 1, |
|
441 "using unquote(): not all characters escaped: " |
|
442 "%s" % result) |
|
443 |
|
444 def test_unquoting_parts(self): |
|
445 # Make sure unquoting works when have non-quoted characters |
|
446 # interspersed |
|
447 given = 'ab%sd' % hexescape('c') |
|
448 expect = "abcd" |
|
449 result = urllib.unquote(given) |
|
450 self.assertEqual(expect, result, |
|
451 "using quote(): %s != %s" % (expect, result)) |
|
452 result = urllib.unquote_plus(given) |
|
453 self.assertEqual(expect, result, |
|
454 "using unquote_plus(): %s != %s" % (expect, result)) |
|
455 |
|
456 def test_unquoting_plus(self): |
|
457 # Test difference between unquote() and unquote_plus() |
|
458 given = "are+there+spaces..." |
|
459 expect = given |
|
460 result = urllib.unquote(given) |
|
461 self.assertEqual(expect, result, |
|
462 "using unquote(): %s != %s" % (expect, result)) |
|
463 expect = given.replace('+', ' ') |
|
464 result = urllib.unquote_plus(given) |
|
465 self.assertEqual(expect, result, |
|
466 "using unquote_plus(): %s != %s" % (expect, result)) |
|
467 |
|
468 def test_unquote_with_unicode(self): |
|
469 r = urllib.unquote(u'br%C3%BCckner_sapporo_20050930.doc') |
|
470 self.assertEqual(r, u'br\xc3\xbcckner_sapporo_20050930.doc') |
|
471 |
|
472 class urlencode_Tests(unittest.TestCase): |
|
473 """Tests for urlencode()""" |
|
474 |
|
475 def help_inputtype(self, given, test_type): |
|
476 """Helper method for testing different input types. |
|
477 |
|
478 'given' must lead to only the pairs: |
|
479 * 1st, 1 |
|
480 * 2nd, 2 |
|
481 * 3rd, 3 |
|
482 |
|
483 Test cannot assume anything about order. Docs make no guarantee and |
|
484 have possible dictionary input. |
|
485 |
|
486 """ |
|
487 expect_somewhere = ["1st=1", "2nd=2", "3rd=3"] |
|
488 result = urllib.urlencode(given) |
|
489 for expected in expect_somewhere: |
|
490 self.assert_(expected in result, |
|
491 "testing %s: %s not found in %s" % |
|
492 (test_type, expected, result)) |
|
493 self.assertEqual(result.count('&'), 2, |
|
494 "testing %s: expected 2 '&'s; got %s" % |
|
495 (test_type, result.count('&'))) |
|
496 amp_location = result.index('&') |
|
497 on_amp_left = result[amp_location - 1] |
|
498 on_amp_right = result[amp_location + 1] |
|
499 self.assert_(on_amp_left.isdigit() and on_amp_right.isdigit(), |
|
500 "testing %s: '&' not located in proper place in %s" % |
|
501 (test_type, result)) |
|
502 self.assertEqual(len(result), (5 * 3) + 2, #5 chars per thing and amps |
|
503 "testing %s: " |
|
504 "unexpected number of characters: %s != %s" % |
|
505 (test_type, len(result), (5 * 3) + 2)) |
|
506 |
|
507 def test_using_mapping(self): |
|
508 # Test passing in a mapping object as an argument. |
|
509 self.help_inputtype({"1st":'1', "2nd":'2', "3rd":'3'}, |
|
510 "using dict as input type") |
|
511 |
|
512 def test_using_sequence(self): |
|
513 # Test passing in a sequence of two-item sequences as an argument. |
|
514 self.help_inputtype([('1st', '1'), ('2nd', '2'), ('3rd', '3')], |
|
515 "using sequence of two-item tuples as input") |
|
516 |
|
517 def test_quoting(self): |
|
518 # Make sure keys and values are quoted using quote_plus() |
|
519 given = {"&":"="} |
|
520 expect = "%s=%s" % (hexescape('&'), hexescape('=')) |
|
521 result = urllib.urlencode(given) |
|
522 self.assertEqual(expect, result) |
|
523 given = {"key name":"A bunch of pluses"} |
|
524 expect = "key+name=A+bunch+of+pluses" |
|
525 result = urllib.urlencode(given) |
|
526 self.assertEqual(expect, result) |
|
527 |
|
528 def test_doseq(self): |
|
529 # Test that passing True for 'doseq' parameter works correctly |
|
530 given = {'sequence':['1', '2', '3']} |
|
531 expect = "sequence=%s" % urllib.quote_plus(str(['1', '2', '3'])) |
|
532 result = urllib.urlencode(given) |
|
533 self.assertEqual(expect, result) |
|
534 result = urllib.urlencode(given, True) |
|
535 for value in given["sequence"]: |
|
536 expect = "sequence=%s" % value |
|
537 self.assert_(expect in result, |
|
538 "%s not found in %s" % (expect, result)) |
|
539 self.assertEqual(result.count('&'), 2, |
|
540 "Expected 2 '&'s, got %s" % result.count('&')) |
|
541 |
|
542 class Pathname_Tests(unittest.TestCase): |
|
543 """Test pathname2url() and url2pathname()""" |
|
544 |
|
545 def test_basic(self): |
|
546 # Make sure simple tests pass |
|
547 expected_path = os.path.join("parts", "of", "a", "path") |
|
548 expected_url = "parts/of/a/path" |
|
549 result = urllib.pathname2url(expected_path) |
|
550 self.assertEqual(expected_url, result, |
|
551 "pathname2url() failed; %s != %s" % |
|
552 (result, expected_url)) |
|
553 result = urllib.url2pathname(expected_url) |
|
554 self.assertEqual(expected_path, result, |
|
555 "url2pathame() failed; %s != %s" % |
|
556 (result, expected_path)) |
|
557 |
|
558 def test_quoting(self): |
|
559 # Test automatic quoting and unquoting works for pathnam2url() and |
|
560 # url2pathname() respectively |
|
561 given = os.path.join("needs", "quot=ing", "here") |
|
562 expect = "needs/%s/here" % urllib.quote("quot=ing") |
|
563 result = urllib.pathname2url(given) |
|
564 self.assertEqual(expect, result, |
|
565 "pathname2url() failed; %s != %s" % |
|
566 (expect, result)) |
|
567 expect = given |
|
568 result = urllib.url2pathname(result) |
|
569 self.assertEqual(expect, result, |
|
570 "url2pathname() failed; %s != %s" % |
|
571 (expect, result)) |
|
572 given = os.path.join("make sure", "using_quote") |
|
573 expect = "%s/using_quote" % urllib.quote("make sure") |
|
574 result = urllib.pathname2url(given) |
|
575 self.assertEqual(expect, result, |
|
576 "pathname2url() failed; %s != %s" % |
|
577 (expect, result)) |
|
578 given = "make+sure/using_unquote" |
|
579 expect = os.path.join("make+sure", "using_unquote") |
|
580 result = urllib.url2pathname(given) |
|
581 self.assertEqual(expect, result, |
|
582 "url2pathname() failed; %s != %s" % |
|
583 (expect, result)) |
|
584 |
|
585 # Just commented them out. |
|
586 # Can't really tell why keep failing in windows and sparc. |
|
587 # Everywhere else they work ok, but on those machines, someteimes |
|
588 # fail in one of the tests, sometimes in other. I have a linux, and |
|
589 # the tests go ok. |
|
590 # If anybody has one of the problematic enviroments, please help! |
|
591 # . Facundo |
|
592 # |
|
593 # def server(evt): |
|
594 # import socket, time |
|
595 # serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
596 # serv.settimeout(3) |
|
597 # serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
598 # serv.bind(("", 9093)) |
|
599 # serv.listen(5) |
|
600 # try: |
|
601 # conn, addr = serv.accept() |
|
602 # conn.send("1 Hola mundo\n") |
|
603 # cantdata = 0 |
|
604 # while cantdata < 13: |
|
605 # data = conn.recv(13-cantdata) |
|
606 # cantdata += len(data) |
|
607 # time.sleep(.3) |
|
608 # conn.send("2 No more lines\n") |
|
609 # conn.close() |
|
610 # except socket.timeout: |
|
611 # pass |
|
612 # finally: |
|
613 # serv.close() |
|
614 # evt.set() |
|
615 # |
|
616 # class FTPWrapperTests(unittest.TestCase): |
|
617 # |
|
618 # def setUp(self): |
|
619 # import ftplib, time, threading |
|
620 # ftplib.FTP.port = 9093 |
|
621 # self.evt = threading.Event() |
|
622 # threading.Thread(target=server, args=(self.evt,)).start() |
|
623 # time.sleep(.1) |
|
624 # |
|
625 # def tearDown(self): |
|
626 # self.evt.wait() |
|
627 # |
|
628 # def testBasic(self): |
|
629 # # connects |
|
630 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
|
631 # ftp.close() |
|
632 # |
|
633 # def testTimeoutNone(self): |
|
634 # # global default timeout is ignored |
|
635 # import socket |
|
636 # self.assert_(socket.getdefaulttimeout() is None) |
|
637 # socket.setdefaulttimeout(30) |
|
638 # try: |
|
639 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
|
640 # finally: |
|
641 # socket.setdefaulttimeout(None) |
|
642 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
|
643 # ftp.close() |
|
644 # |
|
645 # def testTimeoutDefault(self): |
|
646 # # global default timeout is used |
|
647 # import socket |
|
648 # self.assert_(socket.getdefaulttimeout() is None) |
|
649 # socket.setdefaulttimeout(30) |
|
650 # try: |
|
651 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, []) |
|
652 # finally: |
|
653 # socket.setdefaulttimeout(None) |
|
654 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
|
655 # ftp.close() |
|
656 # |
|
657 # def testTimeoutValue(self): |
|
658 # ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [], |
|
659 # timeout=30) |
|
660 # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) |
|
661 # ftp.close() |
|
662 |
|
663 |
|
664 |
|
665 def test_main(): |
|
666 import warnings |
|
667 with warnings.catch_warnings(): |
|
668 warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0", |
|
669 DeprecationWarning) |
|
670 test_support.run_unittest( |
|
671 urlopen_FileTests, |
|
672 urlopen_HttpTests, |
|
673 urlretrieve_FileTests, |
|
674 ProxyTests, |
|
675 QuotingTests, |
|
676 UnquotingTests, |
|
677 urlencode_Tests, |
|
678 Pathname_Tests, |
|
679 #FTPWrapperTests, |
|
680 ) |
|
681 |
|
682 |
|
683 |
|
684 if __name__ == '__main__': |
|
685 test_main() |