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

Add the ability to assert the HTTP version #331

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions gabbi/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
urllib3.disable_warnings()


def http_version_int_to_str(http_version: int) -> str:
"""Emulate the urllib3 v2.x HTTP "version_string".

Args:
http_version (int): HTTP version

Returns:
str: version string "HTTP/?"

"""
major = int(http_version / 10)
minor = int(http_version % 10)

return f'HTTP/{major}.{minor}'


class Http(urllib3.PoolManager):
"""A subclass of the ``urllib3.PoolManager`` to munge the data.

Expand All @@ -51,9 +67,11 @@ def request(self, absolute_uri, method, body, headers, redirect, timeout):
content = response.data
status = response.status
reason = response.reason
http_version_string = http_version_int_to_str(response.version)
headers = response.headers
headers['status'] = str(status)
headers['reason'] = reason
headers['http_version_string'] = http_version_string

# Shut down open PoolManagers whose connections have completed to
# save on socket file descriptors.
Expand Down Expand Up @@ -87,6 +105,7 @@ class VerboseHttp(Http):
HEADER_BLACKLIST = [
'status',
'reason',
'http_version_string',
]

REQUEST_PREFIX = '>'
Expand Down
7 changes: 7 additions & 0 deletions gabbi/tests/gabbits_intercept/http-version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Test, against wsgi-intercept, that the HTTP version can be asserted.
tests:

- name: http version "header"
url: /
response_headers:
http_version_string: /HTTP\/[0-9]\.[0-9]/