def test_http_error(self): # XXX http_error_default # http errors are a special case o = OpenerDirector() meth_spec = [ [("http_open", "error 302")], [("http_error_400", "raise"), "http_open"], [("http_error_302", "return response"), "http_error_303", "http_error"], [("http_error_302")], ] handlers = add_ordered_mock_handlers(o, meth_spec) class Unknown: pass req = Request("http://example.com/") r = o.open(req) assert len(o.calls) == 2 calls = [(handlers[0], "http_open", (req,)), (handlers[2], "http_error_302", (req, Unknown, 302, "", {}))] for i in range(len(calls)): handler, method_name, args, kwds = o.calls[i] self.assert_((handler, method_name) == calls[i][:2]) # check handler methods were called with expected arguments expected_args = calls[i][2] for j in range(len(args)): if expected_args[j] is not Unknown: self.assert_(args[j] == expected_args[j])
def test_processors(self): # *_request / *_response methods get called appropriately o = OpenerDirector() meth_spec = [ [("http_request", "return request"), ("http_response", "return response")], [("http_request", "return request"), ("http_response", "return response")], ] handlers = add_ordered_mock_handlers(o, meth_spec) req = Request("http://example.com/") r = o.open(req) # processor methods are called on *all* handlers that define them, # not just the first handler calls = [(handlers[0], "http_request"), (handlers[1], "http_request"), (handlers[0], "http_response"), (handlers[1], "http_response")] for i in range(len(o.calls)): handler, name, args, kwds = o.calls[i] if i < 2: # *_request self.assert_((handler, name) == calls[i]) self.assert_(len(args) == 1) self.assert_(isinstance(args[0], Request)) else: # *_response self.assert_((handler, name) == calls[i]) self.assert_(len(args) == 2) self.assert_(isinstance(args[0], Request)) # response from opener.open is None, because there's no # handler that defines http_open to handle it self.assert_(args[1] is None or isinstance(args[1], MockResponse))
def test_http_error(self): # XXX http_error_default # http errors are a special case o = OpenerDirector() meth_spec = [ [("http_open", "error 302")], [("http_error_400", "raise"), "http_open"], [("http_error_302", "return response"), "http_error_303", "http_error"], [("http_error_302")], ] handlers = add_ordered_mock_handlers(o, meth_spec) class Unknown: pass req = Request("http://example.com/") r = o.open(req) assert len(o.calls) == 2 calls = [(handlers[0], "http_open", (req, )), (handlers[2], "http_error_302", (req, Unknown, 302, "", {}))] for i in range(len(calls)): handler, method_name, args, kwds = o.calls[i] self.assert_((handler, method_name) == calls[i][:2]) # check handler methods were called with expected arguments expected_args = calls[i][2] for j in range(len(args)): if expected_args[j] is not Unknown: self.assert_(args[j] == expected_args[j])
def test_handler_order(self): o = OpenerDirector() handlers = [] for meths, handler_order in [ ([("http_open", "return self")], 500), (["http_open"], 0), ]: class MockHandlerSubclass(MockHandler): pass h = MockHandlerSubclass(meths) h.handler_order = handler_order handlers.append(h) o.add_handler(h) r = o.open("http://example.com/") # handlers called in reverse order, thanks to their sort order self.assert_(o.calls[0][0] == handlers[1]) self.assert_(o.calls[1][0] == handlers[0])
def test_raise(self): # raising URLError stops processing of request o = OpenerDirector() meth_spec = [ [("http_open", "raise")], [("http_open", "return self")], ] handlers = add_ordered_mock_handlers(o, meth_spec) req = Request("http://example.com/") self.assertRaises(urllib2.URLError, o.open, req) self.assert_(o.calls == [(handlers[0], "http_open", (req, ), {})])
def test_handled(self): # handler returning non-None means no more handlers will be called o = OpenerDirector() meth_spec = [ ["http_open", "ftp_open", "http_error_302"], ["ftp_open"], [("http_open", "return self")], [("http_open", "return self")], ] handlers = add_ordered_mock_handlers(o, meth_spec) req = Request("http://example.com/") r = o.open(req) # Second http_open gets called, third doesn't, since second returned # non-None. Handlers without http_open never get any methods called # on them. # In fact, second mock handler returns self (instead of response), # which becomes the OpenerDirector's return value. self.assert_(r == handlers[2]) calls = [(handlers[0], "http_open"), (handlers[2], "http_open")] for i in range(len(o.calls)): handler, name, args, kwds = o.calls[i] self.assert_((handler, name) == calls[i]) self.assert_(args == (req,))
def test_handled(self): # handler returning non-None means no more handlers will be called o = OpenerDirector() meth_spec = [ ["http_open", "ftp_open", "http_error_302"], ["ftp_open"], [("http_open", "return self")], [("http_open", "return self")], ] handlers = add_ordered_mock_handlers(o, meth_spec) req = Request("http://example.com/") r = o.open(req) # Second http_open gets called, third doesn't, since second returned # non-None. Handlers without http_open never get any methods called # on them. # In fact, second mock handler returns self (instead of response), # which becomes the OpenerDirector's return value. self.assert_(r == handlers[2]) calls = [(handlers[0], "http_open"), (handlers[2], "http_open")] for i in range(len(o.calls)): handler, name, args, kwds = o.calls[i] self.assert_((handler, name) == calls[i]) self.assert_(args == (req, ))