2020-09-15 15:10:41 +02:00
|
|
|
use gotham::{hyper::header::CONTENT_TYPE, router::builder::*, test::TestServer};
|
2020-05-05 19:50:23 +02:00
|
|
|
use gotham_restful::*;
|
|
|
|
use mime::TEXT_PLAIN;
|
|
|
|
|
2020-09-15 15:10:41 +02:00
|
|
|
const RESPONSE: &[u8] = b"This is the only valid response.";
|
2020-05-05 19:50:23 +02:00
|
|
|
|
|
|
|
#[derive(Resource)]
|
|
|
|
#[resource(create)]
|
|
|
|
struct FooResource;
|
|
|
|
|
|
|
|
#[derive(FromBody, RequestBody)]
|
|
|
|
#[supported_types(TEXT_PLAIN)]
|
|
|
|
struct Foo {
|
|
|
|
content: Vec<u8>,
|
|
|
|
content_type: Mime
|
|
|
|
}
|
|
|
|
|
2021-01-18 00:05:30 +00:00
|
|
|
#[create]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn create(body: Foo) -> Raw<Vec<u8>> {
|
2020-05-05 19:50:23 +02:00
|
|
|
Raw::new(body.content, body.content_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-09-15 15:10:41 +02:00
|
|
|
fn custom_request_body() {
|
2020-05-05 19:50:23 +02:00
|
|
|
let server = TestServer::new(build_simple_router(|router| {
|
|
|
|
router.resource::<FooResource>("foo");
|
2020-09-15 15:10:41 +02:00
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let res = server
|
|
|
|
.client()
|
2020-05-05 19:50:23 +02:00
|
|
|
.post("http://localhost/foo", RESPONSE, TEXT_PLAIN)
|
2020-09-15 15:10:41 +02:00
|
|
|
.perform()
|
|
|
|
.unwrap();
|
2020-05-05 19:50:23 +02:00
|
|
|
assert_eq!(res.headers().get(CONTENT_TYPE).unwrap().to_str().unwrap(), "text/plain");
|
|
|
|
let res = res.read_body().unwrap();
|
2020-09-15 15:10:41 +02:00
|
|
|
let body: &[u8] = res.as_ref();
|
2020-05-05 19:50:23 +02:00
|
|
|
assert_eq!(body, RESPONSE);
|
|
|
|
}
|