Exemplo n.º 1
0
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_no_auth(self):
        req = Request('http://example-noauth.com/')
        assert self.mw.process_request(req, self.spider) is None
        self.assertNotIn('Authorization', req.headers)

    def test_auth_domain(self):
        req = Request('http://example.com/')
        assert self.mw.process_request(req, self.spider) is None
        self.assertEqual(req.headers['Authorization'],
                         basic_auth_header('foo', 'bar'))

    def test_auth_subdomain(self):
        req = Request('http://foo.example.com/')
        assert self.mw.process_request(req, self.spider) is None
        self.assertEqual(req.headers['Authorization'],
                         basic_auth_header('foo', 'bar'))

    def test_auth_already_set(self):
        req = Request('http://example.com/',
                      headers=dict(Authorization='Digest 123'))
        assert self.mw.process_request(req, self.spider) is None
        self.assertEqual(req.headers['Authorization'], b'Digest 123')
Exemplo n.º 2
0
    def test_auth(self):
        with pytest.warns(
                ScrapyDeprecationWarning,
                match=
                "Using HttpAuthMiddleware without http_auth_domain is deprecated"
        ):
            mw = HttpAuthMiddleware()
            mw.spider_opened(self.spider)

        # initial request, sets the domain and sends the header
        req = Request('http://example.com/')
        assert mw.process_request(req, self.spider) is None
        self.assertEqual(req.headers['Authorization'],
                         basic_auth_header('foo', 'bar'))

        # subsequent request to the same domain, should send the header
        req = Request('http://example.com/')
        assert mw.process_request(req, self.spider) is None
        self.assertEqual(req.headers['Authorization'],
                         basic_auth_header('foo', 'bar'))

        # subsequent request to a different domain, shouldn't send the header
        req = Request('http://example-noauth.com/')
        assert mw.process_request(req, self.spider) is None
        self.assertNotIn('Authorization', req.headers)
 def test_auth_already_set(self):
     mw = HttpAuthMiddleware()
     mw.spider_opened(self.spider)
     req = Request('http://example.com/',
                   headers=dict(Authorization='Digest 123'))
     assert mw.process_request(req, self.spider) is None
     self.assertEqual(req.headers['Authorization'], b'Digest 123')
Exemplo n.º 4
0
 def test_auth_already_set(self):
     with pytest.warns(
             ScrapyDeprecationWarning,
             match=
             "Using HttpAuthMiddleware without http_auth_domain is deprecated"
     ):
         mw = HttpAuthMiddleware()
         mw.spider_opened(self.spider)
     req = Request('http://example.com/',
                   headers=dict(Authorization='Digest 123'))
     assert mw.process_request(req, self.spider) is None
     self.assertEqual(req.headers['Authorization'], b'Digest 123')
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")
Exemplo n.º 6
0
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'], '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'], 'Digest 123')