2023-06-18 20:43:10 +00:00
|
|
|
#![cfg(feature = "msgpack")]
|
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2023-06-18 20:43:10 +00:00
|
|
|
use rocket::{Rocket, Build};
|
2024-08-10 06:10:33 +00:00
|
|
|
use rocket::serde::msgpack::{self, MsgPack, Compact};
|
2023-06-18 20:43:10 +00:00
|
|
|
use rocket::local::blocking::Client;
|
|
|
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Eq)]
|
2024-08-10 06:10:33 +00:00
|
|
|
struct Person<'r> {
|
|
|
|
name: &'r str,
|
2023-06-18 20:43:10 +00:00
|
|
|
age: u8,
|
|
|
|
gender: Gender,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(serde::Serialize, serde::Deserialize, PartialEq, Eq)]
|
|
|
|
enum Gender {
|
|
|
|
Male,
|
|
|
|
Female,
|
|
|
|
NonBinary,
|
|
|
|
}
|
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
#[rocket::post("/named", data = "<person>")]
|
|
|
|
fn named(person: MsgPack<Person<'_>>) -> MsgPack<Person<'_>> {
|
|
|
|
person
|
2023-06-18 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
#[rocket::post("/compact", data = "<person>")]
|
|
|
|
fn compact(person: MsgPack<Person<'_>>) -> Compact<Person<'_>> {
|
|
|
|
MsgPack(person.into_inner())
|
2023-06-18 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> Rocket<Build> {
|
2024-08-10 06:10:33 +00:00
|
|
|
rocket::build().mount("/", rocket::routes![named, compact])
|
2023-06-18 20:43:10 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
// The object we're going to roundtrip through the API.
|
|
|
|
const OBJECT: Person<'static> = Person {
|
|
|
|
name: "Cal",
|
|
|
|
age: 17,
|
|
|
|
gender: Gender::NonBinary,
|
|
|
|
};
|
2023-06-18 20:43:10 +00:00
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
// [ "Cal", 17, "NonBinary" ]
|
|
|
|
const COMPACT_BYTES: &[u8] = &[
|
|
|
|
147, 163, 67, 97, 108, 17, 169, 78, 111, 110, 66, 105, 110, 97, 114, 121
|
|
|
|
];
|
2024-08-10 03:33:37 +00:00
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
// { "name": "Cal", "age": 17, "gender": "NonBinary" }
|
|
|
|
const NAMED_BYTES: &[u8] = &[
|
|
|
|
131, 164, 110, 97, 109, 101, 163, 67, 97, 108, 163, 97, 103, 101, 17, 166,
|
|
|
|
103, 101, 110, 100, 101, 114, 169, 78, 111, 110, 66, 105, 110, 97, 114, 121
|
|
|
|
];
|
2023-06-18 20:43:10 +00:00
|
|
|
|
|
|
|
#[test]
|
2024-08-10 06:10:33 +00:00
|
|
|
fn check_roundtrip() {
|
2023-06-18 20:43:10 +00:00
|
|
|
let client = Client::debug(rocket()).unwrap();
|
2024-08-10 06:10:33 +00:00
|
|
|
let inputs: &[(&'static str, Cow<'static, [u8]>)] = &[
|
|
|
|
("objpack", msgpack::to_vec(&OBJECT).unwrap().into()),
|
|
|
|
("named bytes", NAMED_BYTES.into()),
|
|
|
|
("compact bytes", COMPACT_BYTES.into()),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (name, input) in inputs {
|
|
|
|
let compact = client.post("/compact").body(input).dispatch();
|
|
|
|
assert_eq!(compact.into_bytes().unwrap(), COMPACT_BYTES, "{name} mismatch");
|
2024-08-10 03:33:37 +00:00
|
|
|
|
2024-08-10 06:10:33 +00:00
|
|
|
let named = client.post("/named").body(input).dispatch();
|
|
|
|
assert_eq!(named.into_bytes().unwrap(), NAMED_BYTES, "{name} mismatch");
|
|
|
|
}
|
2023-06-18 20:43:10 +00:00
|
|
|
}
|