diff --git a/Cargo.toml b/Cargo.toml index bdd3f5d..11188e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,11 +7,11 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -hyper = {version = "=0.13.0-alpha.4", features = ["unstable-stream"]} -futures-preview = {version = "=0.3.0-alpha.19", features = ["std", "async-await"]} -tokio = "=0.2.0-alpha.6" +hyper = "0.13" +futures = "0.3" +tokio = { version = "0.2", features = ["macros"] } crossbeam-channel = "0.4.0" -http = "0.1.18" +http = "0.2" log = "0.4.8" bstr = "0.2.8" regex = "1.3.1" diff --git a/src/mappers.rs b/src/mappers.rs index f96f353..51e1a32 100644 --- a/src/mappers.rs +++ b/src/mappers.rs @@ -9,10 +9,8 @@ use std::fmt; // import the any_of and all_of macros from crate root so they are accessible if // people glob import this module. -/// Accept a list of matchers and returns true if all matchers are true. #[doc(inline)] pub use crate::all_of; -/// Accept a list of matchers and returns true if any matcher is true. #[doc(inline)] pub use crate::any_of; @@ -412,7 +410,7 @@ mod tests { ("key2".to_owned(), "".to_owned()), ]; let mut c = request::query(url_decoded(eq(expected))); - let req = http::Request::get("https://example.com/path?key%201=value%201&key2") + let req = hyper::Request::get("https://example.com/path?key%201=value%201&key2") .body("") .unwrap(); diff --git a/src/responders.rs b/src/responders.rs index 9d1a6d1..e0d4f37 100644 --- a/src/responders.rs +++ b/src/responders.rs @@ -13,7 +13,7 @@ pub use crate::cycle; /// Respond with an HTTP response. pub trait Responder: Send + fmt::Debug { /// Return a future that outputs an HTTP response. - fn respond(&mut self) -> Pin> + Send>>; + fn respond(&mut self) -> Pin> + Send>>; } /// respond with the provided status code. @@ -24,8 +24,8 @@ pub fn status_code(code: u16) -> impl Responder { #[derive(Debug)] pub struct StatusCode(u16); impl Responder for StatusCode { - fn respond(&mut self) -> Pin> + Send>> { - async fn _respond(status_code: u16) -> http::Response { + fn respond(&mut self) -> Pin> + Send>> { + async fn _respond(status_code: u16) -> hyper::Response { hyper::Response::builder() .status(status_code) .body(hyper::Body::empty()) @@ -49,8 +49,8 @@ where #[derive(Debug)] pub struct JsonEncoded(String); impl Responder for JsonEncoded { - fn respond(&mut self) -> Pin> + Send>> { - async fn _respond(body: String) -> http::Response { + fn respond(&mut self) -> Pin> + Send>> { + async fn _respond(body: String) -> hyper::Response { hyper::Response::builder() .status(200) .header("Content-Type", "application/json") @@ -75,8 +75,8 @@ where #[derive(Debug)] pub struct UrlEncoded(String); impl Responder for UrlEncoded { - fn respond(&mut self) -> Pin> + Send>> { - async fn _respond(body: String) -> http::Response { + fn respond(&mut self) -> Pin> + Send>> { + async fn _respond(body: String) -> hyper::Response { hyper::Response::builder() .status(200) .header("Content-Type", "application/x-www-form-urlencoded") @@ -91,13 +91,13 @@ impl Responder for hyper::Response where B: Clone + Into + Send + fmt::Debug, { - fn respond(&mut self) -> Pin> + Send>> { - async fn _respond(resp: http::Response) -> http::Response { + fn respond(&mut self) -> Pin> + Send>> { + async fn _respond(resp: hyper::Response) -> hyper::Response { resp } // Turn &hyper::Response> into a hyper::Response let mut builder = hyper::Response::builder(); - builder + builder = builder .status(self.status().clone()) .version(self.version().clone()); *builder.headers_mut().unwrap() = self.headers().clone(); @@ -121,7 +121,7 @@ pub struct Cycle { responders: Vec>, } impl Responder for Cycle { - fn respond(&mut self) -> Pin> + Send>> { + fn respond(&mut self) -> Pin> + Send>> { let response = self.responders[self.idx].respond(); self.idx = (self.idx + 1) % self.responders.len(); response diff --git a/src/server.rs b/src/server.rs index da16346..41c2f2b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -43,8 +43,7 @@ impl Server { async move { // read the full body into memory prior to handing it to mappers. let (head, body) = req.into_parts(); - use futures::TryStreamExt; - let full_body = body.try_concat().await?; + let full_body = hyper::body::to_bytes(body).await?; let req = hyper::Request::from_parts(head, full_body.to_vec()); log::debug!("Received Request: {:?}", req); let resp = on_req(state, req).await; @@ -61,7 +60,11 @@ impl Server { let addr = server.local_addr(); let (trigger_shutdown, shutdown_received) = futures::channel::oneshot::channel(); let join_handle = std::thread::spawn(move || { - let mut runtime = tokio::runtime::current_thread::Runtime::new().unwrap(); + let mut runtime = tokio::runtime::Builder::new() + .basic_scheduler() + .enable_all() + .build() + .unwrap(); runtime.block_on(async move { futures::select! { _ = server.fuse() => {}, @@ -88,11 +91,12 @@ impl Server { /// If the server is listening on port 1234. /// /// `server.url("/foo?q=1") == "http://localhost:1234/foo?q=1"` - pub fn url(&self, path_and_query: T) -> http::Uri + pub fn url(&self, path_and_query: T) -> hyper::Uri where - http::uri::PathAndQuery: http::HttpTryFrom, + http::uri::PathAndQuery: std::convert::TryFrom, + >::Error: Into, { - http::Uri::builder() + hyper::Uri::builder() .scheme("http") .authority(format!("{}", &self.addr).as_str()) .path_and_query(path_and_query) @@ -156,7 +160,7 @@ impl Drop for Server { } } -async fn on_req(state: ServerState, req: FullRequest) -> http::Response { +async fn on_req(state: ServerState, req: FullRequest) -> hyper::Response { let response_future = { let mut state = state.lock(); let mut iter = state.expected.iter_mut(); @@ -198,8 +202,8 @@ async fn on_req(state: ServerState, req: FullRequest) -> http::Response, cardinality: &Times, hit_count: usize, -) -> Pin> + Send + 'static>> { +) -> Pin> + Send + 'static>> { let body = hyper::Body::from(format!( "Unexpected number of requests for matcher '{:?}'; received {}; expected {:?}", matcher, hit_count, cardinality, )); Box::pin(async move { - http::Response::builder() - .status(http::StatusCode::INTERNAL_SERVER_ERROR) + hyper::Response::builder() + .status(hyper::StatusCode::INTERNAL_SERVER_ERROR) .body(body) .unwrap() }) diff --git a/tests/tests.rs b/tests/tests.rs index 474f6ab..452963f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,9 +1,10 @@ use httptest::{mappers::*, responders::*, Expectation, Times}; -async fn read_response_body(resp: hyper::Response) -> hyper::Response> { - use futures::stream::TryStreamExt; +async fn read_response_body( + resp: hyper::Response, +) -> hyper::Response { let (head, body) = resp.into_parts(); - let body = body.try_concat().await.unwrap().to_vec(); + let body = hyper::body::to_bytes(body).await.unwrap(); hyper::Response::from_parts(head, body) }