Upgrade to hyper v0.13

This commit is contained in:
Glenn Griffin 2019-12-10 13:17:46 -08:00
parent cf0c820da3
commit 6c7daac551
5 changed files with 36 additions and 33 deletions

View File

@ -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"

View File

@ -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();

View File

@ -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<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>>;
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + 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<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>> {
async fn _respond(status_code: u16) -> http::Response<hyper::Body> {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + Send>> {
async fn _respond(status_code: u16) -> hyper::Response<hyper::Body> {
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<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>> {
async fn _respond(body: String) -> http::Response<hyper::Body> {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + Send>> {
async fn _respond(body: String) -> hyper::Response<hyper::Body> {
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<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>> {
async fn _respond(body: String) -> http::Response<hyper::Body> {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + Send>> {
async fn _respond(body: String) -> hyper::Response<hyper::Body> {
hyper::Response::builder()
.status(200)
.header("Content-Type", "application/x-www-form-urlencoded")
@ -91,13 +91,13 @@ impl<B> Responder for hyper::Response<B>
where
B: Clone + Into<hyper::Body> + Send + fmt::Debug,
{
fn respond(&mut self) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>> {
async fn _respond(resp: http::Response<hyper::Body>) -> http::Response<hyper::Body> {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + Send>> {
async fn _respond(resp: hyper::Response<hyper::Body>) -> hyper::Response<hyper::Body> {
resp
}
// Turn &hyper::Response<Vec<u8>> into a hyper::Response<hyper::Body>
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<Box<dyn Responder>>,
}
impl Responder for Cycle {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send>> {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + Send>> {
let response = self.responders[self.idx].respond();
self.idx = (self.idx + 1) % self.responders.len();
response

View File

@ -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<T>(&self, path_and_query: T) -> http::Uri
pub fn url<T>(&self, path_and_query: T) -> hyper::Uri
where
http::uri::PathAndQuery: http::HttpTryFrom<T>,
http::uri::PathAndQuery: std::convert::TryFrom<T>,
<http::uri::PathAndQuery as std::convert::TryFrom<T>>::Error: Into<http::Error>,
{
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<hyper::Body> {
async fn on_req(state: ServerState, req: FullRequest) -> hyper::Response<hyper::Body> {
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<hyper::B
if let Some(f) = response_future {
f.await
} else {
http::Response::builder()
.status(http::StatusCode::INTERNAL_SERVER_ERROR)
hyper::Response::builder()
.status(hyper::StatusCode::INTERNAL_SERVER_ERROR)
.body(hyper::Body::empty())
.unwrap()
}
@ -305,14 +309,14 @@ fn cardinality_error(
matcher: &dyn Matcher<FullRequest>,
cardinality: &Times,
hit_count: usize,
) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send + 'static>> {
) -> Pin<Box<dyn Future<Output = hyper::Response<hyper::Body>> + 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()
})

View File

@ -1,9 +1,10 @@
use httptest::{mappers::*, responders::*, Expectation, Times};
async fn read_response_body(resp: hyper::Response<hyper::Body>) -> hyper::Response<Vec<u8>> {
use futures::stream::TryStreamExt;
async fn read_response_body(
resp: hyper::Response<hyper::Body>,
) -> hyper::Response<hyper::body::Bytes> {
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)
}