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

Commit 62405b7

Browse files
committed
Merge pull request #122 from t2y/fix-use-httpheadermap-with-cli
Fix cli tool/tests to be available with HTTPHeaderMap
2 parents 25cdd6e + df6bb85 commit 62405b7

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

hyper/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def __init__(self):
149149
if port is not None:
150150
info.port = port
151151

152-
log.debug('url info: %s', vars(info))
152+
log.debug('Url Info: %s', vars(info))
153153
args.url = info
154154

155155

@@ -200,11 +200,11 @@ def parse_argument(argv=None):
200200

201201
def get_content_type_and_charset(response):
202202
charset = 'utf-8'
203-
content_type = response.getheader('content-type')
203+
content_type = response.headers.get('content-type')
204204
if content_type is None:
205205
return 'unknown', charset
206206

207-
content_type = content_type.lower()
207+
content_type = content_type[0].decode('utf-8').lower()
208208
type_and_charset = content_type.split(';', 1)
209209
ctype = type_and_charset[0].strip()
210210
if len(type_and_charset) == 2:
@@ -217,7 +217,7 @@ def request(args):
217217
conn = HTTP20Connection(args.url.host, args.url.port)
218218
conn.request(args.method, args.url.path, args.body, args.headers)
219219
response = conn.get_response()
220-
log.debug('Response Headers:\n%s', pformat(response.getheaders()))
220+
log.debug('Response Headers:\n%s', pformat(response.headers))
221221
ctype, charset = get_content_type_and_charset(response)
222222
data = response.read().decode(charset)
223223
if 'json' in ctype:

test/test_cli.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from hyper.cli import KeyValue
77
from hyper.cli import get_content_type_and_charset, main, parse_argument
88
from hyper.cli import set_request_data, set_url_info
9+
from hyper.common.headers import HTTPHeaderMap
910

1011

1112
# mock for testing
@@ -17,7 +18,7 @@ def __init__(self):
1718
class DummyNamespace(object):
1819
def __init__(self, attrs):
1920
self.body = {}
20-
self.headers = {}
21+
self.headers = HTTPHeaderMap()
2122
self.items = []
2223
self.method = None
2324
self._url = ''
@@ -28,11 +29,13 @@ def __init__(self, attrs):
2829

2930
class DummyResponse(object):
3031
def __init__(self, headers):
31-
self.headers = headers
32+
self.headers = HTTPHeaderMap(headers.items())
3233

3334
def read(self):
34-
if 'json' in self.headers.get('content-type', ''):
35-
return b'{"data": "dummy"}'
35+
ctype = self.headers.get('content-type')
36+
if ctype is not None:
37+
if 'json' in ctype[0].decode('utf-8'):
38+
return b'{"data": "dummy"}'
3639
return b'<html>dummy</html>'
3740

3841
def getheader(self, name):

0 commit comments

Comments
 (0)