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#[derive(Debug)]
25pub struct Server {
26 inner: Arc<Mutex<InnerServer>>,
27}
28
29impl Server {
30 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 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 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 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 pub fn get(&self, path: &str) -> Request {
72 self.method(Method::GET, path)
73 }
74
75 pub fn post(&self, path: &str) -> Request {
77 self.method(Method::POST, path)
78 }
79
80 pub fn patch(&self, path: &str) -> Request {
82 self.method(Method::PATCH, path)
83 }
84
85 pub fn put(&self, path: &str) -> Request {
87 self.method(Method::PUT, path)
88 }
89
90 pub fn delete(&self, path: &str) -> Request {
92 self.method(Method::DELETE, path)
93 }
94
95 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}