diff --git a/scrapy/trunk/scrapy/contrib/downloadermiddleware/useragent.py b/scrapy/trunk/scrapy/contrib/downloadermiddleware/useragent.py index ddf51cb89..29206b2c8 100644 --- a/scrapy/trunk/scrapy/contrib/downloadermiddleware/useragent.py +++ b/scrapy/trunk/scrapy/contrib/downloadermiddleware/useragent.py @@ -9,6 +9,6 @@ class UserAgentMiddleware(object): default_useragent = settings.get('USER_AGENT') def process_request(self, request, spider): - ua = getattr(spider, 'user_agent', self.default_useragent) + ua = getattr(spider, 'user_agent', None) or self.default_useragent if ua: request.headers.setdefault('User-Agent', ua) diff --git a/scrapy/trunk/scrapy/tests/test_downloadermiddleware_useragent.py b/scrapy/trunk/scrapy/tests/test_downloadermiddleware_useragent.py index 091567ffd..4ebb36d2b 100644 --- a/scrapy/trunk/scrapy/tests/test_downloadermiddleware_useragent.py +++ b/scrapy/trunk/scrapy/tests/test_downloadermiddleware_useragent.py @@ -23,7 +23,14 @@ class CookiesMiddlewareTest(TestCase): assert self.mw.process_request(req, self.spider) is None self.assertEquals(req.headers['User-Agent'], 'default_useragent') + # None or not present user_agent attribute is the same + self.spider.user_agent = None + req = Request('http://scrapytest.org/') + assert self.mw.process_request(req, self.spider) is None + self.assertEquals(req.headers['User-Agent'], 'default_useragent') + def test_spider_agent(self): + self.mw.default_useragent = 'default_useragent' self.spider.user_agent = 'spider_useragent' req = Request('http://scrapytest.org/') assert self.mw.process_request(req, self.spider) is None