Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions hyper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(self):
if port is not None:
info.port = port

log.debug('url info: %s', vars(info))
log.debug('Url Info: %s', vars(info))
args.url = info


Expand Down Expand Up @@ -200,11 +200,11 @@ def parse_argument(argv=None):

def get_content_type_and_charset(response):
charset = 'utf-8'
content_type = response.getheader('content-type')
content_type = response.headers.get('content-type')
if content_type is None:
return 'unknown', charset

content_type = content_type.lower()
content_type = content_type[0].decode('utf-8').lower()
type_and_charset = content_type.split(';', 1)
ctype = type_and_charset[0].strip()
if len(type_and_charset) == 2:
Expand All @@ -217,7 +217,7 @@ def request(args):
conn = HTTP20Connection(args.url.host, args.url.port)
conn.request(args.method, args.url.path, args.body, args.headers)
response = conn.get_response()
log.debug('Response Headers:\n%s', pformat(response.getheaders()))
log.debug('Response Headers:\n%s', pformat(response.headers))
ctype, charset = get_content_type_and_charset(response)
data = response.read().decode(charset)
if 'json' in ctype:
Expand Down
11 changes: 7 additions & 4 deletions test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hyper.cli import KeyValue
from hyper.cli import get_content_type_and_charset, main, parse_argument
from hyper.cli import set_request_data, set_url_info
from hyper.common.headers import HTTPHeaderMap


# mock for testing
Expand All @@ -17,7 +18,7 @@ def __init__(self):
class DummyNamespace(object):
def __init__(self, attrs):
self.body = {}
self.headers = {}
self.headers = HTTPHeaderMap()
self.items = []
self.method = None
self._url = ''
Expand All @@ -28,11 +29,13 @@ def __init__(self, attrs):

class DummyResponse(object):
def __init__(self, headers):
self.headers = headers
self.headers = HTTPHeaderMap(headers.items())

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

def getheader(self, name):
Expand Down