mirror of https://github.com/scrapy/scrapy.git
HttpAuthMiddleware: implement meta support
This commit is contained in:
parent
8218efaf46
commit
3e70c7a182
|
|
@ -26,6 +26,14 @@ class HttpAuthMiddleware:
|
|||
self.auth = basic_auth_header(usr, pwd)
|
||||
|
||||
def process_request(self, request, spider):
|
||||
auth = getattr(self, 'auth', None)
|
||||
if auth and b'Authorization' not in request.headers:
|
||||
if b'Authorization' in request.headers:
|
||||
return
|
||||
|
||||
usr = request.meta.get('http_user', '')
|
||||
pwd = request.meta.get('http_pass', '')
|
||||
if usr or pwd:
|
||||
auth = basic_auth_header(usr, pwd)
|
||||
else:
|
||||
auth = getattr(self, 'auth', None)
|
||||
if auth:
|
||||
request.headers[b'Authorization'] = auth
|
||||
|
|
|
|||
|
|
@ -30,3 +30,23 @@ class HttpAuthMiddlewareTest(unittest.TestCase):
|
|||
headers=dict(Authorization='Digest 123'))
|
||||
assert self.mw.process_request(req, self.spider) is None
|
||||
self.assertEqual(req.headers['Authorization'], b'Digest 123')
|
||||
|
||||
def test_auth_already_set_with_meta(self):
|
||||
meta = {'http_user': 'bar', 'http_pass': 'foo'}
|
||||
req = Request('http://scrapytest.org/',
|
||||
headers=dict(Authorization='Digest 123'),
|
||||
meta=meta)
|
||||
assert self.mw.process_request(req, self.spider) is None
|
||||
self.assertEqual(req.headers['Authorization'], b'Digest 123')
|
||||
|
||||
def test_auth_meta(self):
|
||||
meta = {'http_user': 'bar', 'http_pass': 'foo'}
|
||||
req = Request('http://scrapytest.org/', meta=meta)
|
||||
assert self.mw.process_request(req, Spider('bar')) is None
|
||||
self.assertEqual(req.headers['Authorization'], b'Basic YmFyOmZvbw==')
|
||||
|
||||
def test_auth_meta_override(self):
|
||||
meta = {'http_user': 'bar', 'http_pass': 'foo'}
|
||||
req = Request('http://scrapytest.org/', meta=meta)
|
||||
assert self.mw.process_request(req, self.spider) is None
|
||||
self.assertEqual(req.headers['Authorization'], b'Basic YmFyOmZvbw==')
|
||||
|
|
|
|||
Loading…
Reference in New Issue