mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
feature: allow TestServer to open a websocket on any URL
* added `TestServer::ws_at(uri_str)` * modified `TestServer::ws()` to call `self.ws_at("/")` to preserve behavior Closes #432
This commit is contained in:
parent
2071ea0532
commit
dca4c110dd
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
* Support HTTP/2 with rustls #36
|
* Support HTTP/2 with rustls #36
|
||||||
|
|
||||||
|
* Allow TestServer to open a websocket on any URL # 433
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
* Do not override HOST header for client request #428
|
* Do not override HOST header for client request #428
|
||||||
|
16
src/test.rs
16
src/test.rs
@ -207,13 +207,21 @@ impl TestServer {
|
|||||||
self.rt.block_on(fut)
|
self.rt.block_on(fut)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Connect to websocket server
|
/// Connect to websocket server at a given path
|
||||||
|
pub fn ws_at(
|
||||||
|
&mut self,
|
||||||
|
path: &str,
|
||||||
|
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
|
||||||
|
let url = self.url(path);
|
||||||
|
self.rt
|
||||||
|
.block_on(ws::Client::with_connector(url, self.conn.clone()).connect())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Connect to a websocket server
|
||||||
pub fn ws(
|
pub fn ws(
|
||||||
&mut self,
|
&mut self,
|
||||||
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
|
) -> Result<(ws::ClientReader, ws::ClientWriter), ws::ClientError> {
|
||||||
let url = self.url("/");
|
self.ws_at("/")
|
||||||
self.rt
|
|
||||||
.block_on(ws::Client::with_connector(url, self.conn.clone()).connect())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create `GET` request
|
/// Create `GET` request
|
||||||
|
@ -64,6 +64,46 @@ fn test_simple() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// websocket resource helper function
|
||||||
|
fn start_ws_resource(req: &HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
|
ws::start(req, Ws)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_simple_path() {
|
||||||
|
const PATH:&str = "/v1/ws/";
|
||||||
|
|
||||||
|
// Create a websocket at a specific path.
|
||||||
|
let mut srv = test::TestServer::new(|app| {
|
||||||
|
app.resource(PATH, |r| r.route().f(start_ws_resource));
|
||||||
|
});
|
||||||
|
// fetch the sockets for the resource at a given path.
|
||||||
|
let (reader, mut writer) = srv.ws_at(PATH).unwrap();
|
||||||
|
|
||||||
|
writer.text("text");
|
||||||
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
||||||
|
assert_eq!(item, Some(ws::Message::Text("text".to_owned())));
|
||||||
|
|
||||||
|
writer.binary(b"text".as_ref());
|
||||||
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
item,
|
||||||
|
Some(ws::Message::Binary(Bytes::from_static(b"text").into()))
|
||||||
|
);
|
||||||
|
|
||||||
|
writer.ping("ping");
|
||||||
|
let (item, reader) = srv.execute(reader.into_future()).unwrap();
|
||||||
|
assert_eq!(item, Some(ws::Message::Pong("ping".to_owned())));
|
||||||
|
|
||||||
|
writer.close(Some(ws::CloseCode::Normal.into()));
|
||||||
|
let (item, _) = srv.execute(reader.into_future()).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
item,
|
||||||
|
Some(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_empty_close_code() {
|
fn test_empty_close_code() {
|
||||||
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
|
let mut srv = test::TestServer::new(|app| app.handler(|req| ws::start(req, Ws)));
|
||||||
|
Loading…
Reference in New Issue
Block a user