diff --git a/src/lib.rs b/src/lib.rs index 1a68a61..51b9122 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ mod tests { #[test] fn simple_request() { - let req: Request = RequestBase::new("https://httpbin.org/"); + let req: Request = RequestBase::new("https://httpbin.org/").unwrap(); let resp = req.launch(); assert_eq!(resp.status_code, StatusCode::OK); assert!(resp.css::("h2")[0].html().contains("httpbin.org")); diff --git a/src/request.rs b/src/request.rs index 324603b..4814576 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,9 +1,11 @@ use crate::response; use http::status::StatusCode; -use url::Url; +use url::{ParseError, Url}; pub trait RequestBase { - fn new(url: &'static str) -> Self; + fn new(url: &'static str) -> Result + where + Self: Sized; fn launch(&self) -> response::Response; } @@ -13,9 +15,10 @@ pub struct Request { } impl RequestBase for Request { - fn new(url: &'static str) -> Request { - Request { - url: Url::parse(url).unwrap(), + fn new(url: &'static str) -> Result { + match Url::parse(url) { + Ok(url_parsed) => Ok(Request { url: url_parsed }), + Err(error) => Err(error), } }