mirror of https://github.com/scrapy/scrapy.git
verify if Request callback is callable
This commit is contained in:
parent
d847e65aae
commit
60727dedf6
|
|
@ -27,6 +27,10 @@ class Request(object_ref):
|
|||
assert isinstance(priority, int), "Request priority not an integer: %r" % priority
|
||||
self.priority = priority
|
||||
|
||||
if callback is not None and not callable(callback):
|
||||
raise TypeError('callback must be a function, got %s' % type(callback).__name__)
|
||||
if errback is not None and not callable(errback):
|
||||
raise TypeError('errback must be a function, got %s' % type(errback).__name__)
|
||||
assert callback or not errback, "Cannot use errback without a callback"
|
||||
self.callback = callback
|
||||
self.errback = errback
|
||||
|
|
|
|||
|
|
@ -235,6 +235,26 @@ class RequestTest(unittest.TestCase):
|
|||
self.assertRaises(AttributeError, setattr, r, 'url', 'http://example2.com')
|
||||
self.assertRaises(AttributeError, setattr, r, 'body', 'xxx')
|
||||
|
||||
def test_callback_is_callable(self):
|
||||
def a_function():
|
||||
pass
|
||||
r = self.request_class('http://example.com')
|
||||
self.assertIsNone(r.callback)
|
||||
r = self.request_class('http://example.com', a_function)
|
||||
self.assertIs(r.callback, a_function)
|
||||
with self.assertRaises(TypeError):
|
||||
self.request_class('http://example.com', 'a_function')
|
||||
|
||||
def test_errback_is_callable(self):
|
||||
def a_function():
|
||||
pass
|
||||
r = self.request_class('http://example.com')
|
||||
self.assertIsNone(r.errback)
|
||||
r = self.request_class('http://example.com', a_function, errback=a_function)
|
||||
self.assertIs(r.errback, a_function)
|
||||
with self.assertRaises(TypeError):
|
||||
self.request_class('http://example.com', a_function, errback='a_function')
|
||||
|
||||
|
||||
class FormRequestTest(RequestTest):
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue