2019-05-25 23:27:04 +00:00
|
|
|
// Rocket sometimes generates mangled identifiers that activate the
|
|
|
|
// non_snake_case lint. We deny the lint in this test to ensure that
|
|
|
|
// code generation uses #[allow(non_snake_case)] in the appropriate places.
|
|
|
|
#![deny(non_snake_case)]
|
|
|
|
|
2018-09-20 04:14:30 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2018-10-23 20:22:26 +00:00
|
|
|
use rocket::http::ext::Normalize;
|
2020-07-05 18:35:36 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2020-07-30 11:17:38 +00:00
|
|
|
use rocket::data::{self, Data, FromData, ToByteUnit};
|
2020-07-22 23:10:02 +00:00
|
|
|
use rocket::request::{Request, Form};
|
2018-10-25 05:14:05 +00:00
|
|
|
use rocket::http::{Status, RawStr, ContentType};
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-22 19:54:52 +00:00
|
|
|
// Use all of the code generation available at once.
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2018-11-27 16:01:47 +00:00
|
|
|
#[derive(FromForm, UriDisplayQuery)]
|
2018-09-20 04:14:30 +00:00
|
|
|
struct Inner<'r> {
|
|
|
|
field: &'r RawStr
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Simple(String);
|
|
|
|
|
2020-07-12 09:23:00 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl FromData for Simple {
|
2018-09-20 04:14:30 +00:00
|
|
|
type Error = ();
|
|
|
|
|
2020-07-12 09:23:00 +00:00
|
|
|
async fn from_data(_: &Request<'_>, data: Data) -> data::Outcome<Self, ()> {
|
2020-07-30 11:17:38 +00:00
|
|
|
let string = data.open(64.bytes()).stream_to_string().await.unwrap();
|
2020-07-22 23:10:02 +00:00
|
|
|
data::Outcome::Success(Simple(string))
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/<a>/<name>/name/<path..>?sky=blue&<sky>&<query..>", format = "json", data = "<simple>", rank = 138)]
|
|
|
|
fn post1(
|
|
|
|
sky: usize,
|
|
|
|
name: &RawStr,
|
|
|
|
a: String,
|
2019-06-13 01:59:25 +00:00
|
|
|
query: Form<Inner<'_>>,
|
2018-09-20 04:14:30 +00:00
|
|
|
path: PathBuf,
|
|
|
|
simple: Simple,
|
|
|
|
) -> String {
|
|
|
|
let string = format!("{}, {}, {}, {}, {}, {}",
|
2018-10-23 20:22:26 +00:00
|
|
|
sky, name, a, query.field, path.normalized_str(), simple.0);
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
let uri = uri!(post2: a, name.url_decode_lossy(), path, sky, query.into_inner());
|
|
|
|
|
|
|
|
format!("({}) ({})", string, uri.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[route(POST, path = "/<a>/<name>/name/<path..>?sky=blue&<sky>&<query..>", format = "json", data = "<simple>", rank = 138)]
|
|
|
|
fn post2(
|
|
|
|
sky: usize,
|
|
|
|
name: &RawStr,
|
|
|
|
a: String,
|
2019-06-13 01:59:25 +00:00
|
|
|
query: Form<Inner<'_>>,
|
2018-09-20 04:14:30 +00:00
|
|
|
path: PathBuf,
|
|
|
|
simple: Simple,
|
|
|
|
) -> String {
|
|
|
|
let string = format!("{}, {}, {}, {}, {}, {}",
|
2018-10-23 20:22:26 +00:00
|
|
|
sky, name, a, query.field, path.normalized_str(), simple.0);
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
let uri = uri!(post2: a, name.url_decode_lossy(), path, sky, query.into_inner());
|
|
|
|
|
|
|
|
format!("({}) ({})", string, uri.to_string())
|
|
|
|
}
|
|
|
|
|
2019-09-09 23:52:36 +00:00
|
|
|
#[allow(dead_code)]
|
2019-05-25 23:27:04 +00:00
|
|
|
#[post("/<_unused_param>?<_unused_query>", data="<_unused_data>")]
|
|
|
|
fn test_unused_params(_unused_param: String, _unused_query: String, _unused_data: Data) {
|
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_full_route() {
|
2018-09-20 04:14:30 +00:00
|
|
|
let rocket = rocket::ignite()
|
|
|
|
.mount("/1", routes![post1])
|
|
|
|
.mount("/2", routes![post2]);
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
|
|
|
let a = "A%20A";
|
|
|
|
let name = "Bob%20McDonald";
|
|
|
|
let path = "this/path/here";
|
|
|
|
let sky = 777;
|
|
|
|
let query = "field=inside";
|
|
|
|
let simple = "data internals";
|
|
|
|
|
|
|
|
let path_part = format!("/{}/{}/name/{}", a, name, path);
|
|
|
|
let query_part = format!("?sky={}&sky=blue&{}", sky, query);
|
|
|
|
let uri = format!("{}{}", path_part, query_part);
|
|
|
|
let expected_uri = format!("{}?sky=blue&sky={}&{}", path_part, sky, query);
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post(&uri).body(simple).dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post(format!("/1{}", uri)).body(simple).dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client
|
2018-09-20 04:14:30 +00:00
|
|
|
.post(format!("/1{}", uri))
|
|
|
|
.header(ContentType::JSON)
|
2019-11-26 16:25:56 +00:00
|
|
|
.body(simple)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
assert_eq!(response.into_string().unwrap(), format!("({}, {}, {}, {}, {}, {}) ({})",
|
2018-09-20 04:14:30 +00:00
|
|
|
sky, name, "A A", "inside", path, simple, expected_uri));
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post(format!("/2{}", uri)).body(simple).dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client
|
2018-09-20 04:14:30 +00:00
|
|
|
.post(format!("/2{}", uri))
|
|
|
|
.header(ContentType::JSON)
|
2019-11-26 16:25:56 +00:00
|
|
|
.body(simple)
|
2020-07-05 18:35:36 +00:00
|
|
|
.dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
assert_eq!(response.into_string().unwrap(), format!("({}, {}, {}, {}, {}, {}) ({})",
|
2018-09-20 04:14:30 +00:00
|
|
|
sky, name, "A A", "inside", path, simple, expected_uri));
|
|
|
|
}
|