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

Commit 2146b71

Browse files
authored
Merge pull request #273 from plucury/development
Send ping frame
2 parents 0eba9b3 + bf0bfdb commit 2146b71

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

hyper/http20/connection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ def __init_state(self):
213213

214214
return
215215

216+
def ping(self, opaque_data):
217+
"""
218+
Send a PING frame.
219+
220+
Concurrency
221+
-----------
222+
223+
This method is thread-safe.
224+
225+
:param opaque_data: A bytestring of length 8 that will be sent in the
226+
PING frame.
227+
:returns: Nothing
228+
"""
229+
self.connect()
230+
with self._write_lock:
231+
with self._conn as conn:
232+
conn.ping(to_bytestring(opaque_data))
233+
self._send_outstanding_data()
234+
216235
def request(self, method, url, body=None, headers=None):
217236
"""
218237
This will send a request to the server using the HTTP request method

test/test_hyper.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from hyperframe.frame import (
66
Frame, DataFrame, RstStreamFrame, SettingsFrame, PushPromiseFrame,
77
WindowUpdateFrame, HeadersFrame, ContinuationFrame, GoAwayFrame,
8-
FRAME_MAX_ALLOWED_LEN
8+
PingFrame, FRAME_MAX_ALLOWED_LEN
99
)
1010
from hpack.hpack_compat import Encoder
1111
from hyper.http20.connection import HTTP20Connection
@@ -15,6 +15,7 @@
1515
combine_repeated_headers, split_repeated_headers, h2_safe_headers
1616
)
1717
from hyper.common.headers import HTTPHeaderMap
18+
from hyper.common.util import to_bytestring
1819
from hyper.compat import zlib_compressobj, is_py2
1920
from hyper.contrib import HTTP20Adapter
2021
import hyper.http20.errors as errors
@@ -80,6 +81,22 @@ def test_connections_can_parse_ipv6_hosts_and_ports(self):
8081
assert c.proxy_host == 'ffff:aaaa::1'
8182
assert c.proxy_port == 8443
8283

84+
def test_ping(self, frame_buffer):
85+
def data_callback(chunk, **kwargs):
86+
frame_buffer.add_data(chunk)
87+
88+
c = HTTP20Connection('www.google.com')
89+
c._sock = DummySocket()
90+
c._send_cb = data_callback
91+
opaque = '00000000'
92+
c.ping(opaque)
93+
94+
frames = list(frame_buffer)
95+
assert len(frames) == 1
96+
f = frames[0]
97+
assert isinstance(f, PingFrame)
98+
assert f.opaque_data == to_bytestring(opaque)
99+
83100
def test_putrequest_establishes_new_stream(self):
84101
c = HTTP20Connection("www.google.com")
85102

0 commit comments

Comments
 (0)