Skip to content

Commit

Permalink
response.request → request (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio authored Nov 27, 2024
1 parent e0cead1 commit a731b96
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
2 changes: 1 addition & 1 deletion sh_scrapy/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def process_response(self, request, response, spider):
rs=len(response.body),
duration=request.meta.get('download_latency', 0) * 1000,
parent=request.meta.setdefault(HS_PARENT_ID_KEY),
fp=self._fingerprint(response.request),
fp=self._fingerprint(request),
)
# Generate and set request id.
request_id = next(self.request_id_sequence)
Expand Down
10 changes: 1 addition & 9 deletions tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ def test_hs_middlewares(hs_downloader_middleware, hs_spider_middleware):
assert len(hs_spider_middleware._seen_requests) == 0
assert len(hs_downloader_middleware._seen_requests) == 0

response_0.request = request_0
hs_downloader_middleware.process_response(request_0, response_0, spider)

assert request_0.meta[HS_REQUEST_ID_KEY] == 0
assert request_0.meta[HS_PARENT_ID_KEY] is None
assert hs_spider_middleware._seen_requests[request_0] == 0

response_0.request = request_0
request_1 = Request(url)
request_2 = Request(url)
item1 = {}
Expand All @@ -71,14 +71,12 @@ def test_hs_middlewares(hs_downloader_middleware, hs_spider_middleware):

response_1 = Response(url)
hs_downloader_middleware.process_request(request_1, spider)
response_1.request = request_1
hs_downloader_middleware.process_response(request_1, response_1, spider)
assert request_1.meta[HS_REQUEST_ID_KEY] == 1
assert request_1.meta[HS_PARENT_ID_KEY] == 0

response_2 = Response(url)
hs_downloader_middleware.process_request(request_2, spider)
response_2.request = request_2
hs_downloader_middleware.process_response(request_2, response_2, spider)
assert request_2.meta[HS_REQUEST_ID_KEY] == 2
assert request_2.meta[HS_PARENT_ID_KEY] == 0
Expand All @@ -105,14 +103,12 @@ def __init__(self, url: str, request: Optional[Request] = None):
response_1 = DummyResponse(url, request)
response_2 = Response(url)
hs_downloader_middleware.process_request(request, spider)
response_1.request = request
hs_downloader_middleware.process_response(request, response_1, spider)

with open(hs_downloader_middleware.pipe_writer.path, 'r') as tmp_file:
assert tmp_file.readline() == ""
assert request.meta == {}

response_2.request = request
hs_downloader_middleware.process_response(request, response_2, spider)
with open(hs_downloader_middleware.pipe_writer.path, 'r') as tmp_file:
assert tmp_file.readline().startswith('REQ')
Expand Down Expand Up @@ -144,7 +140,6 @@ def __init__(self, url: str, request: Optional[Request] = None):
assert len(hs_spider_middleware._seen_requests) == 0
assert len(hs_downloader_middleware._seen_requests) == 0

response_0.request = request_0
hs_downloader_middleware.process_response(request_0, response_0, spider)

assert request_0.meta[HS_REQUEST_ID_KEY] == 0
Expand All @@ -161,7 +156,6 @@ def __init__(self, url: str, request: Optional[Request] = None):
assert HS_REQUEST_ID_KEY not in request_1.meta
assert request_1.meta[HS_PARENT_ID_KEY] == 0

response_1.request = request_1
hs_downloader_middleware.process_response(request_1, response_1, spider)

assert request_1.meta[HS_REQUEST_ID_KEY] == 1
Expand All @@ -171,13 +165,11 @@ def __init__(self, url: str, request: Optional[Request] = None):
response_2_1 = DummyResponse(url, request_2)
response_2_2 = Response(url)

response_2_1.request = request_2
hs_downloader_middleware.process_response(request_2, response_2_1, spider)

assert request_2.meta[HS_REQUEST_ID_KEY] == 1
assert request_2.meta[HS_PARENT_ID_KEY] == 0

response_2_2.request = request_2
hs_downloader_middleware.process_response(request_2, response_2_2, spider)

assert request_2.meta[HS_REQUEST_ID_KEY] == 2
Expand Down

0 comments on commit a731b96

Please sign in to comment.