1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 02:19:22 +02:00

added support for websocket testing

This commit is contained in:
Nikolay Kim
2018-01-30 15:13:33 -08:00
parent 76f9542df7
commit 577f91206c
6 changed files with 134 additions and 10 deletions

View File

@ -6,7 +6,7 @@ use std::sync::mpsc;
use std::str::FromStr;
use std::collections::HashMap;
use actix::{Arbiter, SyncAddress, System, msgs};
use actix::{Arbiter, SyncAddress, System, SystemRunner, msgs};
use cookie::Cookie;
use http::{Uri, Method, Version, HeaderMap, HttpTryFrom};
use http::header::{HeaderName, HeaderValue};
@ -25,6 +25,7 @@ use payload::Payload;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use server::{HttpServer, HttpHandler, IntoHttpHandler, ServerSettings};
use ws::{WsClient, WsClientError, WsClientReader, WsClientWriter};
/// The `TestServer` type.
///
@ -54,7 +55,8 @@ use server::{HttpServer, HttpHandler, IntoHttpHandler, ServerSettings};
pub struct TestServer {
addr: net::SocketAddr,
thread: Option<thread::JoinHandle<()>>,
sys: SyncAddress<System>,
system: SystemRunner,
server_sys: SyncAddress<System>,
}
impl TestServer {
@ -95,7 +97,8 @@ impl TestServer {
TestServer {
addr: addr,
thread: Some(join),
sys: sys,
system: System::new("actix-test"),
server_sys: sys,
}
}
@ -131,7 +134,8 @@ impl TestServer {
TestServer {
addr: addr,
thread: Some(join),
sys: sys,
system: System::new("actix-test"),
server_sys: sys,
}
}
@ -162,10 +166,23 @@ impl TestServer {
/// Stop http server
fn stop(&mut self) {
if let Some(handle) = self.thread.take() {
self.sys.send(msgs::SystemExit(0));
self.server_sys.send(msgs::SystemExit(0));
let _ = handle.join();
}
}
/// Execute future on current core
pub fn execute<F, I, E>(&mut self, fut: F) -> Result<I, E>
where F: Future<Item=I, Error=E>
{
self.system.run_until_complete(fut)
}
/// Connect to websocket server
pub fn ws(&mut self) -> Result<(WsClientReader, WsClientWriter), WsClientError> {
let url = self.url("/");
self.system.run_until_complete(WsClient::new(url).connect().unwrap())
}
}
impl Drop for TestServer {

View File

@ -1,6 +1,6 @@
//! Http client request
#![allow(unused_imports, dead_code)]
use std::{io, str};
use std::{fmt, io, str};
use std::rc::Rc;
use std::time::Duration;
use std::cell::UnsafeCell;
@ -299,8 +299,8 @@ impl Future for WsHandshake {
let match_key = if let Some(key) = resp.headers().get(
HeaderName::try_from("SEC-WEBSOCKET-ACCEPT").unwrap())
{
// ... field is constructed by concatenating /key/ ...
// ... with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455)
// field is constructed by concatenating /key/
// with the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (RFC 6455)
const WS_GUID: &[u8] = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
let mut sha1 = Sha1::new();
sha1.update(self.key.as_ref());
@ -336,6 +336,12 @@ pub struct WsClientReader {
inner: Rc<UnsafeCell<Inner>>
}
impl fmt::Debug for WsClientReader {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "WsClientReader()")
}
}
impl WsClientReader {
#[inline]
fn as_mut(&mut self) -> &mut Inner {

View File

@ -74,7 +74,7 @@ const SEC_WEBSOCKET_VERSION: &str = "SEC-WEBSOCKET-VERSION";
/// `WebSocket` Message
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Message {
Text(String),
Binary(Binary),