1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
//! URL resolver //! //! `pretend` supports URL resolvers. They are used to //! create a full URL from the path specified in the //! `request` attribute. //! //! By default [`UrlResolver`] is used to append the path //! to a base URL. This resolver is used in `[Pretend::with_url]`. //! //! You can implement your own resolvers to suit your needs. For //! example, you can delegate URL resolution to a load balancer. //! In this case, implement [`ResolveUrl`]. pub use url::{ParseError, Url}; /// Describe an URL resolver /// /// See module level documentation for more information. pub trait ResolveUrl { /// Resolve an URL from a path fn resolve_url(&self, path: &str) -> Result<Url, ParseError>; } /// Default URL resolver /// /// This resolver appends the path to a base URL. pub struct UrlResolver { base: Url, } impl UrlResolver { /// Constructor pub fn new(base: Url) -> Self { UrlResolver { base } } } impl ResolveUrl for UrlResolver { fn resolve_url(&self, path: &str) -> Result<Url, ParseError> { self.base.join(path) } } /// Invalid URL resolver /// /// This resolver is used when calling `[Pretend::for_client]`. It /// is will raise an error when resolving any input. /// /// `[Pretend::with_url]` or `[Pretend::with_url_resolver]` should be used to /// set a valid URL resolver. pub struct InvalidUrlResolver; impl ResolveUrl for InvalidUrlResolver { fn resolve_url(&self, _: &str) -> Result<Url, ParseError> { Err(ParseError::EmptyHost) } }