Browse Source

Merge preliminary work for bug #434749.

suites/jessie-proposed-backports
Ben Finney 14 years ago
parent
commit
4565a39bc7
  1. 53
      test/test_urlutils.py
  2. 7
      urlutils.py

53
test/test_urlutils.py

@ -11,6 +11,59 @@
""" Unit test for urlutils module
"""
import httplib
import scaffold
from scaffold import TestCase
import reportbug_exceptions
import urlutils
class StubObject(object):
""" A stub object that allows any access. """
def __init__(self, *args, **kwargs):
pass
def __getattr__(self, name):
return StubObject()
def __call__(self, *args, **kwargs):
return StubObject()
class Test_open_url(TestCase):
""" Test cases for urlopen function """
def setUp(self):
""" Set up test fixtures """
self.stub_opener = StubObject()
def stub_build_opener(*args, **kwargs):
return self.stub_opener
self.urllib2_prev = urlutils.urllib2
self.stub_urllib2 = StubObject()
urlutils.urllib2 = self.stub_urllib2
self.stub_urllib2.Request = StubObject
self.stub_urllib2.build_opener = stub_build_opener
def tearDown(self):
""" Tear down test fixtures """
urlutils.urllib2 = self.urllib2_prev
def test_raises_no_network_when_http_exception(self):
""" Should raise NoNetwork when opener raises HTTPExeception """
class ArbitraryHTTPException(httplib.HTTPException):
pass
def stub_raise_bad_status_line(self, *args, **kwargs):
message = "Bad HTTP stuff happened!"
raise ArbitraryHTTPException(message)
self.stub_opener.open = stub_raise_bad_status_line
url = "foo"
expect_exception = reportbug_exceptions.NoNetwork
self.failUnlessRaises(
expect_exception,
urlutils.open_url, url)

7
urlutils.py

@ -146,6 +146,13 @@ def open_url(url, http_proxy=None):
except TypeError:
print >> sys.stderr, "http_proxy environment variable must be formatted as a valid URI"
raise NoNetwork
except httplib.HTTPException, exc:
exc_name = exc.__class__.__name__
message = (
"Failed to open %(url)r"
" (%(exc_name)s: %(exc)s)"
) % vars()
raise NoNetwork(message)
return page
def launch_browser(url):

Loading…
Cancel
Save