Back to index

python3.2  3.2.2
Public Member Functions
test.test_urllib2.OpenerDirectorTests Class Reference

List of all members.

Public Member Functions

def test_add_non_handler
def test_badly_named_methods
def test_handled
def test_handler_order
def test_raise

Detailed Description

Definition at line 453 of file test_urllib2.py.


Member Function Documentation

Definition at line 455 of file test_urllib2.py.

00455 
00456     def test_add_non_handler(self):
00457         class NonHandler(object):
00458             pass
00459         self.assertRaises(TypeError,
00460                           OpenerDirector().add_handler, NonHandler())

Definition at line 461 of file test_urllib2.py.

00461 
00462     def test_badly_named_methods(self):
00463         # test work-around for three methods that accidentally follow the
00464         # naming conventions for handler methods
00465         # (*_open() / *_request() / *_response())
00466 
00467         # These used to call the accidentally-named methods, causing a
00468         # TypeError in real code; here, returning self from these mock
00469         # methods would either cause no exception, or AttributeError.
00470 
00471         from urllib.error import URLError
00472 
00473         o = OpenerDirector()
00474         meth_spec = [
00475             [("do_open", "return self"), ("proxy_open", "return self")],
00476             [("redirect_request", "return self")],
00477             ]
00478         handlers = add_ordered_mock_handlers(o, meth_spec)
00479         o.add_handler(urllib.request.UnknownHandler())
00480         for scheme in "do", "proxy", "redirect":
00481             self.assertRaises(URLError, o.open, scheme+"://example.com/")

Here is the call graph for this function:

Definition at line 482 of file test_urllib2.py.

00482 
00483     def test_handled(self):
00484         # handler returning non-None means no more handlers will be called
00485         o = OpenerDirector()
00486         meth_spec = [
00487             ["http_open", "ftp_open", "http_error_302"],
00488             ["ftp_open"],
00489             [("http_open", "return self")],
00490             [("http_open", "return self")],
00491             ]
00492         handlers = add_ordered_mock_handlers(o, meth_spec)
00493 
00494         req = Request("http://example.com/")
00495         r = o.open(req)
00496         # Second .http_open() gets called, third doesn't, since second returned
00497         # non-None.  Handlers without .http_open() never get any methods called
00498         # on them.
00499         # In fact, second mock handler defining .http_open() returns self
00500         # (instead of response), which becomes the OpenerDirector's return
00501         # value.
00502         self.assertEqual(r, handlers[2])
00503         calls = [(handlers[0], "http_open"), (handlers[2], "http_open")]
00504         for expected, got in zip(calls, o.calls):
00505             handler, name, args, kwds = got
00506             self.assertEqual((handler, name), expected)
00507             self.assertEqual(args, (req,))

Here is the call graph for this function:

Definition at line 508 of file test_urllib2.py.

00508 
00509     def test_handler_order(self):
00510         o = OpenerDirector()
00511         handlers = []
00512         for meths, handler_order in [
00513             ([("http_open", "return self")], 500),
00514             (["http_open"], 0),
00515             ]:
00516             class MockHandlerSubclass(MockHandler): pass
00517             h = MockHandlerSubclass(meths)
00518             h.handler_order = handler_order
00519             handlers.append(h)
00520             o.add_handler(h)
00521 
00522         r = o.open("http://example.com/")
00523         # handlers called in reverse order, thanks to their sort order
00524         self.assertEqual(o.calls[0][0], handlers[1])
00525         self.assertEqual(o.calls[1][0], handlers[0])

Here is the call graph for this function:

Definition at line 526 of file test_urllib2.py.

00526 
00527     def test_raise(self):
00528         # raising URLError stops processing of request
00529         o = OpenerDirector()
00530         meth_spec = [
00531             [("http_open", "raise")],
00532             [("http_open", "return self")],
00533             ]
00534         handlers = add_ordered_mock_handlers(o, meth_spec)
00535 
00536         req = Request("http://example.com/")
00537         self.assertRaises(urllib.error.URLError, o.open, req)
00538         self.assertEqual(o.calls, [(handlers[0], "http_open", (req,), {})])

Here is the call graph for this function:


The documentation for this class was generated from the following file: