Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Cookies test (#6141)

Georgiy Zatserklianyid4953562649e
diff --git a/tests/mockserver/http_resources.py b/tests/mockserver/http_resources.py
index 79ae43c35..eda524c21 100644
--- a/tests/mockserver/http_resources.py
+++ b/tests/mockserver/http_resources.py
@@ -450,11 +450,13 @@ class UriResource(BaseResource):
class ResponseHeadersResource(BaseResource):
- """Return a response with headers set from the JSON request body"""
+ """Return a response with headers set from the JSON request body, and the
+ status code set from the *n* URL parameter, 200 by default."""
def render(self, request: Request) -> bytes:
assert request.content
body = json.loads(request.content.read().decode())
+ request.setResponseCode(getarg(request, b"n", 200, type_=int))
for header_name, header_value in body.items():
# surrogateescape lets tests send bytes that are not valid UTF-8
request.responseHeaders.setRawHeaders(
diff --git a/tests/utils/bases/download_handlers_http.py b/tests/utils/bases/download_handlers_http.py
index f146dfd71..afff60aea 100644
--- a/tests/utils/bases/download_handlers_http.py
+++ b/tests/utils/bases/download_handlers_http.py
@@ -488,6 +488,20 @@ class TestHttpBase(ABC):
)
assert response.headers.getlist(header_name) == [header_value.encode()]
+ @coroutine_test
+ async def test_download_has_correct_response_status_and_headers(
+ self, mockserver: MockServer
+ ) -> None:
+ request = Request(
+ mockserver.url("/response-headers?n=302", is_secure=self.is_secure),
+ headers={"content-type": "application/json"},
+ body=json.dumps({"Set-Cookie": "status=302"}),
+ )
+ async with self.get_dh() as download_handler:
+ response = await download_handler.download_request(request)
+ assert response.status == 302
+ assert response.headers.getlist("Set-Cookie") == [b"status=302"]
+
@coroutine_test
async def test_download_no_extra_response_headers(
self, mockserver: MockServer