Skip to content

Commit

Permalink
Allow construction of websocket pong messages (#800)
Browse files Browse the repository at this point in the history
Fixes #782
  • Loading branch information
henkkuli committed Feb 9, 2021
1 parent 53d1ce0 commit 2f97a9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/filters/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ impl Message {
}
}

/// Construct a new Pong `Message`.
///
/// Note that one rarely needs to manually construct a Pong message because the underlying tungstenite socket
/// automatically responds to the Ping messages it receives. Manual construction might still be useful in some cases
/// like in tests or to send unidirectional heartbeats.
pub fn pong<V: Into<Vec<u8>>>(v: V) -> Message {
Message {
inner: protocol::Message::Pong(v.into()),
}
}

/// Construct the default Close `Message`.
pub fn close() -> Message {
Message {
Expand Down
22 changes: 22 additions & 0 deletions tests/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ async fn echo_pings() {
assert_eq!(msg.as_bytes(), &b"clt"[..]);
}

#[tokio::test]
async fn pongs_only() {
let _ = pretty_env_logger::try_init();

let mut client = warp::test::ws()
.handshake(ws_echo())
.await
.expect("handshake");

// construct a pong message and make sure it is correct
let msg = Message::pong("clt");
assert!(msg.is_pong());
assert_eq!(msg.as_bytes(), &b"clt"[..]);

// send it to echo and wait for `ws_echo` to send it back
client.send(msg).await;

let msg = client.recv().await.expect("recv");
assert!(msg.is_pong());
assert_eq!(msg.as_bytes(), &b"clt"[..]);
}

#[tokio::test]
async fn closed() {
let _ = pretty_env_logger::try_init();
Expand Down

0 comments on commit 2f97a9f

Please sign in to comment.