Add unit tests for handshakes

pull/1/head
Daniel Abramov 7 years ago
parent ddf30ee987
commit dbd9c7775b
  1. 50
      src/lib.rs

@ -191,7 +191,55 @@ impl<T> Sink for WebSocketStream<T> where T: Io {
#[cfg(test)]
mod tests {
use super::*;
use url;
use std::io;
use futures::{Future, Stream};
use tokio_core::net::{TcpStream, TcpListener};
use tokio_core::reactor::Core;
use tungstenite::handshake::server::ServerHandshake;
use tungstenite::handshake::client::{ClientHandshake, Request};
#[test]
fn it_works() {
fn handshakes() {
use std::sync::mpsc::channel;
use std::thread;
let (tx, rx) = channel();
thread::spawn(move || {
let mut core = Core::new().unwrap();
let handle = core.handle();
let address = "0.0.0.0:12345".parse().unwrap();
let listener = TcpListener::bind(&address, &handle).unwrap();
let connections = listener.incoming();
tx.send(()).unwrap();
let handshakes = connections.and_then(|(connection, _)| {
ServerHandshake::<TcpStream>::new_async(connection)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
});
let server = handshakes.for_each(|_| {
Ok(())
});
core.run(server).unwrap();
});
rx.recv().unwrap();
let mut core = Core::new().unwrap();
let handle = core.handle();
let address = "0.0.0.0:12345".parse().unwrap();
let tcp = TcpStream::connect(&address, &handle);
let handshake = tcp.and_then(|stream| {
let url = url::Url::parse("ws://localhost:12345/").unwrap();
ClientHandshake::<TcpStream>::new_async(stream, Request { url: url })
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
});
let client = handshake.and_then(|_| {
Ok(())
});
core.run(client).unwrap();
}
}

Loading…
Cancel
Save