Skip to content

Commit f59de24

Browse files
authored
Add close_frame function to ws::Message (#775)
1 parent c8964b1 commit f59de24

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/filters/ws.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,15 @@ impl Message {
324324
self.inner.is_pong()
325325
}
326326

327+
/// Try to get the close frame (close code and reason)
328+
pub fn close_frame(&self) -> Option<(u16, &str)> {
329+
if let protocol::Message::Close(Some(ref close_frame)) = self.inner {
330+
Some((close_frame.code.into(), close_frame.reason.as_ref()))
331+
} else {
332+
None
333+
}
334+
}
335+
327336
/// Try to get a reference to the string text, if this is a Text message.
328337
pub fn to_str(&self) -> Result<&str, ()> {
329338
match self.inner {

tests/ws.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ async fn binary() {
8181
assert_eq!(msg.as_bytes(), &b"bonk"[..]);
8282
}
8383

84+
#[tokio::test]
85+
async fn close_frame() {
86+
let _ = pretty_env_logger::try_init();
87+
88+
let route = warp::ws().map(|ws: warp::ws::Ws| {
89+
ws.on_upgrade(|mut websocket| async move {
90+
let msg = websocket.next().await.expect("item").expect("ok");
91+
let _ = msg.close_frame().expect("close frame");
92+
})
93+
});
94+
95+
let client = warp::test::ws().handshake(route).await.expect("handshake");
96+
drop(client);
97+
}
98+
8499
#[tokio::test]
85100
async fn send_ping() {
86101
let _ = pretty_env_logger::try_init();

0 commit comments

Comments
 (0)