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 #273 from plucury/development
Browse files Browse the repository at this point in the history
Send ping frame
  • Loading branch information
Lukasa committed Aug 18, 2016
2 parents 0eba9b3 + bf0bfdb commit 2146b71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
19 changes: 19 additions & 0 deletions hyper/http20/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,25 @@ def __init_state(self):

return

def ping(self, opaque_data):
"""
Send a PING frame.
Concurrency
-----------
This method is thread-safe.
:param opaque_data: A bytestring of length 8 that will be sent in the
PING frame.
:returns: Nothing
"""
self.connect()
with self._write_lock:
with self._conn as conn:
conn.ping(to_bytestring(opaque_data))
self._send_outstanding_data()

def request(self, method, url, body=None, headers=None):
"""
This will send a request to the server using the HTTP request method
Expand Down
19 changes: 18 additions & 1 deletion test/test_hyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from hyperframe.frame import (
Frame, DataFrame, RstStreamFrame, SettingsFrame, PushPromiseFrame,
WindowUpdateFrame, HeadersFrame, ContinuationFrame, GoAwayFrame,
FRAME_MAX_ALLOWED_LEN
PingFrame, FRAME_MAX_ALLOWED_LEN
)
from hpack.hpack_compat import Encoder
from hyper.http20.connection import HTTP20Connection
Expand All @@ -15,6 +15,7 @@
combine_repeated_headers, split_repeated_headers, h2_safe_headers
)
from hyper.common.headers import HTTPHeaderMap
from hyper.common.util import to_bytestring
from hyper.compat import zlib_compressobj, is_py2
from hyper.contrib import HTTP20Adapter
import hyper.http20.errors as errors
Expand Down Expand Up @@ -80,6 +81,22 @@ def test_connections_can_parse_ipv6_hosts_and_ports(self):
assert c.proxy_host == 'ffff:aaaa::1'
assert c.proxy_port == 8443

def test_ping(self, frame_buffer):
def data_callback(chunk, **kwargs):
frame_buffer.add_data(chunk)

c = HTTP20Connection('www.google.com')
c._sock = DummySocket()
c._send_cb = data_callback
opaque = '00000000'
c.ping(opaque)

frames = list(frame_buffer)
assert len(frames) == 1
f = frames[0]
assert isinstance(f, PingFrame)
assert f.opaque_data == to_bytestring(opaque)

def test_putrequest_establishes_new_stream(self):
c = HTTP20Connection("www.google.com")

Expand Down

0 comments on commit 2146b71

Please sign in to comment.