kantan/
server.rs

1use ::anyhow::Context;
2use ::anyhow::Result;
3use ::cookie::Cookie;
4use ::cookie::CookieJar;
5use ::hyper::http::Method;
6use ::std::sync::Arc;
7use ::std::sync::Mutex;
8
9use crate::Request;
10
11mod inner_server;
12pub(crate) use self::inner_server::*;
13
14///
15/// The `Server` represents your application, running as a web server,
16/// and you can make web requests to your application.
17///
18/// For most people's needs, this is where to start when writing a test.
19/// This allows you Allowing you to create new requests that will go to this server.
20///
21/// You can make a request against the `Server` by calling the
22/// `get`, `post`, `put`, `delete`, and `patch` methods (you can also use `method`).
23///
24#[derive(Debug)]
25pub struct Server {
26    inner: Arc<Mutex<InnerServer>>,
27}
28
29impl Server {
30    /// This will take the given app, and run it.
31    /// It will use a randomly selected port for running.
32    ///
33    /// This is the same as creating a new `Server` with a configuration,
34    /// and passing `ServerConfig::default()`.
35    pub fn new(server_address: String) -> Result<Self> {
36        let inner_test_server = InnerServer::new(server_address)?;
37        let inner_mutex = Mutex::new(inner_test_server);
38        let inner = Arc::new(inner_mutex);
39
40        Ok(Self { inner })
41    }
42
43    /// Clears all of the cookies stored internally.
44    pub fn clear_cookies(&mut self) {
45        InnerServer::clear_cookies(&mut self.inner)
46            .with_context(|| format!("Trying to clear_cookies"))
47            .unwrap()
48    }
49
50    /// Adds extra cookies to be used on *all* future requests.
51    ///
52    /// Any cookies which have the same name as the new cookies,
53    /// will get replaced.
54    pub fn add_cookies(&mut self, cookies: CookieJar) {
55        InnerServer::add_cookies(&mut self.inner, cookies)
56            .with_context(|| format!("Trying to add_cookies"))
57            .unwrap()
58    }
59
60    /// Adds a cookie to be included on *all* future requests.
61    ///
62    /// If a cookie with the same name already exists,
63    /// then it will be replaced.
64    pub fn add_cookie(&mut self, cookie: Cookie) {
65        InnerServer::add_cookie(&mut self.inner, cookie)
66            .with_context(|| format!("Trying to add_cookie"))
67            .unwrap()
68    }
69
70    /// Creates a HTTP GET request to the path.
71    pub fn get(&self, path: &str) -> Request {
72        self.method(Method::GET, path)
73    }
74
75    /// Creates a HTTP POST request to the given path.
76    pub fn post(&self, path: &str) -> Request {
77        self.method(Method::POST, path)
78    }
79
80    /// Creates a HTTP PATCH request to the path.
81    pub fn patch(&self, path: &str) -> Request {
82        self.method(Method::PATCH, path)
83    }
84
85    /// Creates a HTTP PUT request to the path.
86    pub fn put(&self, path: &str) -> Request {
87        self.method(Method::PUT, path)
88    }
89
90    /// Creates a HTTP DELETE request to the path.
91    pub fn delete(&self, path: &str) -> Request {
92        self.method(Method::DELETE, path)
93    }
94
95    /// Creates a HTTP request, to the path given, using the given method.
96    pub fn method(&self, method: Method, path: &str) -> Request {
97        let debug_method = method.clone();
98        InnerServer::send(&self.inner, method, path)
99            .with_context(|| {
100                format!(
101                    "Trying to create internal request for {} {}",
102                    debug_method, path
103                )
104            })
105            .unwrap()
106    }
107}