Skip to content

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

Description

@timyig

Describe the bug

probably related to: #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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions