Skip to content

Commit 723faf4

Browse files
committed
Merge #454: Fix bitreq doc example
3c0c4ce Fix bitreq doc example (Jamil Lambert, PhD) Pull request description: The example in the docs cause a random CI failure. Change it so that it doesn't rely on `example.com` anymore. Add a timeout to avoid hanging. Closes #434 ACKs for top commit: tcharding: ACK 3c0c4ce Tree-SHA512: 6ac41943f5c6300f61fcb6464c617ef975f7bc5b62e9dac728b641c9ad4cafa7316a8b9c75316351fd8d94e7ebedb8a9772646b648351f6e8e60cd69fce4ab00
2 parents 5267573 + 3c0c4ce commit 723faf4

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

bitreq/src/lib.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,35 @@
8989
//!
9090
//! ## Get
9191
//!
92-
//! This is a simple example of sending a GET request and printing out
93-
//! the response's body, status code, and reason phrase. The `?` are
92+
//! This is a simple example of sending a GET request and checking the
93+
//! response's body, status code, and reason phrase. The `?` are
9494
//! needed because the server could return invalid UTF-8 in the body,
95-
//! or something could go wrong during the download.
95+
//! or something could go wrong while sending the request or receiving
96+
//! the response.
9697
//!
9798
//! ```
9899
//! # #[cfg(feature = "std")]
99-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
100-
//! let response = bitreq::get("http://example.com").send()?;
100+
//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
101+
//! # use std::thread;
102+
//! # use tiny_http::{Response, Server};
103+
//! #
104+
//! # let server = Server::http("127.0.0.1:0")?;
105+
//! # let addr = server.server_addr().to_ip().expect("IP listen addr");
106+
//! # let server_thread = thread::spawn(move || {
107+
//! # let request = server.recv().expect("server recv");
108+
//! # let response = Response::from_string("<html></html>");
109+
//! # let _ = request.respond(response);
110+
//! # });
111+
//! #
112+
//! # let url = format!("http://{addr}/");
113+
//! let response = bitreq::get(&url).with_timeout(10).send()?;
101114
//! assert!(response.as_str()?.contains("</html>"));
102115
//! assert_eq!(200, response.status_code);
103116
//! assert_eq!("OK", response.reason_phrase);
117+
//! # server_thread.join().expect("server thread join");
104118
//! # Ok(()) }
105119
//! # #[cfg(not(feature = "std"))]
106-
//! # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) }
120+
//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { Ok(()) }
107121
//! ```
108122
//!
109123
//! Note: you could change the `get` function to `head` or `put` or

0 commit comments

Comments
 (0)