def test_urlopen_success(self): response = urllib2.addinfourl(self.fp, self.headers, self.url) response.code = 200 response.msg = "OK" with mock.patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.return_value = response urlopen(self.url)
def test_urlopen_httperror_2(self): url = urllib2.Request(self.url) error = urllib2.HTTPError(self.url, 404, "Not Found", self.headers, self.fp) #print error.msg # Not Found with mock.patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.side_effect = error try: urlopen(url) except urllib2.HTTPError as err: #print err.msg # Not Found (http://www.a.com/) assert err.msg.endswith("({0})".format(self.url)) else: self.fail()
def test_urlopen_httperror_4(self): url = self.url fp = StringIO('{"status": "404 Not Found"}') error = urllib2.HTTPError(self.url, 404, "Not Found", self.headers, fp) #print error.msg # Not Found with mock.patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.side_effect = error try: urlopen(url) except urllib2.HTTPError as err: print err.msg # Not Found (http://www.a.com/) (Application couldn't match the route.) assert err.msg.endswith("({0}) (Application couldn't match the route.)".format(self.url)) else: self.fail()
def test_urlopen_httperror_3(self): url = self.url fp = StringIO("<!doctype html>\n<html>\n<title>404 Not Found</title>\n</html>\n") error = urllib2.HTTPError(self.url, 404, "Not Found", self.headers, fp) #print error.msg # Not Found with mock.patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.side_effect = error try: urlopen(url) except urllib2.HTTPError as err: #print err.msg # Not Found (http://www.a.com/) (Proxy couldn't match the route.) assert err.msg.endswith("({0}) (Proxy couldn't match the route.)".format(self.url)) else: self.fail()
def test_urlopen_urlerror_2(self): url = urllib2.Request(self.url) error = urllib2.URLError(socket.gaierror(-2, 'Name or service not known'),) #print error.args # (gaierror(-2, 'Name or service not known'),) #print error.reason # [Errno -2] Name or service not known with mock.patch('urllib2.urlopen') as mock_urlopen: mock_urlopen.side_effect = error try: urlopen(url) except urllib2.URLError as err: #print err.args # (gaierror(-2, 'Name or service not known'), 'http://www.a.com/') #print err.reason # [Errno -2] Name or service not known (http://www.a.com/) assert err.args[1] == self.url assert err.reason.endswith("({0})".format(self.url)) else: self.fail()