|
| 1 | +use crate::{Client, RequestBuilder}; |
| 2 | +use std::sync::Arc; |
| 3 | +/// Extension trait that adds http request methods |
| 4 | +/// |
| 5 | +/// Blanket implementation provided for all `http_client::HttpClient`s |
| 6 | +pub trait ClientExt { |
| 7 | + /// Construct a new surf Client from this http client |
| 8 | + fn client(&self) -> Client; |
| 9 | + |
| 10 | + /// Builds a `CONNECT` request. |
| 11 | + fn connect(&self, path: &str) -> RequestBuilder { |
| 12 | + self.client().connect(path) |
| 13 | + } |
| 14 | + |
| 15 | + /// Builds a `DELETE` request. |
| 16 | + fn delete(&self, path: &str) -> RequestBuilder { |
| 17 | + self.client().delete(path) |
| 18 | + } |
| 19 | + |
| 20 | + /// Builds a `GET` request. |
| 21 | + fn get(&self, path: &str) -> RequestBuilder { |
| 22 | + self.client().get(path) |
| 23 | + } |
| 24 | + |
| 25 | + /// Builds a `HEAD` request. |
| 26 | + fn head(&self, path: &str) -> RequestBuilder { |
| 27 | + self.client().head(path) |
| 28 | + } |
| 29 | + |
| 30 | + /// Builds an `OPTIONS` request. |
| 31 | + fn options(&self, path: &str) -> RequestBuilder { |
| 32 | + self.client().options(path) |
| 33 | + } |
| 34 | + |
| 35 | + /// Builds a `PATCH` request. |
| 36 | + fn patch(&self, path: &str) -> RequestBuilder { |
| 37 | + self.client().patch(path) |
| 38 | + } |
| 39 | + |
| 40 | + /// Builds a `POST` request. |
| 41 | + fn post(&self, path: &str) -> RequestBuilder { |
| 42 | + self.client().post(path) |
| 43 | + } |
| 44 | + |
| 45 | + /// Builds a `PUT` request. |
| 46 | + fn put(&self, path: &str) -> RequestBuilder { |
| 47 | + self.client().put(path) |
| 48 | + } |
| 49 | + |
| 50 | + /// Builds a `TRACE` request. |
| 51 | + fn trace(&self, path: &str) -> RequestBuilder { |
| 52 | + self.client().trace(path) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<HC: http_client::HttpClient + Clone> ClientExt for HC { |
| 57 | + fn client(&self) -> Client { |
| 58 | + Client::with_http_client(Arc::new(self.clone())) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use futures_util::future::BoxFuture; |
| 65 | + use http_types::{Body, Error, Request, Response}; |
| 66 | + |
| 67 | + #[async_std::test] |
| 68 | + async fn with_a_fake_client() -> http_types::Result<()> { |
| 69 | + #[derive(Debug, Clone)] |
| 70 | + struct MyClient; |
| 71 | + impl http_client::HttpClient for MyClient { |
| 72 | + fn send(&self, _req: Request) -> BoxFuture<'static, Result<Response, Error>> { |
| 73 | + Box::pin(async move { |
| 74 | + let mut response = Response::new(200); |
| 75 | + response.set_body(Body::from_string(String::from("hello"))); |
| 76 | + Ok(response) |
| 77 | + }) |
| 78 | + } |
| 79 | + } |
| 80 | + use super::ClientExt; |
| 81 | + let mut response = MyClient.get("http://hello.example").await?; |
| 82 | + assert_eq!(response.body_string().await?, "hello"); |
| 83 | + assert!(response.status().is_success()); |
| 84 | + |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | +} |
0 commit comments