try to write unit test

This commit is contained in:
Dietmar Maurer 2018-11-06 13:58:05 +01:00
parent cfa5886c33
commit b8bcc7630c
2 changed files with 55 additions and 8 deletions

View File

@ -1,6 +1,7 @@
use failure::*; use failure::*;
use std::collections::HashMap; use std::collections::HashMap;
use serde_json::{json, Value}; use serde_json::{json, Value};
use url::form_urlencoded;
pub type PropertyMap = HashMap<&'static str, Jss>; pub type PropertyMap = HashMap<&'static str, Jss>;
@ -219,22 +220,72 @@ pub fn parse_parameter_strings(data: &Vec<(String, String)>, schema: &Jss) -> Re
} }
} }
pub fn parse_query_string(query: &str, schema: &Jss) -> Result<Value, Vec<Error>> {
let param_list: Vec<(String, String)> =
form_urlencoded::parse(query.as_bytes()).into_owned().collect();
parse_parameter_strings(&param_list, schema)
}
#[test] #[test]
fn test_shema1() { fn test_shema1() {
let schema = Jss::Object(JssObject { let schema = Jss::Object(JssObject {
description: "TEST", description: "TEST",
optional: None, optional: false,
additional_properties: None, additional_properties: false,
properties: { properties: {
let map = HashMap::new(); let map = HashMap::new();
Box::new(map) map
} }
}); });
println!("TEST Schema: {:?}", schema); println!("TEST Schema: {:?}", schema);
} }
#[test]
fn test_query_boolean() {
let schema = parameter!{force => Boolean!{ optional => false }};
//let res = parse_query_string("", &schema);
//assert!(res.is_err());
let schema = parameter!{force => Boolean!{ optional => true }};
let res = parse_query_string("", &schema);
assert!(res.is_ok());
let res = parse_query_string("a=b", &schema);
assert!(res.is_err());
let res = parse_query_string("force", &schema);
assert!(res.is_err());
let res = parse_query_string("force=yes", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=1", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=On", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=TRUE", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=TREU", &schema);
assert!(res.is_err());
let res = parse_query_string("force=NO", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=0", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=off", &schema);
assert!(res.is_ok());
let res = parse_query_string("force=False", &schema);
assert!(res.is_ok());
}
/* /*
#[test] #[test]
fn test_shema1() { fn test_shema1() {

View File

@ -12,7 +12,6 @@ use apitest::json_schema::*;
//use serde_derive::{Serialize, Deserialize}; //use serde_derive::{Serialize, Deserialize};
use serde_json::{json, Value}; use serde_json::{json, Value};
use url::form_urlencoded;
use hyper::{Method, Body, Request, Response, Server, StatusCode}; use hyper::{Method, Body, Request, Response, Server, StatusCode};
use hyper::rt::Future; use hyper::rt::Future;
@ -65,10 +64,7 @@ fn handle_request(req: Request<Body>) -> Response<Body> {
// extract param // extract param
let param = match query { let param = match query {
Some(data) => { Some(data) => {
let param_list: Vec<(String, String)> = match parse_query_string(data, &api_method.parameters) {
form_urlencoded::parse(data.as_bytes()).into_owned().collect();
match parse_parameter_strings(&param_list, &api_method.parameters) {
Ok(query) => query, Ok(query) => query,
Err(ref error_list) => { Err(ref error_list) => {
let mut msg = String::from(""); let mut msg = String::from("");