2019-03-18 05:02:03 +00:00
|
|
|
use actix::prelude::*;
|
2021-04-01 05:40:10 +00:00
|
|
|
use actix_web::{
|
|
|
|
http::{header, StatusCode},
|
2021-04-02 07:26:59 +00:00
|
|
|
web, App, HttpRequest, HttpResponse,
|
2021-04-01 05:40:10 +00:00
|
|
|
};
|
2019-03-18 05:02:03 +00:00
|
|
|
use actix_web_actors::*;
|
2019-12-15 16:45:38 +00:00
|
|
|
use bytes::Bytes;
|
2021-04-01 14:26:13 +00:00
|
|
|
use futures_util::{SinkExt as _, StreamExt as _};
|
2019-03-18 05:02:03 +00:00
|
|
|
|
|
|
|
struct Ws;
|
|
|
|
|
|
|
|
impl Actor for Ws {
|
2019-03-18 05:11:50 +00:00
|
|
|
type Context = ws::WebsocketContext<Self>;
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Ws {
|
2021-02-11 23:03:17 +00:00
|
|
|
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
|
2019-12-15 16:45:38 +00:00
|
|
|
match msg.unwrap() {
|
2019-03-18 05:31:10 +00:00
|
|
|
ws::Message::Ping(msg) => ctx.pong(&msg),
|
|
|
|
ws::Message::Text(text) => ctx.text(text),
|
|
|
|
ws::Message::Binary(bin) => ctx.binary(bin),
|
|
|
|
ws::Message::Close(reason) => ctx.close(reason),
|
2021-01-04 01:01:35 +00:00
|
|
|
_ => {}
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_simple() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let mut srv = actix_test::start(|| {
|
2019-12-15 16:45:38 +00:00
|
|
|
App::new().service(web::resource("/").to(
|
2021-02-11 23:03:17 +00:00
|
|
|
|req: HttpRequest, stream: web::Payload| async move { ws::start(Ws, &req, stream) },
|
2019-12-15 16:45:38 +00:00
|
|
|
))
|
|
|
|
});
|
2019-03-18 05:02:03 +00:00
|
|
|
|
|
|
|
// client service
|
2019-12-15 16:45:38 +00:00
|
|
|
let mut framed = srv.ws().await.unwrap();
|
2021-01-04 11:27:32 +00:00
|
|
|
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Binary("text".into()))
|
|
|
|
.await
|
2019-03-18 05:02:03 +00:00
|
|
|
.unwrap();
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
2020-07-21 23:28:33 +00:00
|
|
|
assert_eq!(item, ws::Frame::Binary(Bytes::from_static(b"text")));
|
2019-12-15 16:45:38 +00:00
|
|
|
|
|
|
|
framed.send(ws::Message::Ping("text".into())).await.unwrap();
|
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Pong(Bytes::copy_from_slice(b"text")));
|
2019-03-18 05:02:03 +00:00
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
framed
|
|
|
|
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
|
|
.await
|
2019-03-18 05:02:03 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2019-12-15 16:45:38 +00:00
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
|
2019-03-18 05:02:03 +00:00
|
|
|
}
|
2021-04-01 05:40:10 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_with_credentials() {
|
2021-04-02 07:26:59 +00:00
|
|
|
let mut srv = actix_test::start(|| {
|
2021-04-01 05:40:10 +00:00
|
|
|
App::new().service(web::resource("/").to(
|
|
|
|
|req: HttpRequest, stream: web::Payload| async move {
|
|
|
|
if req.headers().contains_key("Authorization") {
|
|
|
|
ws::start(Ws, &req, stream)
|
|
|
|
} else {
|
|
|
|
Ok(HttpResponse::new(StatusCode::UNAUTHORIZED))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
// client service without credentials
|
|
|
|
match srv.ws().await {
|
|
|
|
Ok(_) => panic!("WebSocket client without credentials should panic"),
|
|
|
|
Err(awc::error::WsClientError::InvalidResponseStatus(status)) => {
|
|
|
|
assert_eq!(status, StatusCode::UNAUTHORIZED)
|
|
|
|
}
|
|
|
|
Err(e) => panic!("Invalid error from WebSocket client: {}", e),
|
|
|
|
}
|
|
|
|
|
|
|
|
let headers = srv.client_headers().unwrap();
|
|
|
|
headers.insert(
|
|
|
|
header::AUTHORIZATION,
|
|
|
|
header::HeaderValue::from_static("Bearer Something"),
|
|
|
|
);
|
|
|
|
|
|
|
|
// client service with credentials
|
|
|
|
let client = srv.ws();
|
|
|
|
|
|
|
|
let mut framed = client.await.unwrap();
|
|
|
|
|
|
|
|
framed.send(ws::Message::Text("text".into())).await.unwrap();
|
|
|
|
|
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Text(Bytes::from_static(b"text")));
|
|
|
|
|
|
|
|
framed
|
|
|
|
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let item = framed.next().await.unwrap().unwrap();
|
|
|
|
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));
|
|
|
|
}
|