1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-04-20 06:54:46 +00:00

openapi spec tests

This commit is contained in:
Dominic 2020-05-19 21:07:29 +02:00
parent 81803fd54a
commit e5e9cd5d3c
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
7 changed files with 431 additions and 9 deletions

View file

@ -3,6 +3,8 @@ use gotham::{
test::TestServer
};
use mime::Mime;
#[allow(unused_imports)]
use std::{fs::File, io::{Read, Write}, str};
pub fn test_get_response(server : &TestServer, path : &str, expected : &[u8])
{
@ -35,3 +37,21 @@ pub fn test_delete_response(server : &TestServer, path : &str, expected : &[u8])
let body : &[u8] = res.as_ref();
assert_eq!(body, expected);
}
#[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();
}
};
}