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 #305 from ojii/more-helpful-error-message
Browse files Browse the repository at this point in the history
More informative error message if no protocol found for http2
  • Loading branch information
Lukasa committed Jan 24, 2017
2 parents 08dd74d + 5e133b6 commit 28bfaba
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
5 changes: 4 additions & 1 deletion hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,10 @@ def connect(self):
proto = H2C_PROTOCOL

log.debug("Selected NPN protocol: %s", proto)
assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL
assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL, (
"No suitable protocol found. Supported protocols: %s. "
"Check your OpenSSL version."
) % ','.join(H2_NPN_PROTOCOLS + [H2C_PROTOCOL])

self._sock = BufferedSocket(sock, self.network_buffer_size)

Expand Down
42 changes: 42 additions & 0 deletions test/test_http20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""
test_http20.py
~~~~~~~~~~~~~~
Unit tests for hyper's HTTP/2.0 implementation.
"""
import pytest
from mock import patch

from server import SocketLevelTest


class TestHTTP20Connection(SocketLevelTest):
h2 = True

def test_useful_error_with_no_protocol(self):
self.set_up()

def socket_handler(listener):
sock = listener.accept()[0]
sock.close()

self._start_server(socket_handler)
conn = self.get_connection()

with patch('hyper.http20.connection.wrap_socket') as mock:
mock.return_value = (None, None)
with pytest.raises(AssertionError) as exc_info:
conn.connect()
assert (
"No suitable protocol found."
in
str(exc_info)
)
assert (
"Check your OpenSSL version."
in
str(exc_info)
)

self.tear_down()
7 changes: 5 additions & 2 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import hyper
import hyper.http11.connection
import pytest
from mock import patch
from h2.frame_buffer import FrameBuffer
from hyper.compat import ssl
from hyper.contrib import HTTP20Adapter
Expand All @@ -34,8 +35,8 @@
hyper.tls._context.check_hostname = False
hyper.tls._context.verify_mode = ssl.CERT_NONE

# Cover our bases because NPN doesn't yet work on all our test platforms.
hyper.http20.connection.H2_NPN_PROTOCOLS += ['', None]
# Cover our bases because NPN doesn't yet work on all our test platforms.
PROTOCOLS = hyper.http20.connection.H2_NPN_PROTOCOLS + ['', None]


def decode_frame(frame_data):
Expand Down Expand Up @@ -76,6 +77,7 @@ def receive_preamble(sock):
return


@patch('hyper.http20.connection.H2_NPN_PROTOCOLS', PROTOCOLS)
class TestHyperIntegration(SocketLevelTest):
# These are HTTP/2 tests.
h2 = True
Expand Down Expand Up @@ -1031,6 +1033,7 @@ def socket_handler(listener):
self.tear_down()


@patch('hyper.http20.connection.H2_NPN_PROTOCOLS', PROTOCOLS)
class TestRequestsAdapter(SocketLevelTest):
# This uses HTTP/2.
h2 = True
Expand Down

0 comments on commit 28bfaba

Please sign in to comment.