test_urllib_toplevel.py - "Regresssion tests for urllib"...

This preview shows page 1 - 4 out of 24 pages.

The preview shows page 2 - 4 out of 24 pages.
"""Regresssion tests for urllib"""from __future__ import absolute_import, division, unicode_literalsimport ioimport osimport sysimport tempfilefrom nturl2path import url2pathname, pathname2urlfrom base64 import b64encodeimport collectionsPY2 = sys.version_info[0] == 2from future.builtins import bytes, chr, hex, open, range, str, intfrom future.standard_library import install_aliasesinstall_aliases()from urllib import parse as urllib_parsefrom urllib import request as urllib_requestfrom urllib import error as urllib_errorfrom http import client as http_clienttry:from future.moves.test import supportexcept ImportError:from future.backports.test import supportif PY2:from future.backports.email import message as email_messageelse:from email import message as email_message# from future.backports.email import message as email_messagefrom future.tests.base import unittest, skip26, expectedFailurePY26,expectedFailurePY2def hexescape(char):"""Escape char as RFC 2396 specifies"""hex_repr = hex(ord(char))[2:].upper()if len(hex_repr) == 1:hex_repr = "0%s" % hex_reprreturn "%" + hex_repr# Shortcut for testing FancyURLopener_urlopener = Nonedef urlopen(url, data=None, proxies=None):"""urlopen(url [, data]) -> open file-like object"""global _urlopenerif proxies is not None:opener = urllib_request.FancyURLopener(proxies=proxies)elif not _urlopener:with support.check_warnings(('FancyURLopener style of invoking requests is deprecated.',DeprecationWarning)):opener = urllib_request.FancyURLopener()_urlopener = openerelse:opener = _urlopenerif data is None:return opener.open(url)
else:return opener.open(url, data)class FakeHTTPMixin(object):def fakehttp(self, fakedata):class FakeSocket(io.BytesIO):io_refs = 1def sendall(self, data):FakeHTTPConnection.buf = datadef makefile(self, *args, **kwds):self.io_refs += 1return selfdef read(self, amt=None):if self.closed:return b""return io.BytesIO.read(self, amt)def readline(self, length=None):if self.closed:return b""return io.BytesIO.readline(self, length)def close(self):self.io_refs -= 1if self.io_refs == 0:io.BytesIO.close(self)class FakeHTTPConnection(http_client.HTTPConnection):# buffer to store data for verification in urlopen tests.buf = Nonedef connect(self):self.sock = FakeSocket(fakedata)self._connection_class = http_client.HTTPConnectionhttp_client.HTTPConnection = FakeHTTPConnectiondef unfakehttp(self):http_client.HTTPConnection = self._connection_classclass urlopen_FileTests(unittest.TestCase):"""Test urlopen() opening a temporary file.Try to test as much functionality as possible so as to cut down on relianceon connecting to the Net for testing."""def setUp(self):# Create a temp file to use for testingself.text = bytes("test_urllib: %s\n" % self.__class__.__name__,"ascii")f = open(support.TESTFN, 'wb')
try:f.write(self.text)finally:f.close()self.pathname = support.TESTFNself.returned_obj = urlopen("file:%s" % self.pathname)def tearDown(self):"""Shut down the open object"""self.returned_obj.close()os.remove(support.TESTFN)def test_interface(self):# Make sure object returned by urlopen() has the specified methodsfor attr in ("read", "readline", "readlines", "fileno",

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 24 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Summer
Professor
N/A
Tags
Character encoding, ASCII, URLError

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture