From 63ffea61f5e7208c422c6a6191aa8488fd847e3e Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 29 Jun 2017 20:59:21 -0700 Subject: [PATCH] Start writing tests --- Cargo.toml | 9 ++++- src/proto/settings.rs | 1 + tests/client_request.rs | 89 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 tests/client_request.rs diff --git a/Cargo.toml b/Cargo.toml index cec578d..408f52d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,17 @@ fnv = "1.0.5" ordermap = "0.2.10" [dev-dependencies] +mock-io = { git = "https://github.com/carllerche/mock-io" } + +# Fuzzing +quickcheck = "0.4.1" +rand = "0.3.15" + +# HPACK fixtures hex = "0.2.0" walkdir = "1.0.0" serde = "1.0.0" serde_json = "1.0.0" -quickcheck = "0.4.1" -rand = "0.3.15" # Akamai example tokio-core = "0.1" diff --git a/src/proto/settings.rs b/src/proto/settings.rs index e2385c3..0b8ad4e 100644 --- a/src/proto/settings.rs +++ b/src/proto/settings.rs @@ -120,6 +120,7 @@ impl Sink for Settings } fn poll_complete(&mut self) -> Poll<(), ConnectionError> { + trace!("Settings::poll_complete"); try_ready!(self.try_send_pending()); self.inner.poll_complete() } diff --git a/tests/client_request.rs b/tests/client_request.rs new file mode 100644 index 0000000..78f5126 --- /dev/null +++ b/tests/client_request.rs @@ -0,0 +1,89 @@ +extern crate h2; +extern crate http; +extern crate futures; +extern crate mock_io; +extern crate env_logger; + +use h2::client; +use http::request; + +use futures::*; + +#[test] +fn handshake() { + let _ = ::env_logger::init().unwrap(); + + let mock = mock_io::Builder::new() + .client_handshake() + .build(); + + let mut h2 = client::handshake(mock) + .wait().unwrap(); + + // At this point, the connection should be closed + assert!(Stream::wait(h2).next().is_none()); +} + +#[test] +#[ignore] // Not working yet +fn hello_world() { + let _ = ::env_logger::init().unwrap(); + + let mock = mock_io::Builder::new() + .client_handshake() + // GET https://example.com/ HEADERS frame + .write(&[0, 0, 13, 1, 5, 0, 0, 0, 1, 130, 135, 65, 136, 47, 145, 211, 93, 5, 92, 135, 167, 132]) + // .read(&[0, 0, 0, 1, 5, 0, 0, 0, 1]) + .build(); + + let mut h2 = client::handshake(mock) + .wait().unwrap(); + + let mut request = request::Head::default(); + request.uri = "https://example.com/".parse().unwrap(); + // request.version = version::H2; + + println!("~~~ SEND REQUEST ~~~"); + // Send the request + let mut h2 = h2.send_request(1, request, true).wait().unwrap(); + + println!("~~~ WAIT FOR RESPONSE ~~~"); + // Iterate the response frames + let mut h2 = Stream::wait(h2); + + let headers = h2.next().unwrap(); + + // At this point, the connection should be closed + assert!(h2.next().is_none()); +} + +#[test] +#[ignore] +fn request_without_scheme() { +} + +#[test] +#[ignore] +fn request_with_h1_version() { +} + +const SETTINGS: &'static [u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0]; +const SETTINGS_ACK: &'static [u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0]; + +trait MockH2 { + fn client_handshake(&mut self) -> &mut Self; +} + +impl MockH2 for mock_io::Builder { + fn client_handshake(&mut self) -> &mut Self { + self.write(b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n") + // Settings frame + .write(SETTINGS) + /* + .read(&[0, 0, 0, 4, 0, 0, 0, 0, 0]) + // Settings ACK + .write(&[0, 0, 0, 4, 1, 0, 0, 0, 0]) + .read(&[0, 0, 0, 4, 1, 0, 0, 0, 0]) + */ + } +}