added the remaining doctests for main lib
This commit is contained in:
parent
c68cff2728
commit
99ede02159
1 changed files with 26 additions and 9 deletions
35
src/lib.rs
35
src/lib.rs
|
@ -49,7 +49,7 @@ use std::{
|
||||||
///
|
///
|
||||||
/// let data = Example { code: 123, message: "hello".to_string() };
|
/// let data = Example { code: 123, message: "hello".to_string() };
|
||||||
/// let json = miniserde::json::to_string(&data);
|
/// 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!(
|
/// assert_eq!(
|
||||||
/// format!("{request}"),
|
/// format!("{request}"),
|
||||||
/// "POST /api HTTP/1.1\r\nHost: example.org\r\n\r\n{\"code\":123,\"message\":\"hello\"}"
|
/// "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.
|
/// Convenience functions are provided for each HTTP method [`Request::get`], [`Request::post`] etc.
|
||||||
///
|
///
|
||||||
/// # Usage
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use request::*;
|
/// # use request::*;
|
||||||
|
@ -90,11 +90,11 @@ impl<'a> Request<'a> {
|
||||||
|
|
||||||
/// Add a body to the request.
|
/// Add a body to the request.
|
||||||
///
|
///
|
||||||
/// # Usage
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use request::*;
|
/// # 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!");
|
/// 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 {
|
pub fn body(self, body: &'a str) -> Self {
|
||||||
|
@ -105,7 +105,7 @@ impl<'a> Request<'a> {
|
||||||
|
|
||||||
/// Add a header to the request.
|
/// Add a header to the request.
|
||||||
///
|
///
|
||||||
/// # Usage
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use request::*;
|
/// # use request::*;
|
||||||
|
@ -119,7 +119,7 @@ impl<'a> Request<'a> {
|
||||||
|
|
||||||
/// Construct a new GET request.
|
/// Construct a new GET request.
|
||||||
///
|
///
|
||||||
/// # Usage
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use request::*;
|
/// # use request::*;
|
||||||
|
@ -131,11 +131,29 @@ impl<'a> Request<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a new POST request.
|
/// 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.
|
/// 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<Response, IoError> {
|
pub fn send(&self) -> Result<Response, IoError> {
|
||||||
// format the message
|
// format the message
|
||||||
let message = format!("{self}");
|
let message = format!("{self}");
|
||||||
|
@ -153,7 +171,6 @@ impl<'a> Request<'a> {
|
||||||
let lines = BufReader::new(stream)
|
let lines = BufReader::new(stream)
|
||||||
.lines()
|
.lines()
|
||||||
.map(|l| l.unwrap())
|
.map(|l| l.unwrap())
|
||||||
.inspect(|l| println!("{l}"))
|
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
let received = lines.join("\n");
|
let received = lines.join("\n");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue