responses icon indicating copy to clipboard operation
responses copied to clipboard

Inconsistent Behavior of responses._recorder - content_type and Content-Type Header Conflict

Open timyig opened this issue 1 year ago • 1 comments

Describe the bug

probably related to: https://github.com/getsentry/responses/issues/675

Summary: When using the _recorder functionality from the responses library to generate YAML files for testing, the recorded output includes both content_type and Content-Type in headers. This results in a RuntimeError during playback due to conflicting definitions, making it difficult to use recorded responses directly in tests without manual intervention.

Additional context

This is an example recorded response

# ping_response.yml
responses:
- response:
    auto_calculate_content_length: false
    body: "{\"message\": \"Test response\"}"
    content_type: text/plain
    headers:
      content-length: '28'
      content-type: application/json; charset=UTF-8
    method: GET
    status: 200
    url: https://dev.opensearch:9200/
poetry show | grep responses
responses                  0.25.3

Version of responses

0.25.3

Steps to Reproduce

Recording

@_recorder.record(file_path=OUTPUT_DIR / "get_ping_opensearch_success.yml")
def get_ping_opensearch():
    endpoint = BASE_URL + OPENSEARCH_PORT
    headers = {
        "Content-Type": "application/json",
        "osd-xsrf": "osd-fetch",
        "Accept": "*/*",
        "securitytenant": "global",
    }
    response = requests.request(headers=headers, auth=auth, verify=False, method="get", url=endpoint)
    print(f"Status Code: {response.status_code}")
    pprint(f"Response: {response.headers}")

Output

Status Code: 200
("Response: {'content-type': 'application/json; charset=UTF-8', "
 "'content-length': '568'}")

add from file

@responses.activate
    def test_ping(self):
    responses._add_from_file(file_path="ping_response.yml")
    pass

Error

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

Expected Result

Not raise RuntimeError

Actual Result

RuntimeError: You cannot define both content_typeandheaders[Content-Type]. Using the content_type kwarg is recommended.

timyig avatar Dec 04 '24 14:12 timyig

We actually see this slightly differently, the recorded content_type is set to text_plain, and the header is not recorded.

dwt avatar Jul 21 '25 07:07 dwt