Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit f1af179

Browse files
committed
Improve header argument handling
1 parent b3bf52f commit f1af179

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

CONTRIBUTORS.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ In chronological order:
3838
- Added support for upgrade of plaintext HTTP/1.1 to plaintext HTTP/2.
3939
- Added proxy support.
4040
- Improved IPv6 support.
41+
42+
- Eugene Obukhov (@irvind)
43+
44+
- General code improvements.

hyper/http11/connection.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import socket
1111
import base64
1212

13-
from collections import Iterable
13+
from collections import Iterable, Mapping
1414

1515
from .response import HTTP11Response
1616
from ..tls import wrap_socket, H2C_PROTOCOL
@@ -150,10 +150,12 @@ def request(self, method, url, body=None, headers={}):
150150
url = to_bytestring(url)
151151

152152
if not isinstance(headers, HTTPHeaderMap):
153-
if isinstance(headers, Iterable):
154-
headers = dict(headers)
155-
156-
headers = HTTPHeaderMap(headers.items())
153+
if isinstance(headers, Mapping):
154+
headers = HTTPHeaderMap(headers.items())
155+
elif isinstance(headers, Iterable):
156+
headers = HTTPHeaderMap(headers)
157+
else:
158+
raise ValueError('Header argument must be a dictionary or an iterable')
157159

158160
if self._sock is None:
159161
self.connect()

test/test_http11.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,21 @@ def test_basic_request(self):
124124

125125
assert received == expected
126126

127-
def test_iterable_headers(self):
127+
def test_iterable_header(self):
128128
c = HTTP11Connection('httpbin.org')
129129
c._sock = sock = DummySocket()
130130

131-
c.request('GET', '/get', headers=(('User-Agent', 'hyper'),))
131+
c.request('GET', '/get', headers=(
132+
('User-Agent', 'hyper'),
133+
('Custom-field', 'test'),
134+
('Custom-field2', 'test'),
135+
))
132136

133137
expected = (
134138
b"GET /get HTTP/1.1\r\n"
135139
b"User-Agent: hyper\r\n"
140+
b"Custom-field: test\r\n"
141+
b"Custom-field2: test\r\n"
136142
b"connection: Upgrade, HTTP2-Settings\r\n"
137143
b"upgrade: h2c\r\n"
138144
b"HTTP2-Settings: AAQAAP//\r\n"
@@ -143,6 +149,13 @@ def test_iterable_headers(self):
143149

144150
assert received == expected
145151

152+
def test_invalid_header(self):
153+
c = HTTP11Connection('httpbin.org')
154+
c._sock = sock = DummySocket()
155+
156+
with pytest.raises(ValueError):
157+
c.request('GET', '/get', headers=42)
158+
146159
def test_proxy_request(self):
147160
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
148161
c._sock = sock = DummySocket()

0 commit comments

Comments
 (0)