getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.08k stars 347 forks source link

_recorder.record captures header 'content-encoding: gzip' but does not return gzip content on replay #724

Open chriskauffman37 opened 1 week ago

chriskauffman37 commented 1 week ago

Describe the bug

headers has been added to the recording file which may include content-encoding. When a recording with content-encoding: gzip is replayed an error is received:

urllib3.exceptions.DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check'))

Additional context

No response

Version of responses

0.25.3

Steps to Reproduce

# Recording code

@_recorder.record(file_path="out.yaml")
def record():
    # call lookup API
- response:
    auto_calculate_content_length: false
    body: '[{"first_name":true,"last_name":true,"email":false,"phone_number":false,"custom_id":true,"user_id":"abc123","status":{"code":"ACTIVE","description":"The
      user is fully active with all features enabled. Further restrictions may apply
      for some features."},"employers":[{"id":"abc123","sso_required":false}]}]'
    content_type: text/plain
    headers:
      API-Version: '1'
      X-Dep-Next: T-0.8.4
      X-Download-Options: noopen
      X-Permitted-Cross-Domain-Policies: none
      X-Request-Id: f8903c30-4fb2-4b30-95b1-b20e0fdfb515
      X-Runtime: '0.129368'
      apigw-requestid: Z0iA3ibpIAMEaxg=
      cache-control: max-age=0, private, must-revalidate
      content-encoding: gzip
      etag: W/"2b965752b6adbb3fd7467a489ad314b0"
      referrer-policy: strict-origin-when-cross-origin
      vary: Accept-Encoding, Origin
      x-content-type-options: nosniff
      x-frame-options: SAMEORIGIN
      x-xss-protection: '0'
    method: GET
    status: 200
    url: https://api.com/v1/users/lookup?custom_id=060419806551&last_name=Murphy&first_name=Lucy
# Test code

@responses.activate
def test_lookup_api():
    responses._add_from_file(file_path="out.yaml")

    lookup = # call lookup API
    assert lookup["first_name"] == True

Expected Result

Pass

Actual Result

urllib3.exceptions.DecodeError: ('Received response with content-encoding: gzip, but failed to decode it.', error('Error -3 while decompressing data: incorrect header check'))

markstory commented 1 week ago

Storing gzip data in yaml files feels gross. Removing the content-encoding header and storing the response body as str/bytes seems more human-friendly to me.