Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
the10thWiz committed May 9, 2022
1 parent 00da4df commit 1400626
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion core/codegen/src/attribute/catch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn _catch(
name: stringify!(#user_catcher_fn_name),
code: #status_code,
handler: monomorphized_function,
route_type: #_Box::new(self),
catcher_type: #_Box::new(self),
}
}

Expand Down
17 changes: 16 additions & 1 deletion core/lib/src/local/asynchronous/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

use parking_lot::RwLock;

use crate::{Rocket, Phase, Orbit, Ignite, Error};
use crate::{Rocket, Phase, Orbit, Ignite, Error, Build};
use crate::local::asynchronous::{LocalRequest, LocalResponse};
use crate::http::{Method, uri::Origin, private::cookie};

Expand Down Expand Up @@ -76,6 +76,21 @@ impl Client {
})
}

// WARNING: This is unstable! Do not use this method outside of Rocket!
// This is used by the `Client` doctests.
#[doc(hidden)]
pub fn _test_with<M, T, F>(mods: M, f: F) -> T
where F: FnOnce(&Self, LocalRequest<'_>, LocalResponse<'_>) -> T + Send,
M: FnOnce(Rocket<Build>) -> Rocket<Build>
{
crate::async_test(async {
let client = Client::debug(mods(crate::build())).await.unwrap();
let request = client.get("/");
let response = request.clone().dispatch().await;
f(&client, request, response)
})
}

#[inline(always)]
pub(crate) fn _rocket(&self) -> &Rocket<Orbit> {
&self.rocket
Expand Down
15 changes: 14 additions & 1 deletion core/lib/src/local/blocking/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::cell::RefCell;

use crate::{Rocket, Phase, Orbit, Ignite, Error};
use crate::{Rocket, Phase, Orbit, Ignite, Error, Build};
use crate::local::{asynchronous, blocking::{LocalRequest, LocalResponse}};
use crate::http::{Method, uri::Origin};

Expand Down Expand Up @@ -54,6 +54,19 @@ impl Client {
f(&client, request, response)
}

// WARNING: This is unstable! Do not use this method outside of Rocket!
// This is used by the `Client` doctests.
#[doc(hidden)]
pub fn _test_with<M, T, F>(mods: M, f: F) -> T
where F: FnOnce(&Self, LocalRequest<'_>, LocalResponse<'_>) -> T + Send,
M: FnOnce(Rocket<Build>) -> Rocket<Build>
{
let client = Client::debug(mods(crate::build())).unwrap();
let request = client.get("/");
let response = request.clone().dispatch();
f(&client, request, response)
}

#[inline(always)]
pub(crate) fn inner(&self) -> &asynchronous::Client {
self.inner.as_ref().expect("internal invariant broken: self.inner is Some")
Expand Down
12 changes: 6 additions & 6 deletions core/lib/src/local/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ macro_rules! pub_response_impl {
/// # Example
///
/// ```rust
/// # use rocket::get;
/// # use rocket::{get, routes};
/// #[get("/")]
/// fn index() -> &'static str { "Hello World" }
#[doc = $doc_prelude]
/// # Client::_test(|_, _, response| {
/// # Client::_test_with(|r| r.mount("/", routes![index]), |_, _, response| {
/// let response: LocalResponse = response;
/// assert!(response.routed_by::<index>())
/// assert!(response.routed_by::<index>());
/// # });
/// ```
///
Expand All @@ -214,13 +214,13 @@ macro_rules! pub_response_impl {
/// # Example
///
/// ```rust
/// # use rocket::get;
/// # use rocket::{catch, catchers};
/// #[catch(404)]
/// fn default_404() -> &'static str { "Hello World" }
#[doc = $doc_prelude]
/// # Client::_test(|_, _, response| {
/// # Client::_test_with(|r| r.register("/", catchers![default_404]), |_, _, response| {
/// let response: LocalResponse = response;
/// assert!(response.caught_by::<default_404>())
/// assert!(response.caught_by::<default_404>());
/// # });
/// ```
///
Expand Down
25 changes: 20 additions & 5 deletions examples/hello/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ fn hello() {

let uri = format!("/?{}{}{}", q("lang", lang), q("emoji", emoji), q("name", name));
let response = client.get(uri).dispatch();
assert!(response.routed_by::<super::hello>(), "Response was not generated by the `hello` route");
assert!(
response.routed_by::<super::hello>(),
"Response was not generated by the `hello` route"
);
assert_eq!(response.into_string().unwrap(), expected);

let uri = format!("/?{}{}{}", q("emoji", emoji), q("name", name), q("lang", lang));
let response = client.get(uri).dispatch();
assert!(response.routed_by::<super::hello>(), "Response was not generated by the `hello` route");
assert!(
response.routed_by::<super::hello>(),
"Response was not generated by the `hello` route"
);
assert_eq!(response.into_string().unwrap(), expected);
}
}
Expand All @@ -44,7 +50,10 @@ fn hello() {
fn hello_world() {
let client = Client::tracked(super::rocket()).unwrap();
let response = client.get("/hello/world").dispatch();
assert!(response.routed_by::<super::world>(), "Response was not generated by the `world` route");
assert!(
response.routed_by::<super::world>(),
"Response was not generated by the `world` route"
);
assert_eq!(response.into_string(), Some("Hello, world!".into()));
}

Expand All @@ -64,13 +73,19 @@ fn wave() {
let real_name = RawStr::new(name).percent_decode_lossy();
let expected = format!("👋 Hello, {} year old named {}!", age, real_name);
let response = client.get(uri).dispatch();
assert!(response.routed_by::<super::wave>(), "Response was not generated by the `wave` route");
assert!(
response.routed_by::<super::wave>(),
"Response was not generated by the `wave` route"
);
assert_eq!(response.into_string().unwrap(), expected);

for bad_age in &["1000", "-1", "bird", "?"] {
let bad_uri = format!("/wave/{}/{}", name, bad_age);
let response = client.get(bad_uri).dispatch();
assert!(response.caught_by::<rocket::catcher::DefaultCatcher>(), "Response was not generated by the default catcher");
assert!(
response.caught_by::<rocket::catcher::DefaultCatcher>(),
"Response was not generated by the default catcher"
);
assert_eq!(response.status(), Status::NotFound);
}
}
Expand Down

0 comments on commit 1400626

Please sign in to comment.