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

Commit

Permalink
Merge pull request #179 from irvind/development
Browse files Browse the repository at this point in the history
Allow non-dictionary header arguments in request method.
  • Loading branch information
Lukasa committed Dec 7, 2015
2 parents e7ce870 + f0518ad commit 46f4b1b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ In chronological order:
- Added support for upgrade of plaintext HTTP/1.1 to plaintext HTTP/2.
- Added proxy support.
- Improved IPv6 support.

- Eugene Obukhov (@irvind)

- General code improvements.
10 changes: 8 additions & 2 deletions hyper/http11/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import socket
import base64

from collections import Iterable, Mapping

from .response import HTTP11Response
from ..tls import wrap_socket, H2C_PROTOCOL
from ..common.bufsocket import BufferedSocket
Expand Down Expand Up @@ -148,8 +150,12 @@ def request(self, method, url, body=None, headers={}):
url = to_bytestring(url)

if not isinstance(headers, HTTPHeaderMap):
# FIXME: Handle things that aren't dictionaries here.
headers = HTTPHeaderMap(headers.items())
if isinstance(headers, Mapping):
headers = HTTPHeaderMap(headers.items())
elif isinstance(headers, Iterable):
headers = HTTPHeaderMap(headers)
else:
raise ValueError('Header argument must be a dictionary or an iterable')

if self._sock is None:
self.connect()
Expand Down
34 changes: 34 additions & 0 deletions test/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ def test_basic_request(self):

assert received == expected

def test_iterable_header(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()

c.request('GET', '/get', headers=(
('User-Agent', 'hyper'),
('Custom-field', 'test'),
('Custom-field2', 'test'),
('Custom-field', 'test2'),
))

expected = (
b"GET /get HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"Custom-field: test\r\n"
b"Custom-field2: test\r\n"
b"Custom-field: test2\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP//\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
)
received = b''.join(sock.queue)

assert received == expected

def test_invalid_header(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()

with pytest.raises(ValueError):
c.request('GET', '/get', headers=42)

def test_proxy_request(self):
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
c._sock = sock = DummySocket()
Expand Down

0 comments on commit 46f4b1b

Please sign in to comment.