1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 13:02:28 +00:00
deprecated-gotham-restful/tests/util/mod.rs

58 lines
1.7 KiB
Rust
Raw Permalink Normal View History

use gotham::{
hyper::Body,
test::TestServer
};
use mime::Mime;
2020-05-19 21:07:29 +02:00
#[allow(unused_imports)]
use std::{fs::File, io::{Read, Write}, str};
pub fn test_get_response(server : &TestServer, path : &str, expected : &[u8])
{
let res = server.client().get(path).perform().unwrap().read_body().unwrap();
let body : &[u8] = res.as_ref();
assert_eq!(body, expected);
}
pub fn test_post_response<B>(server : &TestServer, path : &str, body : B, mime : Mime, expected : &[u8])
where
B : Into<Body>
{
let res = server.client().post(path, body, mime).perform().unwrap().read_body().unwrap();
let body : &[u8] = res.as_ref();
assert_eq!(body, expected);
}
pub fn test_put_response<B>(server : &TestServer, path : &str, body : B, mime : Mime, expected : &[u8])
where
B : Into<Body>
{
let res = server.client().put(path, body, mime).perform().unwrap().read_body().unwrap();
let body : &[u8] = res.as_ref();
assert_eq!(body, expected);
}
pub fn test_delete_response(server : &TestServer, path : &str, expected : &[u8])
{
let res = server.client().delete(path).perform().unwrap().read_body().unwrap();
let body : &[u8] = res.as_ref();
assert_eq!(body, expected);
}
2020-05-19 21:07:29 +02:00
#[cfg(feature = "openapi")]
pub fn test_openapi_response(server : &TestServer, path : &str, output_file : &str)
{
let res = server.client().get(path).perform().unwrap().read_body().unwrap();
let body = serde_json::to_string_pretty(&serde_json::from_slice::<serde_json::Value>(res.as_ref()).unwrap()).unwrap();
match File::open(output_file) {
Ok(mut file) => {
let mut expected = String::new();
file.read_to_string(&mut expected).unwrap();
assert_eq!(body, expected);
},
Err(_) => {
let mut file = File::create(output_file).unwrap();
file.write_all(body.as_bytes()).unwrap();
}
};
}