Rocket/core/codegen/tests/route-data.rs
Sergio Benitez 5d9035ddc1 Keep an op-log for sync 'CookieJar'.
In brief, this commit:

  * Updates to the latest upstream 'cookie', fixing a memory leak.
  * Make changes to 'CookieJar' observable only through 'pending()'.
  * Deprecates 'Client::new()' in favor of 'Client::tracked()'.
  * Makes 'dispatch()' on tracked 'Client's synchronize on cookies.
  * Makes 'Client::untracked()' actually untracked.

This commit updates to the latest 'cookie' which removes support for
'Sync' cookie jars. Instead of relying on 'cookie', this commit
implements an op-log based 'CookieJar' which internally keeps track of
changes. The API is such that changes are only observable through
specialized '_pending()' methods.
2020-10-14 21:37:16 -07:00

54 lines
1.5 KiB
Rust

#[macro_use] extern crate rocket;
use rocket::{Request, Data};
use rocket::local::blocking::Client;
use rocket::request::Form;
use rocket::data::{self, FromData, ToByteUnit};
use rocket::http::{RawStr, ContentType, Status};
// Test that the data parameters works as expected.
#[derive(FromForm)]
struct Inner<'r> {
field: &'r RawStr
}
struct Simple(String);
#[async_trait]
impl FromData for Simple {
type Error = ();
async fn from_data(_: &Request<'_>, data: Data) -> data::Outcome<Self, ()> {
match data.open(64.bytes()).stream_to_string().await {
Ok(string) => data::Outcome::Success(Simple(string)),
Err(_) => data::Outcome::Failure((Status::InternalServerError, ())),
}
}
}
#[post("/f", data = "<form>")]
fn form(form: Form<Inner<'_>>) -> String { form.field.url_decode_lossy() }
#[post("/s", data = "<simple>")]
fn simple(simple: Simple) -> String { simple.0 }
#[test]
fn test_data() {
let rocket = rocket::ignite().mount("/", routes![form, simple]);
let client = Client::tracked(rocket).unwrap();
let response = client.post("/f")
.header(ContentType::Form)
.body("field=this%20is%20here")
.dispatch();
assert_eq!(response.into_string().unwrap(), "this is here");
let response = client.post("/s").body("this is here").dispatch();
assert_eq!(response.into_string().unwrap(), "this is here");
let response = client.post("/s").body("this%20is%20here").dispatch();
assert_eq!(response.into_string().unwrap(), "this%20is%20here");
}