-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from tharropoulos/header-middleware
Add header inspection tools
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import logging | ||
|
||
from scrapy import signals | ||
|
||
|
||
class HeaderInspectionMiddleware: | ||
""" | ||
Middleware to inspect headers of outgoing requests and incoming responses | ||
""" | ||
|
||
def __init__(self): | ||
self.spider = None | ||
|
||
@classmethod | ||
def from_crawler(cls, crawler): | ||
middleware = cls() | ||
crawler.signals.connect(middleware.spider_opened, signal=signals.spider_opened) | ||
return middleware | ||
|
||
def spider_opened(self, spider): | ||
self.spider = spider | ||
|
||
def process_request(self, request, spider): | ||
""" | ||
This method is called for each request that goes through the download middleware. | ||
""" | ||
logging.debug("\nOutgoing request to: %s", request.url) | ||
logging.debug("\nHeaders: %s", request.headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import pdb | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from scrapy.http import Request | ||
from scrapy.spidermiddlewares.httperror import HttpError | ||
|
||
|
||
@pytest.fixture | ||
def config(): | ||
return MagicMock( | ||
index_name="test_index", | ||
start_urls=[{"url": "http://example.com"}], | ||
allowed_domains=["example.com"], | ||
stop_urls=[], | ||
js_render=False, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def env_vars(monkeypatch): | ||
monkeypatch.setenv("DOCSEARCH_BASICAUTH_USERNAME", "testuser") | ||
monkeypatch.setenv("DOCSEARCH_BASICAUTH_PASSWORD", "testpass") | ||
monkeypatch.setenv("DOCSEARCH_AUTH_DOMAIN", "http://example.com") | ||
|
||
|
||
def test_spider_auth_attributes(config, env_vars): | ||
"""Test that DocumentationSpider correctly sets up Basic Auth attributes""" | ||
from scraper.src.documentation_spider import DocumentationSpider | ||
|
||
spider = DocumentationSpider(config=config, typesense_helper=None, strategy=None) | ||
|
||
assert spider.http_user == "testuser" | ||
assert spider.http_pass == "testpass" | ||
assert spider.http_auth_domain == "http://example.com" |