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

Commit 46f4b1b

Browse files
committed
Merge pull request #179 from irvind/development
Allow non-dictionary header arguments in request method.
2 parents e7ce870 + f0518ad commit 46f4b1b

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import socket
1111
import base64
1212

13+
from collections import Iterable, Mapping
14+
1315
from .response import HTTP11Response
1416
from ..tls import wrap_socket, H2C_PROTOCOL
1517
from ..common.bufsocket import BufferedSocket
@@ -148,8 +150,12 @@ def request(self, method, url, body=None, headers={}):
148150
url = to_bytestring(url)
149151

150152
if not isinstance(headers, HTTPHeaderMap):
151-
# FIXME: Handle things that aren't dictionaries here.
152-
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')
153159

154160
if self._sock is None:
155161
self.connect()

test/test_http11.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,40 @@ def test_basic_request(self):
124124

125125
assert received == expected
126126

127+
def test_iterable_header(self):
128+
c = HTTP11Connection('httpbin.org')
129+
c._sock = sock = DummySocket()
130+
131+
c.request('GET', '/get', headers=(
132+
('User-Agent', 'hyper'),
133+
('Custom-field', 'test'),
134+
('Custom-field2', 'test'),
135+
('Custom-field', 'test2'),
136+
))
137+
138+
expected = (
139+
b"GET /get HTTP/1.1\r\n"
140+
b"User-Agent: hyper\r\n"
141+
b"Custom-field: test\r\n"
142+
b"Custom-field2: test\r\n"
143+
b"Custom-field: test2\r\n"
144+
b"connection: Upgrade, HTTP2-Settings\r\n"
145+
b"upgrade: h2c\r\n"
146+
b"HTTP2-Settings: AAQAAP//\r\n"
147+
b"host: httpbin.org\r\n"
148+
b"\r\n"
149+
)
150+
received = b''.join(sock.queue)
151+
152+
assert received == expected
153+
154+
def test_invalid_header(self):
155+
c = HTTP11Connection('httpbin.org')
156+
c._sock = sock = DummySocket()
157+
158+
with pytest.raises(ValueError):
159+
c.request('GET', '/get', headers=42)
160+
127161
def test_proxy_request(self):
128162
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
129163
c._sock = sock = DummySocket()

0 commit comments

Comments
 (0)