responses icon indicating copy to clipboard operation
responses copied to clipboard

RuntimeError: You cannot define both `content_type` and `headers[Content-Type]`.

Open nschloe opened this issue 9 months ago • 1 comments

Describe the bug

In my pytests, I record responses with the fixture

import hashlib

import pytest
import responses
from responses._recorder import recorder


@pytest.fixture
def mock_requests(request):
    fname = hashlib.sha256(request.node.nodeid.encode()).hexdigest()[:16]
    yml_path = request.path.parent / "responses" / f"{fname}.yml"

    if request.config.getoption("--update-responses"):
        yml_path.parent.mkdir(exist_ok=True)
        if yml_path.exists():
            yml_path.unlink()
        with recorder as r:
            yield r
            # The register might in fact be empty, but never mind
            recorder.dump_to_file(
                file_path=yml_path,
                registered=r.get_registry().registered,
            )
    else:
        assert yml_path.exists()
        with responses.RequestsMock() as r:
            r._add_from_file(file_path=yml_path)  # noqa: SLF001
            yield r

This usually works fine, but I sometimes get responses like

responses:
  - response:
      auto_calculate_content_length: false
      body:
        '{"status":"ok","message-type":"work","message-version":"1.0.0","message":{"indexed":{"date-parts":[[2025,4,5]],"date-time":"2025-04-05T15:23:57Z","timestamp":1743866637814},"edition-number":"1","reference-count":0,"publisher":"Wiley","isbn-type":[{"value":"9780471967583","type":"print"},{"value":"9780470868270","type":"electronic"}],"license":[{"start":{"date-parts":[[2003,6,6]],"date-time":"2003-06-06T00:00:00Z","timestamp":1054857600000},"content-version":"tdm","delay-in-days":0,"URL":"http:\/\/doi.wiley.com\/10.1002\/tdm_license_1.1"}],"content-domain":{"domain":[],"crossmark-restriction":false},"short-container-title":[],"published-print":{"date-parts":[[2003,6,6]]},"DOI":"10.1002\/0470868279","type":"monograph","created":{"date-parts":[[2005,1,28]],"date-time":"2005-01-28T19:00:13Z","timestamp":1106938813000},"source":"Crossref","is-referenced-by-count":547,"title":["Numerical
        Methods for Ordinary Differential Equations"],"prefix":"10.1002","author":[{"given":"J.C.","family":"Butcher","sequence":"first","affiliation":[]}],"member":"311","published-online":{"date-parts":[[2005,1,28]]},"container-title":[],"original-title":[],"language":"en","link":[{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/pdf\/10.1002\/0470868279","content-type":"application\/pdf","content-version":"vor","intended-application":"text-mining"},{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/pdf\/10.1002\/0470868279","content-type":"unspecified","content-version":"vor","intended-application":"similarity-checking"}],"deposited":{"date-parts":[[2023,8,20]],"date-time":"2023-08-20T18:49:32Z","timestamp":1692557372000},"score":1,"resource":{"primary":{"URL":"https:\/\/onlinelibrary.wiley.com\/doi\/book\/10.1002\/0470868279"}},"subtitle":[],"short-title":[],"issued":{"date-parts":[[2003,6,6]]},"ISBN":["9780471967583","9780470868270"],"references-count":0,"alternative-id":["10.1002\/0470868279"],"URL":"https:\/\/doi.org\/10.1002\/0470868279","relation":{},"subject":[],"published":{"date-parts":[[2003,6,6]]}}}'
      content_type: text/plain
      headers:
        access-control-allow-headers:
          X-Requested-With, Accept, Accept-Encoding, Accept-Charset,
          Accept-Language, Accept-Ranges, Cache-Control
        access-control-allow-origin: "*"
        access-control-expose-headers: Link
        connection: close
        content-encoding: gzip
        content-length: "871"
        content-type: application/json
        date: Tue, 08 Apr 2025 12:35:37 GMT
        permissions-policy: interest-cohort=()
        server: Jetty(9.4.40.v20210413)
        set-cookie:
          AWSALB=q+h9ABpvNWjFxcvQm3RXWezoK9cDzdpQ744+UzK8XrW4K/QpNE49u2geK33JiVSCotsja9oBo0d4Eq9EWYfPAlFVfltotJjVtr9Ws/ytgxvmlycVk1/rSWOT+aiz;
          Expires=Tue, 15 Apr 2025 12:35:37 GMT; Path=/, AWSALBCORS=q+h9ABpvNWjFxcvQm3RXWezoK9cDzdpQ744+UzK8XrW4K/QpNE49u2geK33JiVSCotsja9oBo0d4Eq9EWYfPAlFVfltotJjVtr9Ws/ytgxvmlycVk1/rSWOT+aiz;
          Expires=Tue, 15 Apr 2025 12:35:37 GMT; Path=/; SameSite=None
        vary: Accept-Encoding
        x-api-pool: polite
        x-rate-limit-interval: 1s
        x-rate-limit-limit: "50"
        x-ratelimit-interval: 1s
        x-ratelimit-limit: "50"
      method: GET
      status: 200
      url: https://api.crossref.org/works/10.1002%2F0470868279

When replaying, this leads to the error

E               RuntimeError: You cannot define both `content_type` and `headers[Content-Type]`. Using the `content_type` kwarg is recommended.

There is no content_type specification in my own code.

Additional context

No response

Version of responses

0.25.7

Steps to Reproduce

See above.

Expected Result

See above.

Actual Result

See above.

nschloe avatar Apr 08 '25 12:04 nschloe

related to #675

beliaev-maksim avatar Apr 08 '25 14:04 beliaev-maksim