mirror of
https://github.com/rwf2/Rocket.git
synced 2024-12-30 14:22:40 +00:00
03127f4dae
This commit adds the 'local::blocking' module and moves the existing asynchronous testing to 'local::asynchronous'. It also includes several changes to improve the local API, bringing it to parity (and beyond) with master. These changes are: * 'LocalRequest' implements 'Clone'. * 'LocalResponse' doesn't implement 'DerefMut<Target=Response>'. Instead, direct methods on the type, such as 'into_string()', can be used to read the 'Response'. * 'Response::body()' returns an '&ResponseBody' as opposed to '&mut ResponseBody', which is returned by a new 'Response::body_mut()'. * '&ResponseBody' implements 'known_size()` to retrieve a body's size, if it is known. Co-authored-by: Jeb Rosen <jeb@jebrosen.com>
116 lines
4.0 KiB
Rust
116 lines
4.0 KiB
Rust
#![feature(proc_macro_hygiene)]
|
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::local::asynchronous::Client;
|
|
use rocket::http::{ContentType, MediaType, Accept, Status};
|
|
|
|
// Test that known formats work as expected, including not colliding.
|
|
|
|
#[post("/", format = "json")]
|
|
fn json() -> &'static str { "json" }
|
|
|
|
#[post("/", format = "xml")]
|
|
fn xml() -> &'static str { "xml" }
|
|
|
|
// Unreachable. Written for codegen.
|
|
#[post("/", format = "application/json", rank = 2)]
|
|
fn json_long() -> &'static str { "json_long" }
|
|
|
|
#[post("/", format = "application/msgpack")]
|
|
fn msgpack_long() -> &'static str { "msgpack_long" }
|
|
|
|
// Unreachable. Written for codegen.
|
|
#[post("/", format = "msgpack", rank = 2)]
|
|
fn msgpack() -> &'static str { "msgpack" }
|
|
|
|
#[get("/", format = "plain")]
|
|
fn plain() -> &'static str { "plain" }
|
|
|
|
#[get("/", format = "binary", rank = 2)]
|
|
fn binary() -> &'static str { "binary" }
|
|
|
|
#[get("/", rank = 3)]
|
|
fn other() -> &'static str { "other" }
|
|
|
|
#[rocket::async_test]
|
|
async fn test_formats() {
|
|
let rocket = rocket::ignite()
|
|
.mount("/", routes![json, xml, json_long, msgpack_long, msgpack,
|
|
plain, binary, other]);
|
|
|
|
let client = Client::new(rocket).await.unwrap();
|
|
|
|
let response = client.post("/").header(ContentType::JSON).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "json");
|
|
|
|
let response = client.post("/").header(ContentType::MsgPack).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "msgpack_long");
|
|
|
|
let response = client.post("/").header(ContentType::XML).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "xml");
|
|
|
|
let response = client.get("/").header(Accept::Plain).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "plain");
|
|
|
|
let response = client.get("/").header(Accept::Binary).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "binary");
|
|
|
|
let response = client.get("/").header(ContentType::JSON).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "plain");
|
|
|
|
let response = client.get("/").dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "plain");
|
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|
|
|
|
// Test custom formats.
|
|
|
|
#[get("/", format = "application/foo")]
|
|
fn get_foo() -> &'static str { "get_foo" }
|
|
|
|
#[post("/", format = "application/foo")]
|
|
fn post_foo() -> &'static str { "post_foo" }
|
|
|
|
#[get("/", format = "bar/baz", rank = 2)]
|
|
fn get_bar_baz() -> &'static str { "get_bar_baz" }
|
|
|
|
#[put("/", format = "bar/baz")]
|
|
fn put_bar_baz() -> &'static str { "put_bar_baz" }
|
|
|
|
#[rocket::async_test]
|
|
async fn test_custom_formats() {
|
|
let rocket = rocket::ignite()
|
|
.mount("/", routes![get_foo, post_foo, get_bar_baz, put_bar_baz]);
|
|
|
|
let client = Client::new(rocket).await.unwrap();
|
|
|
|
let foo_a = Accept::new(&[MediaType::new("application", "foo").into()]);
|
|
let foo_ct = ContentType::new("application", "foo");
|
|
let bar_baz_ct = ContentType::new("bar", "baz");
|
|
let bar_baz_a = Accept::new(&[MediaType::new("bar", "baz").into()]);
|
|
|
|
let response = client.get("/").header(foo_a).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "get_foo");
|
|
|
|
let response = client.post("/").header(foo_ct).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "post_foo");
|
|
|
|
let response = client.get("/").header(bar_baz_a).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "get_bar_baz");
|
|
|
|
let response = client.put("/").header(bar_baz_ct).dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "put_bar_baz");
|
|
|
|
let response = client.get("/").dispatch().await;
|
|
assert_eq!(response.into_string().await.unwrap(), "get_foo");
|
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
let response = client.post("/").header(ContentType::HTML).dispatch().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|