1use std::fmt;
23use parking_lot::RwLock;
45use crate::{Rocket, Phase, Orbit, Ignite, Error};
6use crate::local::asynchronous::{LocalRequest, LocalResponse};
7use crate::http::{Method, uri::Origin};
8use crate::listener::Endpoint;
910/// An `async` client to construct and dispatch local requests.
11///
12/// For details, see [the top-level documentation](../index.html#client).
13/// For the `blocking` version, see
14/// [`blocking::Client`](crate::local::blocking::Client).
15///
16/// ## Multithreaded Tracking Synchronization Pitfalls
17///
18/// Unlike its [`blocking`](crate::local::blocking) variant, this `async`
19/// `Client` implements `Sync`. However, using it in a multithreaded environment
20/// while tracking cookies can result in surprising, non-deterministic behavior.
21/// This is because while cookie modifications are serialized, the ordering
22/// depends on the ordering of request dispatch.
23///
24/// If possible, refrain from sharing a single instance of a tracking `Client`
25/// across multiple threads. Instead, prefer to create a unique instance of
26/// `Client` per thread. If this is not possible, ensure that you are not
27/// depending on the ordering of cookie modifications or have arranged for
28/// request dispatch to occur in a deterministic manner.
29///
30/// Alternatively, use an untracked client, which does not suffer from these
31/// pitfalls.
32///
33/// ## Example
34///
35/// The following snippet creates a `Client` from a `Rocket` instance and
36/// dispatches a local `POST /` request with a body of `Hello, world!`.
37///
38/// ```rust,no_run
39/// use rocket::local::asynchronous::Client;
40///
41/// # rocket::async_test(async {
42/// let rocket = rocket::build();
43/// let client = Client::tracked(rocket).await.expect("valid rocket");
44/// let response = client.post("/")
45/// .body("Hello, world!")
46/// .dispatch()
47/// .await;
48/// # });
49/// ```
50pub struct Client {
51 rocket: Rocket<Orbit>,
52 cookies: RwLock<cookie::CookieJar>,
53pub(in super) tracked: bool,
54}
5556impl Client {
57pub(crate) async fn _new<P: Phase>(
58 rocket: Rocket<P>,
59 tracked: bool,
60 secure: bool,
61 ) -> Result<Client, Error> {
62let mut endpoint = Endpoint::new("local client");
63if secure {
64 endpoint = endpoint.assume_tls();
65 }
6667let rocket = rocket.local_launch(endpoint).await?;
68let cookies = RwLock::new(cookie::CookieJar::new());
69Ok(Client { rocket, cookies, tracked })
70 }
7172// WARNING: This is unstable! Do not use this method outside of Rocket!
73 // This is used by the `Client` doctests.
74#[doc(hidden)]
75pub fn _test<T, F>(f: F) -> T
76where F: FnOnce(&Self, LocalRequest<'_>, LocalResponse<'_>) -> T + Send77 {
78crate::async_test(async {
79let client = Client::debug(crate::build()).await.unwrap();
80let request = client.get("/");
81let response = request.clone().dispatch().await;
82f(&client, request, response)
83 })
84 }
8586#[inline(always)]
87pub(crate) fn _rocket(&self) -> &Rocket<Orbit> {
88&self.rocket
89 }
9091#[inline(always)]
92pub(crate) fn _with_raw_cookies<F, T>(&self, f: F) -> T
93where F: FnOnce(&cookie::CookieJar) -> T
94 {
95f(&self.cookies.read())
96 }
9798#[inline(always)]
99pub(crate) fn _with_raw_cookies_mut<F, T>(&self, f: F) -> T
100where F: FnOnce(&mut cookie::CookieJar) -> T
101 {
102f(&mut self.cookies.write())
103 }
104105#[inline(always)]
106fn _req<'c, 'u: 'c, U>(&'c self, method: Method, uri: U) -> LocalRequest<'c>
107where U: TryInto<Origin<'u>> + fmt::Display108 {
109LocalRequest::new(self, method, uri)
110 }
111112pub(crate) async fn _terminate(self) -> Rocket<Ignite> {
113let rocket = self.rocket;
114 rocket.shutdown().notify();
115 rocket.fairings.handle_shutdown(&rocket).await;
116 rocket.deorbit()
117 }
118119// Generates the public API methods, which call the private methods above.
120pub_client_impl!("use rocket::local::asynchronous::Client;" @async await);
121}
122123impl std::fmt::Debugfor Client {
124fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125self._rocket().fmt(f)
126 }
127}
128129#[cfg(test)]
130mod test {
131#[test]
132fn test_local_client_impl_send_sync() {
133fn assert_sync_send<T: Sync + Send>() {}
134 assert_sync_send::<super::Client>();
135 }
136}