Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
4 changes: 2 additions & 2 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
33 changes: 33 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="<p>ok</p>",
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():
Expand Down