httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
32.74k stars 3.68k forks source link

Failing tests with responses ≥ 0.22.0 #1461

Closed alexshpilkin closed 1 year ago

alexshpilkin commented 1 year ago

Checklist


Minimal reproduction code and steps

  1. git clone https://github.com/httpie/httpie; cd httpie
  2. pip install 'responses>=0.22.0' .[test]
  3. pytest

Current result

A multitude of failures in tests/test_encoding.py, tests/test_json.py, etc. in the vein of https://hydra.nixos.org/build/202035507: KeyError: 0 on httpie/models.py line 82.

Expected result

A passing test suite.


Additional information, screenshots, or code examples

I wrote some of this up in https://github.com/NixOS/nixpkgs/pull/205270#issuecomment-1361147904, but the problem is not NixOS-specific. The short version is that before https://github.com/getsentry/responses/pull/585, the reference to httpie.models.HTTPResponse()._orig.raw._original_response.version in the implementation of httpie.models.HTTPResponse.headers found the then-extant responses.OriginalResponseShim object, which does not have a version attribute, and therefore successfully defaulted to 11, whereas now that that class has been removed it finds a urllib3.HTTPResponse object instead, which defaults to version=0, and it’s not prepared to handle that.

Given the amount of groveling into internal data structures that goes on here (I don’t think requests even documents Request.raw as being a urllib3.HTTPResponse object), I’m not sure if this is a bug in the httpie test suite or a regression in responses, so I’m filing it here for you to decide.

For reference, the following change makes the tests pass for me:

diff --git a/httpie/models.py b/httpie/models.py
index d97b55e..a3ec6e7 100644
--- a/httpie/models.py
+++ b/httpie/models.py
@@ -77,6 +77,8 @@ class HTTPResponse(HTTPMessage):
             else:
                 raw_version = raw.version
         except AttributeError:
+            raw_version = 0
+        if not raw_version:
             # Assume HTTP/1.1
             raw_version = 11
         version = {
jkbrzt commented 1 year ago

@alexshpilkin thank you for the detailed report! Fixed in master.