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

Commit fc7e8db

Browse files
committed
Send ping frame
1 parent 0eba9b3 commit fc7e8db

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-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: 15 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
@@ -80,6 +80,20 @@ def test_connections_can_parse_ipv6_hosts_and_ports(self):
8080
assert c.proxy_host == 'ffff:aaaa::1'
8181
assert c.proxy_port == 8443
8282

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

0 commit comments

Comments
 (0)