Skip to content

Commit 70aaa6f

Browse files
Add the ability to assert the HTTP version
1 parent 7377408 commit 70aaa6f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

gabbi/httpclient.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,30 @@
1717
import certifi
1818
import urllib3
1919

20-
from gabbi.handlers import jsonhandler
2120
from gabbi import utils
21+
from gabbi.handlers import jsonhandler
2222

2323
# Disable SSL warnings otherwise tests which process stderr will get
2424
# extra information.
2525
urllib3.disable_warnings()
2626

2727

28+
def http_version_int_to_str(http_version: int) -> str:
29+
"""Emulate the urllib3 v2.x HTTP "version_string".
30+
31+
Args:
32+
http_version (int): HTTP version
33+
34+
Returns:
35+
str: version string "HTTP/?"
36+
37+
"""
38+
major = int(http_version / 10)
39+
minor = int(http_version % 10)
40+
41+
return f'HTTP/{major}.{minor}'
42+
43+
2844
class Http(urllib3.PoolManager):
2945
"""A subclass of the ``urllib3.PoolManager`` to munge the data.
3046
@@ -51,9 +67,11 @@ def request(self, absolute_uri, method, body, headers, redirect, timeout):
5167
content = response.data
5268
status = response.status
5369
reason = response.reason
70+
http_version_string = http_version_int_to_str(response.version)
5471
headers = response.headers
5572
headers['status'] = str(status)
5673
headers['reason'] = reason
74+
headers['http_version_string'] = http_version_string
5775

5876
# Shut down open PoolManagers whose connections have completed to
5977
# save on socket file descriptors.
@@ -87,6 +105,7 @@ class VerboseHttp(Http):
87105
HEADER_BLACKLIST = [
88106
'status',
89107
'reason',
108+
'http_version_string',
90109
]
91110

92111
REQUEST_PREFIX = '>'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Test, against wsgi-intercept, that the HTTP version can be asserted.
2+
tests:
3+
4+
- name: http version "header"
5+
url: /
6+
response_headers:
7+
http_version_string: /HTTP\/[0-9]\.[0-9]/

0 commit comments

Comments
 (0)