Skip to content

Commit

Permalink
Add close_frame function to ws::Message (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra authored Jan 14, 2021
1 parent c8964b1 commit f59de24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/filters/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,15 @@ impl Message {
self.inner.is_pong()
}

/// Try to get the close frame (close code and reason)
pub fn close_frame(&self) -> Option<(u16, &str)> {
if let protocol::Message::Close(Some(ref close_frame)) = self.inner {
Some((close_frame.code.into(), close_frame.reason.as_ref()))
} else {
None
}
}

/// Try to get a reference to the string text, if this is a Text message.
pub fn to_str(&self) -> Result<&str, ()> {
match self.inner {
Expand Down
15 changes: 15 additions & 0 deletions tests/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ async fn binary() {
assert_eq!(msg.as_bytes(), &b"bonk"[..]);
}

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

let route = warp::ws().map(|ws: warp::ws::Ws| {
ws.on_upgrade(|mut websocket| async move {
let msg = websocket.next().await.expect("item").expect("ok");
let _ = msg.close_frame().expect("close frame");
})
});

let client = warp::test::ws().handshake(route).await.expect("handshake");
drop(client);
}

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

0 comments on commit f59de24

Please sign in to comment.