Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
timyig opened this issue Dec 4, 2024 · 0 comments
Labels

Comments

@timyig
Copy link

timyig commented Dec 4, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Waiting for: Product Owner
Development

No branches or pull requests

2 participants