aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--netlib/tcp.py8
-rw-r--r--test/http2/test_http2_protocol.py87
-rw-r--r--test/test_tcp.py5
3 files changed, 92 insertions, 8 deletions
diff --git a/netlib/tcp.py b/netlib/tcp.py
index 98b17c50..eb8a523f 100644
--- a/netlib/tcp.py
+++ b/netlib/tcp.py
@@ -412,7 +412,7 @@ class _Connection(object):
def alpn_select_callback(conn, options):
if alpn_select in options:
return bytes(alpn_select)
- else:
+ else: # pragma no cover
return options[0]
context.set_alpn_select_callback(alpn_select_callback)
@@ -500,9 +500,9 @@ class TCPClient(_Connection):
return self.connection.gettimeout()
def get_alpn_proto_negotiated(self):
- if OpenSSL._util.lib.Cryptography_HAS_ALPN:
+ if OpenSSL._util.lib.Cryptography_HAS_ALPN and self.ssl_established:
return self.connection.get_alpn_proto_negotiated()
- else: # pragma no cover
+ else:
return None
@@ -616,7 +616,7 @@ class BaseHandler(_Connection):
def get_alpn_proto_negotiated(self):
if OpenSSL._util.lib.Cryptography_HAS_ALPN and self.ssl_established:
return self.connection.get_alpn_proto_negotiated()
- else: # pragma no cover
+ else:
return None
diff --git a/test/http2/test_http2_protocol.py b/test/http2/test_http2_protocol.py
index 76a0ffe9..ebd2c9a7 100644
--- a/test/http2/test_http2_protocol.py
+++ b/test/http2/test_http2_protocol.py
@@ -1,4 +1,3 @@
-
import OpenSSL
from netlib import http2
@@ -113,11 +112,11 @@ class TestPerformClientConnectionPreface(test.ServerTestBase):
protocol.perform_client_connection_preface()
-class TestStreamIds():
+class TestClientStreamIds():
c = tcp.TCPClient(("127.0.0.1", 0))
protocol = http2.HTTP2Protocol(c)
- def test_stream_ids(self):
+ def test_client_stream_ids(self):
assert self.protocol.current_stream_id is None
assert self.protocol.next_stream_id() == 1
assert self.protocol.current_stream_id == 1
@@ -127,6 +126,20 @@ class TestStreamIds():
assert self.protocol.current_stream_id == 5
+class TestServerStreamIds():
+ c = tcp.TCPClient(("127.0.0.1", 0))
+ protocol = http2.HTTP2Protocol(c, is_server=True)
+
+ def test_server_stream_ids(self):
+ assert self.protocol.current_stream_id is None
+ assert self.protocol.next_stream_id() == 2
+ assert self.protocol.current_stream_id == 2
+ assert self.protocol.next_stream_id() == 4
+ assert self.protocol.current_stream_id == 4
+ assert self.protocol.next_stream_id() == 6
+ assert self.protocol.current_stream_id == 6
+
+
class TestApplySettings(test.ServerTestBase):
class handler(tcp.BaseHandler):
@@ -242,5 +255,71 @@ class TestReadResponse(test.ServerTestBase):
status, headers, body = protocol.read_response()
assert headers == {':status': '200', 'etag': 'foobar'}
- assert status == '200'
+ assert status == "200"
+ assert body == b'foobar'
+
+
+class TestReadEmptyResponse(test.ServerTestBase):
+ class handler(tcp.BaseHandler):
+
+ def handle(self):
+ self.wfile.write(
+ b'00000801050000000188628594e78c767f'.decode('hex'))
+ self.wfile.flush()
+
+ ssl = True
+
+ def test_read_empty_response(self):
+ c = tcp.TCPClient(("127.0.0.1", self.port))
+ c.connect()
+ c.convert_to_ssl()
+ protocol = http2.HTTP2Protocol(c)
+
+ status, headers, body = protocol.read_response()
+
+ assert headers == {':status': '200', 'etag': 'foobar'}
+ assert status == "200"
+ assert body == b''
+
+
+class TestReadRequest(test.ServerTestBase):
+ class handler(tcp.BaseHandler):
+
+ def handle(self):
+ self.wfile.write(
+ b'000003010400000001828487'.decode('hex'))
+ self.wfile.write(
+ b'000006000100000001666f6f626172'.decode('hex'))
+ self.wfile.flush()
+
+ ssl = True
+
+ def test_read_request(self):
+ c = tcp.TCPClient(("127.0.0.1", self.port))
+ c.connect()
+ c.convert_to_ssl()
+ protocol = http2.HTTP2Protocol(c, is_server=True)
+
+ headers, body = protocol.read_request()
+
+ assert headers == {':method': 'GET', ':path': '/', ':scheme': 'https'}
assert body == b'foobar'
+
+
+class TestCreateResponse():
+ c = tcp.TCPClient(("127.0.0.1", 0))
+
+ def test_create_request_simple(self):
+ bytes = http2.HTTP2Protocol(self.c, is_server=True).create_response(200)
+ assert len(bytes) == 1
+ assert bytes[0] ==\
+ '00000101050000000288'.decode('hex')
+
+ def test_create_request_with_body(self):
+ bytes = http2.HTTP2Protocol(self.c, is_server=True).create_response(
+ 200, [(b'foo', b'bar')], 'foobar')
+ assert len(bytes) == 2
+ assert bytes[0] ==\
+ '00000901040000000288408294e7838c767f'.decode('hex')
+ assert bytes[1] ==\
+ '000006000100000002666f6f626172'.decode('hex')
diff --git a/test/test_tcp.py b/test/test_tcp.py
index d5506556..8aa34d2b 100644
--- a/test/test_tcp.py
+++ b/test/test_tcp.py
@@ -376,6 +376,11 @@ class TestALPN(test.ServerTestBase):
c.convert_to_ssl(alpn_protos=["foobar"])
assert c.get_alpn_proto_negotiated() == "foobar"
+ def test_no_alpn(self):
+ c = tcp.TCPClient(("127.0.0.1", self.port))
+ c.connect()
+ assert c.get_alpn_proto_negotiated() == None
+
else:
def test_none_alpn(self):
c = tcp.TCPClient(("127.0.0.1", self.port))