1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#![deny(warnings)]
use futures::{FutureExt, SinkExt, StreamExt};
use warp::ws::Message;
use warp::Filter;
#[tokio::test]
async fn upgrade() {
let _ = pretty_env_logger::try_init();
let route = warp::ws().map(|ws: warp::ws::Ws| ws.on_upgrade(|_| async {}));
// From https://tools.ietf.org/html/rfc6455#section-1.2
let key = "dGhlIHNhbXBsZSBub25jZQ==";
let accept = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=";
let resp = warp::test::request()
.header("connection", "upgrade")
.header("upgrade", "websocket")
.header("sec-websocket-version", "13")
.header("sec-websocket-key", key)
.reply(&route)
.await;
assert_eq!(resp.status(), 101);
assert_eq!(resp.headers()["connection"], "upgrade");
assert_eq!(resp.headers()["upgrade"], "websocket");
assert_eq!(resp.headers()["sec-websocket-accept"], accept);
let resp = warp::test::request()
.header("connection", "keep-alive, Upgrade")
.header("upgrade", "Websocket")
.header("sec-websocket-version", "13")
.header("sec-websocket-key", key)
.reply(&route)
.await;
assert_eq!(resp.status(), 101);
}
#[tokio::test]
async fn fail() {
let _ = pretty_env_logger::try_init();
let route = warp::any().map(warp::reply);
warp::test::ws()
.handshake(route)
.await
.expect_err("handshake non-websocket route should fail");
}
#[tokio::test]
async fn text() {
let _ = pretty_env_logger::try_init();
let mut client = warp::test::ws()
.handshake(ws_echo())
.await
.expect("handshake");
client.send_text("hello warp").await;
let msg = client.recv().await.expect("recv");
assert_eq!(msg.to_str(), Ok("hello warp"));
}
#[tokio::test]
async fn binary() {
let _ = pretty_env_logger::try_init();
let mut client = warp::test::ws()
.handshake(ws_echo())
.await
.expect("handshake");
client.send(warp::ws::Message::binary(&b"bonk"[..])).await;
let msg = client.recv().await.expect("recv");
assert!(msg.is_binary());
assert_eq!(msg.as_bytes(), &b"bonk"[..]);
}
#[tokio::test]
async fn send_ping() {
let _ = pretty_env_logger::try_init();
let filter = warp::ws().map(|ws: warp::ws::Ws| {
ws.on_upgrade(|mut websocket| {
async move {
websocket.send(Message::ping("srv")).await.unwrap();
// assume the client will pong back
let msg = websocket.next().await.expect("item").expect("ok");
assert!(msg.is_pong());
assert_eq!(msg.as_bytes(), &b"srv"[..]);
}
})
});
let mut client = warp::test::ws().handshake(filter).await.expect("handshake");
let msg = client.recv().await.expect("recv");
assert!(msg.is_ping());
assert_eq!(msg.as_bytes(), &b"srv"[..]);
client.recv_closed().await.expect("closed");
}
#[tokio::test]
async fn echo_pings() {
let _ = pretty_env_logger::try_init();
let mut client = warp::test::ws()
.handshake(ws_echo())
.await
.expect("handshake");
client.send(Message::ping("clt")).await;
// tungstenite sends the PONG first
let msg = client.recv().await.expect("recv");
assert!(msg.is_pong());
assert_eq!(msg.as_bytes(), &b"clt"[..]);
// and then `ws_echo` sends us back the same PING
let msg = client.recv().await.expect("recv");
assert!(msg.is_ping());
assert_eq!(msg.as_bytes(), &b"clt"[..]);
// and then our client would have sent *its* PONG
// and `ws_echo` would send *that* back too
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();
let route =
warp::ws().map(|ws: warp::ws::Ws| ws.on_upgrade(|websocket| websocket.close().map(|_| ())));
let mut client = warp::test::ws().handshake(route).await.expect("handshake");
client.recv_closed().await.expect("closed");
}
#[tokio::test]
async fn limit_message_size() {
let _ = pretty_env_logger::try_init();
let echo = warp::ws().map(|ws: warp::ws::Ws| {
ws.max_message_size(1024).on_upgrade(|websocket| {
// Just echo all messages back...
let (tx, rx) = websocket.split();
rx.forward(tx).map(|result| {
assert!(result.is_err());
assert_eq!(
format!("{}", result.unwrap_err()).as_str(),
"Space limit exceeded: Message too big: 0 + 1025 > 1024"
);
})
})
});
let mut client = warp::test::ws().handshake(echo).await.expect("handshake");
client.send(warp::ws::Message::binary(vec![0; 1025])).await;
client.send_text("hello warp").await;
assert!(client.recv().await.is_err());
}
fn ws_echo() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Copy {
warp::ws().map(|ws: warp::ws::Ws| {
ws.on_upgrade(|websocket| {
// Just echo all messages back...
let (tx, rx) = websocket.split();
rx.inspect(|i| log::debug!("ws recv: {:?}", i))
.forward(tx)
.map(|_| ())
})
})
}
|