From 99ede021599757c0b2300241d4111916051f4c5a Mon Sep 17 00:00:00 2001 From: vodofrede Date: Thu, 29 Feb 2024 12:56:24 +0100 Subject: [PATCH] added the remaining doctests for main lib --- src/lib.rs | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e84e02f..29dcbbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,7 +49,7 @@ use std::{ /// /// let data = Example { code: 123, message: "hello".to_string() }; /// let json = miniserde::json::to_string(&data); -/// let request = Request::post("example.org/api").body(&json); +/// let request = Request::post("example.org/api", &json); /// assert_eq!( /// format!("{request}"), /// "POST /api HTTP/1.1\r\nHost: example.org\r\n\r\n{\"code\":123,\"message\":\"hello\"}" @@ -72,7 +72,7 @@ impl<'a> Request<'a> { /// /// Convenience functions are provided for each HTTP method [`Request::get`], [`Request::post`] etc. /// - /// # Usage + /// # Examples /// /// ```rust /// # use request::*; @@ -90,11 +90,11 @@ impl<'a> Request<'a> { /// Add a body to the request. /// - /// # Usage + /// # Examples /// /// ```rust /// # use request::*; - /// let request = Request::post("example.org").body("Hello Server!"); + /// let request = Request::new("example.org", Method::POST).body("Hello Server!"); /// assert_eq!(format!("{request}"), "POST / HTTP/1.1\r\nHost: example.org\r\n\r\nHello Server!"); /// ``` pub fn body(self, body: &'a str) -> Self { @@ -105,7 +105,7 @@ impl<'a> Request<'a> { /// Add a header to the request. /// - /// # Usage + /// # Examples /// /// ```rust /// # use request::*; @@ -119,7 +119,7 @@ impl<'a> Request<'a> { /// Construct a new GET request. /// - /// # Usage + /// # Examples /// /// ```rust /// # use request::*; @@ -131,11 +131,29 @@ impl<'a> Request<'a> { } /// Construct a new POST request. - pub fn post(url: &'a str) -> Self { - Request::new(url, Method::POST) + /// + /// # Examples + /// + /// ```rust + /// # use request::*; + /// let request = Request::post("example.org", r#"{ "hello": "world"}"#); + /// assert_eq!(format!("{request}"), "POST / HTTP/1.1\r\nHost: example.org\r\n\r\n{ \"hello\": \"world\"}") + /// ``` + pub fn post(url: &'a str, body: &'a str) -> Self { + Request::new(url, Method::POST).body(body) } /// Dispatch the request. + /// + /// # Examples + /// + /// ```rust + /// # use request::*; + /// // ... start a local server on port 8000 ... + /// let request = Request::get("localhost:8000"); + /// let response = request.send().expect("request failed"); + /// assert_eq!(response.status, 200); + /// ``` pub fn send(&self) -> Result { // format the message let message = format!("{self}"); @@ -153,7 +171,6 @@ impl<'a> Request<'a> { let lines = BufReader::new(stream) .lines() .map(|l| l.unwrap()) - .inspect(|l| println!("{l}")) .collect::>(); let received = lines.join("\n");