Skip to content

Commit

Permalink
Fix panic in response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sagebind committed Jun 8, 2019
1 parent f9cfc09 commit f89778d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
37 changes: 22 additions & 15 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::internal::handler::CurlHandler;
use crate::internal::response::ResponseFuture;
use crate::middleware::Middleware;
use crate::options::*;
use futures::executor;
use futures::executor::block_on;
use http::{Request, Response};
use lazy_static::lazy_static;
use std::fmt;
Expand All @@ -20,15 +20,6 @@ lazy_static! {
);
}

/// Get a reference to a global client instance.
pub(crate) fn global() -> &'static Client {
lazy_static! {
static ref CLIENT: Client = Client::new();
}

&CLIENT
}

/// An HTTP client builder, capable of creating custom
/// [`Client`](struct.Client.html) instances with customized behavior.
///
Expand Down Expand Up @@ -134,6 +125,14 @@ impl Client {
Builder::default().build().expect("client failed to initialize")
}

/// Get a reference to a global client instance.
pub(crate) fn shared() -> &'static Self {
lazy_static! {
static ref CLIENT: Client = Client::new();
}
&CLIENT
}

/// Create a new builder for building a custom client.
pub fn builder() -> Builder {
Builder::new()
Expand All @@ -144,13 +143,21 @@ impl Client {
/// The response body is provided as a stream that may only be consumed
/// once.
pub fn get<U>(&self, uri: U) -> Result<Response<Body>, Error> where http::Uri: http::HttpTryFrom<U> {
let request = http::Request::get(uri).body(Body::default())?;
self.send(request)
block_on(self.get_async(uri))
}

/// Sends an HTTP GET request asynchronously.
///
/// The response body is provided as a stream that may only be consumed
/// once.
pub async fn get_async<U>(&self, uri: U) -> Result<Response<Body>, Error> where http::Uri: http::HttpTryFrom<U> {
let request = http::Request::get(uri).body(Body::empty())?;
self.send_async(request).await
}

/// Sends an HTTP HEAD request.
pub fn head<U>(&self, uri: U) -> Result<Response<Body>, Error> where http::Uri: http::HttpTryFrom<U> {
let request = http::Request::head(uri).body(Body::default())?;
let request = http::Request::head(uri).body(Body::empty())?;
self.send(request)
}

Expand All @@ -177,7 +184,7 @@ impl Client {
/// The response body is provided as a stream that may only be consumed
/// once.
pub fn delete<U>(&self, uri: U) -> Result<Response<Body>, Error> where http::Uri: http::HttpTryFrom<U> {
let request = http::Request::delete(uri).body(Body::default())?;
let request = http::Request::delete(uri).body(Body::empty())?;
self.send(request)
}

Expand All @@ -192,7 +199,7 @@ impl Client {
/// The response body is provided as a stream that may only be consumed
/// once.
pub fn send<B: Into<Body>>(&self, request: Request<B>) -> Result<Response<Body>, Error> {
executor::block_on(self.send_async(request))
block_on(self.send_async(request))
}

/// Begin sending a request and return a future of the response.
Expand Down
21 changes: 15 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
#![feature(async_await)]

use std::future::Future;

pub mod body;
pub mod client;
pub mod options;
Expand Down Expand Up @@ -198,33 +200,40 @@ pub type Response = http::Response<Body>;
///
/// The response body is provided as a stream that may only be consumed once.
pub fn get<U>(uri: U) -> Result<Response, Error> where http::Uri: http::HttpTryFrom<U> {
client::global().get(uri)
Client::shared().get(uri)
}

/// Sends an HTTP GET request asynchronously.
///
/// The response body is provided as a stream that may only be consumed once.
pub fn get_async<U>(uri: U) -> impl Future<Output=Result<Response, Error>> where http::Uri: http::HttpTryFrom<U> {
Client::shared().get_async(uri)
}

/// Sends an HTTP HEAD request.
pub fn head<U>(uri: U) -> Result<Response, Error> where http::Uri: http::HttpTryFrom<U> {
client::global().head(uri)
Client::shared().head(uri)
}

/// Sends an HTTP POST request.
///
/// The response body is provided as a stream that may only be consumed once.
pub fn post<U>(uri: U, body: impl Into<Body>) -> Result<Response, Error> where http::Uri: http::HttpTryFrom<U> {
client::global().post(uri, body)
Client::shared().post(uri, body)
}

/// Sends an HTTP PUT request.
///
/// The response body is provided as a stream that may only be consumed once.
pub fn put<U>(uri: U, body: impl Into<Body>) -> Result<Response, Error> where http::Uri: http::HttpTryFrom<U> {
client::global().put(uri, body)
Client::shared().put(uri, body)
}

/// Sends an HTTP DELETE request.
///
/// The response body is provided as a stream that may only be consumed once.
pub fn delete<U>(uri: U) -> Result<Response, Error> where http::Uri: http::HttpTryFrom<U> {
client::global().delete(uri)
Client::shared().delete(uri)
}

/// Sends an HTTP request.
Expand All @@ -236,7 +245,7 @@ pub fn delete<U>(uri: U) -> Result<Response, Error> where http::Uri: http::HttpT
///
/// The response body is provided as a stream that may only be consumed once.
pub fn send<B: Into<Body>>(request: http::Request<B>) -> Result<Response, Error> {
client::global().send(request.map(|body| body.into()))
Client::shared().send(request.map(|body| body.into()))
}

/// Gets a human-readable string with the version number of cHTTP and its
Expand Down
4 changes: 2 additions & 2 deletions tests/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn simple_response_body() {
});

block_on(async {
let mut response = chttp::get(server.endpoint()).unwrap();
let mut response = chttp::get_async(server.endpoint()).await.unwrap();
let response_text = response.body_mut().text().await.unwrap();
assert_eq!(response_text, "hello world");
})
Expand All @@ -27,7 +27,7 @@ fn large_response_body() {
});

block_on(async {
let mut response = chttp::get(server.endpoint()).unwrap();
let mut response = chttp::get_async(server.endpoint()).await.unwrap();
let response_text = response.body_mut().text().await.unwrap();
assert_eq!(response_text, "wow so large ".repeat(1000));
})
Expand Down

0 comments on commit f89778d

Please sign in to comment.