|
| 1 | +extern crate websocket; |
| 2 | +extern crate tokio_core; |
| 3 | +extern crate futures; |
| 4 | + |
| 5 | +use websocket::{ClientBuilder, OwnedMessage}; |
| 6 | +use websocket::result::WebSocketError; |
| 7 | +use tokio_core::reactor::Core; |
| 8 | +use futures::sink::Sink; |
| 9 | +use futures::stream::Stream; |
| 10 | +use futures::Future; |
| 11 | +use futures::future::{self, Loop}; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + let addr = "ws://127.0.0.1:9001".to_string(); |
| 15 | + let agent = "rust-websocket"; |
| 16 | + let mut core = Core::new().unwrap(); |
| 17 | + let handle = core.handle(); |
| 18 | + |
| 19 | + println!("Using fuzzingserver {}", addr); |
| 20 | + println!("Using agent {}", agent); |
| 21 | + |
| 22 | + let case_count = get_case_count(addr.clone(), &mut core); |
| 23 | + println!("We will be running {} test cases!", case_count); |
| 24 | + |
| 25 | + println!("Running test suite..."); |
| 26 | + for case_id in 1..(case_count + 1) { |
| 27 | + let url = addr.clone() + "/runCase?case=" + &case_id.to_string()[..] + "&agent=" + agent; |
| 28 | + |
| 29 | + let test_case = ClientBuilder::new(&url) |
| 30 | + .unwrap() |
| 31 | + .async_connect_insecure(&handle) |
| 32 | + .and_then(move |(duplex, _)| { |
| 33 | + println!("Executing test case: {}/{}", case_id, case_count); |
| 34 | + future::loop_fn(duplex, |stream| { |
| 35 | + stream.into_future() |
| 36 | + .or_else(|(err, stream)| { |
| 37 | + println!("Could not receive message: {:?}", err); |
| 38 | + stream.send(OwnedMessage::Close(None)).map(|s| (None, s)) |
| 39 | + }) |
| 40 | + .and_then(|(msg, stream)| match msg { |
| 41 | + Some(OwnedMessage::Text(txt)) => { |
| 42 | + stream.send(OwnedMessage::Text(txt)) |
| 43 | + .map(|s| Loop::Continue(s)) |
| 44 | + .boxed() |
| 45 | + } |
| 46 | + Some(OwnedMessage::Binary(bin)) => { |
| 47 | + stream.send(OwnedMessage::Binary(bin)) |
| 48 | + .map(|s| Loop::Continue(s)) |
| 49 | + .boxed() |
| 50 | + } |
| 51 | + Some(OwnedMessage::Ping(data)) => { |
| 52 | + stream.send(OwnedMessage::Pong(data)) |
| 53 | + .map(|s| Loop::Continue(s)) |
| 54 | + .boxed() |
| 55 | + } |
| 56 | + Some(OwnedMessage::Close(_)) => { |
| 57 | + stream.send(OwnedMessage::Close(None)) |
| 58 | + .map(|_| Loop::Break(())) |
| 59 | + .boxed() |
| 60 | + } |
| 61 | + Some(OwnedMessage::Pong(_)) => { |
| 62 | + future::ok(Loop::Continue(stream)).boxed() |
| 63 | + } |
| 64 | + None => future::ok(Loop::Break(())).boxed(), |
| 65 | + }) |
| 66 | + }) |
| 67 | + }) |
| 68 | + .map(move |_| { |
| 69 | + println!("Test case {} is finished!", case_id); |
| 70 | + }) |
| 71 | + .or_else(move |err| { |
| 72 | + println!("Test case {} ended with an error: {:?}", case_id, err); |
| 73 | + Ok(()) as Result<(), ()> |
| 74 | + }); |
| 75 | + |
| 76 | + core.run(test_case).ok(); |
| 77 | + } |
| 78 | + |
| 79 | + update_reports(addr.clone(), agent, &mut core); |
| 80 | + println!("Test suite finished!"); |
| 81 | +} |
| 82 | + |
| 83 | +fn get_case_count(addr: String, core: &mut Core) -> usize { |
| 84 | + let url = addr + "/getCaseCount"; |
| 85 | + let err = "Unsupported message in /getCaseCount"; |
| 86 | + |
| 87 | + let counter = ClientBuilder::new(&url) |
| 88 | + .unwrap() |
| 89 | + .async_connect_insecure(&core.handle()) |
| 90 | + .and_then(|(s, _)| s.into_future().map_err(|e| e.0)) |
| 91 | + .and_then(|(msg, _)| match msg { |
| 92 | + Some(OwnedMessage::Text(txt)) => Ok(txt.parse().unwrap()), |
| 93 | + _ => Err(WebSocketError::ProtocolError(err)), |
| 94 | + }); |
| 95 | + core.run(counter).unwrap() |
| 96 | +} |
| 97 | + |
| 98 | +fn update_reports(addr: String, agent: &str, core: &mut Core) { |
| 99 | + println!("Updating reports..."); |
| 100 | + let url = addr + "/updateReports?agent=" + agent; |
| 101 | + |
| 102 | + let updater = ClientBuilder::new(&url) |
| 103 | + .unwrap() |
| 104 | + .async_connect_insecure(&core.handle()) |
| 105 | + .and_then(|(sink, _)| sink.send(OwnedMessage::Close(None))); |
| 106 | + core.run(updater).unwrap(); |
| 107 | + |
| 108 | + println!("Reports updated."); |
| 109 | +} |
0 commit comments