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

Commit 55fe99f

Browse files
committed
Use scheme to determine TLS use from CLI
1 parent 2889eeb commit 55fe99f

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

hyper/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def __init__(self):
123123
self.port = 443
124124
self.query = None
125125
self.scheme = 'https'
126+
self.secure = False
126127

127128
info = UrlInfo()
128129
_result = urlsplit(args._url)
@@ -134,6 +135,9 @@ def __init__(self):
134135
if info.scheme == 'http' and not _result.port:
135136
info.port = 80
136137

138+
# Set the secure arg is the scheme is HTTPS, otherwise do unsecured.
139+
info.secure = info.scheme == 'https'
140+
137141
if info.netloc:
138142
hostname, _ = split_host_and_port(info.netloc)
139143
info.host = hostname # ensure stripping port number
@@ -214,7 +218,7 @@ def get_content_type_and_charset(response):
214218

215219

216220
def request(args):
217-
conn = HTTPConnection(args.url.host, args.url.port)
221+
conn = HTTPConnection(args.url.host, args.url.port, secure=args.url.secure)
218222
conn.request(args.method, args.url.path, args.body, args.headers)
219223
response = conn.get_response()
220224
log.debug('Response Headers:\n%s', pformat(response.headers))

test/test_cli.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ def getheaders(self):
4646

4747

4848
class DummyConnection(object):
49-
def __init__(self, host, port):
49+
def __init__(self, host, port, secure=False):
5050
self.host = host
5151
self.port = port
5252
self.response = DummyResponse({'content-type': 'application/json'})
53+
self.secure = secure
5354

5455
def request(self, method, path, body, headers):
5556
return method, path, body, headers
@@ -177,27 +178,33 @@ def test_set_request_data(args, expected):
177178
@pytest.mark.parametrize(('args', 'expected'), [
178179
(DummyNamespace({'_url': ''}),
179180
{'query': None, 'host': 'localhost', 'fragment': None,
180-
'port': 443, 'netloc': None, 'scheme': 'https', 'path': '/'}),
181+
'port': 443, 'netloc': None, 'scheme': 'https', 'path': '/',
182+
'secure': True}),
181183
(DummyNamespace({'_url': 'example.com'}),
182-
{'host': 'example.com', 'port': 443, 'path': '/'}),
184+
{'host': 'example.com', 'port': 443, 'path': '/', 'secure': True}),
183185
(DummyNamespace({'_url': 'example.com/httpbin/get'}),
184-
{'host': 'example.com', 'port': 443, 'path': '/httpbin/get'}),
186+
{'host': 'example.com', 'port': 443, 'path': '/httpbin/get',
187+
'secure': True}),
185188
(DummyNamespace({'_url': 'example.com:80'}),
186-
{'host': 'example.com', 'port': 80, 'path': '/'}),
189+
{'host': 'example.com', 'port': 80, 'path': '/', 'secure': True}),
187190
(DummyNamespace({'_url': 'http://example.com'}),
188-
{'host': 'example.com', 'port': 80, 'path': '/', 'scheme': 'http'}),
191+
{'host': 'example.com', 'port': 80, 'path': '/', 'scheme': 'http',
192+
'secure': False}),
189193
(DummyNamespace({'_url': 'http://example.com/'}),
190-
{'host': 'example.com', 'port': 80, 'path': '/', 'scheme': 'http'}),
194+
{'host': 'example.com', 'port': 80, 'path': '/', 'scheme': 'http',
195+
'secure': False}),
191196
(DummyNamespace({'_url': 'http://example.com:8080'}),
192-
{'host': 'example.com', 'port': 8080, 'path': '/', 'scheme': 'http'}),
197+
{'host': 'example.com', 'port': 8080, 'path': '/', 'scheme': 'http',
198+
'secure': False}),
193199
(DummyNamespace({'_url': 'https://example.com'}),
194-
{'host': 'example.com', 'port': 443, 'path': '/', 'scheme': 'https'}),
200+
{'host': 'example.com', 'port': 443, 'path': '/', 'scheme': 'https',
201+
'secure': True}),
195202
(DummyNamespace({'_url': 'https://example.com/httpbin/get'}),
196203
{'host': 'example.com', 'port': 443, 'path': '/httpbin/get',
197-
'scheme': 'https'}),
204+
'scheme': 'https', 'secure': True}),
198205
(DummyNamespace({'_url': 'https://example.com:8443/httpbin/get'}),
199206
{'host': 'example.com', 'port': 8443, 'path': '/httpbin/get',
200-
'scheme': 'https'}),
207+
'scheme': 'https', 'secure': True}),
201208
], ids=[
202209
'set no url (it means default settings)',
203210
'set only hostname',

0 commit comments

Comments
 (0)