diff --git a/CHANGES b/CHANGES index fcab1798..4cb85d92 100644 --- a/CHANGES +++ b/CHANGES @@ -11,6 +11,9 @@ Unreleased include the ``params`` and ``req_kwargs`` attributes added by ``responses``. * Fixed non-strict `json_params_matcher` raising `TypeError` when an actual nested object is compared with an expected scalar value. +* Fixed ``responses.add()`` raising ``AttributeError`` when ``content_type`` is + combined with ``headers`` given as a list of ``(name, value)`` tuples (for + example, several ``Set-Cookie`` headers). 0.26.3 ------ diff --git a/responses/__init__.py b/responses/__init__.py index 5b1546a3..3d9c3901 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -859,8 +859,8 @@ def add( and "headers" in kwargs and kwargs["headers"] is not None ): - header_keys = [header.lower() for header in kwargs["headers"]] - if "content-type" in header_keys: + # headers may be a mapping or a list of (name, value) tuples + if "Content-Type" in HTTPHeaderDict(kwargs["headers"]): raise RuntimeError( "You cannot define both `content_type` and `headers[Content-Type]`." " Using the `content_type` kwarg is recommended." diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index bc8f1e04..d8cef974 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -1557,6 +1557,39 @@ def run(): assert_reset() +def test_content_type_with_list_of_header_tuples(): + @responses.activate + def run(): + url = "http://example.com/" + responses.add( + responses.GET, + url, + body="

ok

", + content_type="text/html", + headers=[("Set-Cookie", "a=1"), ("Set-Cookie", "b=2")], + ) + resp = requests.get(url) + assert resp.headers["Content-Type"] == "text/html" + assert resp.cookies["a"] == "1" + assert resp.cookies["b"] == "2" + + with pytest.raises(RuntimeError) as excinfo: + responses.add( + responses.GET, + url, + body="test", + content_type="text/html", + headers=[("content-type", "text/html; charset=utf-8")], + ) + assert ( + "You cannot define both `content_type` and `headers[Content-Type]`" + in str(excinfo.value) + ) + + run() + assert_reset() + + def test_auto_calculate_content_length_string_body(): @responses.activate def run():