Best Python code snippet using SeleniumBase
test_urllibnet.py
Source:test_urllibnet.py  
1#!/usr/bin/env python2import unittest3from test import test_support4import socket5import urllib6import sys7import os8import mimetools9def _open_with_retry(func, host, *args, **kwargs):10    # Connecting to remote hosts is flaky.  Make it more robust11    # by retrying the connection several times.12    for i in range(3):13        try:14            return func(host, *args, **kwargs)15        except IOError, last_exc:16            continue17        except:18            raise19    raise last_exc20class URLTimeoutTest(unittest.TestCase):21    TIMEOUT = 10.022    def setUp(self):23        socket.setdefaulttimeout(self.TIMEOUT)24    def tearDown(self):25        socket.setdefaulttimeout(None)26    def testURLread(self):27        f = _open_with_retry(urllib.urlopen, "http://www.python.org/")28        x = f.read()29class urlopenNetworkTests(unittest.TestCase):30    """Tests urllib.urlopen using the network.31    These tests are not exhaustive.  Assuming that testing using files does a32    good job overall of some of the basic interface features.  There are no33    tests exercising the optional 'data' and 'proxies' arguments.  No tests34    for transparent redirection have been written.35    setUp is not used for always constructing a connection to36    http://www.python.org/ since there a few tests that don't use that address37    and making a connection is expensive enough to warrant minimizing unneeded38    connections.39    """40    def urlopen(self, *args):41        return _open_with_retry(urllib.urlopen, *args)42    def test_basic(self):43        # Simple test expected to pass.44        open_url = self.urlopen("http://www.python.org/")45        for attr in ("read", "readline", "readlines", "fileno", "close",46                     "info", "geturl"):47            self.assert_(hasattr(open_url, attr), "object returned from "48                            "urlopen lacks the %s attribute" % attr)49        try:50            self.assert_(open_url.read(), "calling 'read' failed")51        finally:52            open_url.close()53    def test_readlines(self):54        # Test both readline and readlines.55        open_url = self.urlopen("http://www.python.org/")56        try:57            self.assert_(isinstance(open_url.readline(), basestring),58                         "readline did not return a string")59            self.assert_(isinstance(open_url.readlines(), list),60                         "readlines did not return a list")61        finally:62            open_url.close()63    def test_info(self):64        # Test 'info'.65        open_url = self.urlopen("http://www.python.org/")66        try:67            info_obj = open_url.info()68        finally:69            open_url.close()70            self.assert_(isinstance(info_obj, mimetools.Message),71                         "object returned by 'info' is not an instance of "72                         "mimetools.Message")73            self.assertEqual(info_obj.getsubtype(), "html")74    def test_geturl(self):75        # Make sure same URL as opened is returned by geturl.76        URL = "http://www.python.org/"77        open_url = self.urlopen(URL)78        try:79            gotten_url = open_url.geturl()80        finally:81            open_url.close()82        self.assertEqual(gotten_url, URL)83    def test_getcode(self):84        # test getcode() with the fancy opener to get 404 error codes85        URL = "http://www.python.org/XXXinvalidXXX"86        open_url = urllib.FancyURLopener().open(URL)87        try:88            code = open_url.getcode()89        finally:90            open_url.close()91        self.assertEqual(code, 404)92    def test_fileno(self):93        if (sys.platform in ('win32',) or94                not hasattr(os, 'fdopen')):95            # On Windows, socket handles are not file descriptors; this96            # test can't pass on Windows.97            return98        # Make sure fd returned by fileno is valid.99        open_url = self.urlopen("http://www.python.org/")100        fd = open_url.fileno()101        FILE = os.fdopen(fd)102        try:103            self.assert_(FILE.read(), "reading from file created using fd "104                                      "returned by fileno failed")105        finally:106            FILE.close()107    def test_bad_address(self):108        # Make sure proper exception is raised when connecting to a bogus109        # address.110        self.assertRaises(IOError,111                          # SF patch 809915:  In Sep 2003, VeriSign started112                          # highjacking invalid .com and .net addresses to113                          # boost traffic to their own site.  This test114                          # started failing then.  One hopes the .invalid115                          # domain will be spared to serve its defined116                          # purpose.117                          # urllib.urlopen, "http://www.sadflkjsasadf.com/")118                          urllib.urlopen, "http://sadflkjsasf.i.nvali.d/")119class urlretrieveNetworkTests(unittest.TestCase):120    """Tests urllib.urlretrieve using the network."""121    def urlretrieve(self, *args):122        return _open_with_retry(urllib.urlretrieve, *args)123    def test_basic(self):124        # Test basic functionality.125        file_location,info = self.urlretrieve("http://www.python.org/")126        self.assert_(os.path.exists(file_location), "file location returned by"127                        " urlretrieve is not a valid path")128        FILE = file(file_location)129        try:130            self.assert_(FILE.read(), "reading from the file location returned"131                         " by urlretrieve failed")132        finally:133            FILE.close()134            os.unlink(file_location)135    def test_specified_path(self):136        # Make sure that specifying the location of the file to write to works.137        file_location,info = self.urlretrieve("http://www.python.org/",138                                              test_support.TESTFN)139        self.assertEqual(file_location, test_support.TESTFN)140        self.assert_(os.path.exists(file_location))141        FILE = file(file_location)142        try:143            self.assert_(FILE.read(), "reading from temporary file failed")144        finally:145            FILE.close()146            os.unlink(file_location)147    def test_header(self):148        # Make sure header returned as 2nd value from urlretrieve is good.149        file_location, header = self.urlretrieve("http://www.python.org/")150        os.unlink(file_location)151        self.assert_(isinstance(header, mimetools.Message),152                     "header is not an instance of mimetools.Message")153def test_main():154    test_support.requires('network')155    from warnings import filterwarnings, catch_warnings156    with catch_warnings():157        filterwarnings('ignore', '.*urllib\.urlopen.*Python 3.0',158                        DeprecationWarning)159        test_support.run_unittest(URLTimeoutTest,160                                  urlopenNetworkTests,161                                  urlretrieveNetworkTests)162if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
