1
0
mirror of https://github.com/scrapy/scrapy.git synced 2025-02-23 10:24:01 +00:00
scrapy/tests/test_downloadermiddleware_httpauth.py
2015-09-01 04:00:26 +05:00

33 lines
968 B
Python

import unittest
from scrapy.http import Request
from scrapy.downloadermiddlewares.httpauth import HttpAuthMiddleware
from scrapy.spiders import Spider
class TestSpider(Spider):
http_user = 'foo'
http_pass = 'bar'
class HttpAuthMiddlewareTest(unittest.TestCase):
def setUp(self):
self.mw = HttpAuthMiddleware()
self.spider = TestSpider('foo')
self.mw.spider_opened(self.spider)
def tearDown(self):
del self.mw
def test_auth(self):
req = Request('http://scrapytest.org/')
assert self.mw.process_request(req, self.spider) is None
self.assertEquals(req.headers['Authorization'], b'Basic Zm9vOmJhcg==')
def test_auth_already_set(self):
req = Request('http://scrapytest.org/',
headers=dict(Authorization='Digest 123'))
assert self.mw.process_request(req, self.spider) is None
self.assertEquals(req.headers['Authorization'], b'Digest 123')