rocket/local/client.rs
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
macro_rules! req_method {
($import:literal, $NAME:literal, $f:ident, $method:expr) => (
req_method!(@
$import,
$NAME,
concat!("let req = client.", stringify!($f), r#"("/hello");"#),
$f,
$method
);
);
(@$import:literal, $NAME:literal, $use_it:expr, $f:ident, $method:expr) => (
/// Create a local `
#[doc = $NAME]
/// ` request to the URI `uri`.
///
/// When dispatched, the request will be served by the instance of Rocket
/// within `self`. The request is not dispatched automatically. To actually
/// dispatch the request, call [`LocalRequest::dispatch()`] on the returned
/// request.
///
/// # Example
///
/// ```rust,no_run
#[doc = $import]
///
/// # Client::_test(|client, _, _| {
/// let client: &Client = client;
#[doc = $use_it]
/// # });
/// ```
#[inline(always)]
pub fn $f<'c, 'u: 'c, U>(&'c self, uri: U) -> LocalRequest<'c>
where U: TryInto<Origin<'u>> + fmt::Display
{
self.req($method, uri)
}
)
}
macro_rules! pub_client_impl {
($import:literal $(@$prefix:tt $suffix:tt)?) =>
{
/// Construct a new `Client` from an instance of `Rocket` _with_ cookie
/// tracking. This is typically the desired mode of operation for testing.
///
/// # Cookie Tracking
///
/// With cookie tracking enabled, a `Client` propagates cookie changes made
/// by responses to previously dispatched requests. In other words,
/// succeeding requests reflect changes (additions and removals) made by any
/// prior responses.
///
/// Cookie tracking requires synchronization between dispatches. **As such,
/// cookie tracking _should not_ be enabled if a local client is being used
/// to serve requests on multiple threads.**
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network errors,
/// the `Error` is returned.
///
/// ```rust,no_run
#[doc = $import]
///
/// let rocket = rocket::build();
/// let client = Client::tracked(rocket);
/// ```
#[inline(always)]
pub $($prefix)? fn tracked<P: Phase>(rocket: Rocket<P>) -> Result<Self, Error> {
Self::_new(rocket, true) $(.$suffix)?
}
/// Construct a new `Client` from an instance of `Rocket` _without_
/// cookie tracking.
///
/// # Cookie Tracking
///
/// Unlike the [`tracked()`](Client::tracked()) constructor, a `Client`
/// returned from this method _does not_ automatically propagate cookie
/// changes and thus requires no synchronization between dispatches.
///
/// # Errors
///
/// If launching the `Rocket` instance would fail, excepting network
/// errors, the `Error` is returned.
///
/// ```rust,no_run
#[doc = $import]
///
/// let rocket = rocket::build();
/// let client = Client::untracked(rocket);
/// ```
pub $($prefix)? fn untracked<P: Phase>(rocket: Rocket<P>) -> Result<Self, Error> {
Self::_new(rocket, false) $(.$suffix)?
}
/// Terminates `Client` by initiating a graceful shutdown via
/// [`Shutdown::notify()`] and running shutdown fairings.
///
/// This method _must_ be called on a `Client` if graceful shutdown is
/// required for testing as `Drop` _does not_ signal `Shutdown` nor run
/// shutdown fairings. Returns the instance of `Rocket` being managed by
/// this client after all shutdown fairings run to completion.
///
/// [`Shutdown::notify()`]: crate::Shutdown::notify()
///
/// ```rust,no_run
#[doc = $import]
///
/// # fn f(client: Client) {
/// let client: Client = client;
/// let rocket = client.terminate();
/// # }
/// ```
#[inline(always)]
pub $($prefix)? fn terminate(self) -> Rocket<Ignite> {
Self::_terminate(self) $(.$suffix)?
}
#[doc(hidden)]
pub $($prefix)? fn debug_with(routes: Vec<crate::Route>) -> Result<Self, Error> {
let rocket = crate::custom(crate::Config::debug_default());
Self::debug(rocket.mount("/", routes)) $(.$suffix)?
}
#[doc(hidden)]
pub $($prefix)? fn debug(rocket: Rocket<crate::Build>) -> Result<Self, Error> {
use crate::config;
let figment = rocket.figment().clone()
.merge((config::Config::LOG_LEVEL, config::LogLevel::Debug))
.select(config::Config::DEBUG_PROFILE);
Self::tracked(rocket.configure(figment)) $(.$suffix)?
}
/// Deprecated alias to [`Client::tracked()`].
#[deprecated(
since = "0.5.0",
note = "choose between `Client::untracked()` and `Client::tracked()`"
)]
pub $($prefix)? fn new<P: Phase>(rocket: Rocket<P>) -> Result<Self, Error> {
Self::tracked(rocket) $(.$suffix)?
}
/// Returns a reference to the `Rocket` this client is creating requests
/// for.
///
/// # Example
///
/// ```rust
#[doc = $import]
///
/// # Client::_test(|client, _, _| {
/// let client: &Client = client;
/// let rocket = client.rocket();
/// # });
/// ```
#[inline(always)]
pub fn rocket(&self) -> &Rocket<Orbit> {
&*self._rocket()
}
/// Returns a cookie jar containing all of the cookies this client is
/// currently tracking.
///
/// If cookie tracking is disabled, the returned jar will always be empty.
/// Otherwise, it will contains all of the cookies collected from responses
/// to requests dispatched by this client that have not expired.
///
/// # Example
///
/// ```rust
#[doc = $import]
///
/// # Client::_test(|client, _, _| {
/// let client: &Client = client;
/// let cookie = client.cookies();
/// # });
/// ```
#[inline(always)]
pub fn cookies(&self) -> crate::http::CookieJar<'_> {
let config = &self.rocket().config();
let jar = self._with_raw_cookies(|jar| jar.clone());
crate::http::CookieJar::from(jar, config)
}
req_method!($import, "GET", get, Method::Get);
req_method!($import, "PUT", put, Method::Put);
req_method!($import, "POST", post, Method::Post);
req_method!($import, "DELETE", delete, Method::Delete);
req_method!($import, "OPTIONS", options, Method::Options);
req_method!($import, "HEAD", head, Method::Head);
req_method!($import, "PATCH", patch, Method::Patch);
/// Create a local `GET` request to the URI `uri`.
///
/// When dispatched, the request will be served by the instance of
/// Rocket within `self`. The request is not dispatched automatically.
/// To actually dispatch the request, call [`LocalRequest::dispatch()`]
/// on the returned request.
///
/// # Example
///
/// ```rust,no_run
#[doc = $import]
/// use rocket::http::Method;
///
/// # Client::_test(|client, _, _| {
/// let client: &Client = client;
/// client.req(Method::Get, "/hello");
/// # });
/// ```
#[inline(always)]
pub fn req<'c, 'u: 'c, U>(
&'c self,
method: Method,
uri: U
) -> LocalRequest<'c>
where U: TryInto<Origin<'u>> + fmt::Display
{
self._req(method, uri)
}
#[cfg(test)]
#[allow(dead_code)]
fn _ensure_impls_exist() {
fn is_send<T: Send>() {}
is_send::<Self>();
fn is_debug<T: std::fmt::Debug>() {}
is_debug::<Self>();
}
}}