From b8bcc7630cafa197c2c9f85291e8405054b255cc Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 6 Nov 2018 13:58:05 +0100 Subject: [PATCH] try to write unit test --- src/json_schema.rs | 57 +++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 6 +---- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/json_schema.rs b/src/json_schema.rs index 2af76b38..74ec21f8 100644 --- a/src/json_schema.rs +++ b/src/json_schema.rs @@ -1,6 +1,7 @@ use failure::*; use std::collections::HashMap; use serde_json::{json, Value}; +use url::form_urlencoded; 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> { + + let param_list: Vec<(String, String)> = + form_urlencoded::parse(query.as_bytes()).into_owned().collect(); + + parse_parameter_strings(¶m_list, schema) +} + #[test] fn test_shema1() { let schema = Jss::Object(JssObject { description: "TEST", - optional: None, - additional_properties: None, + optional: false, + additional_properties: false, properties: { let map = HashMap::new(); - Box::new(map) + map } }); 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] fn test_shema1() { diff --git a/src/main.rs b/src/main.rs index 04e4014a..98cecbea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use apitest::json_schema::*; //use serde_derive::{Serialize, Deserialize}; use serde_json::{json, Value}; -use url::form_urlencoded; use hyper::{Method, Body, Request, Response, Server, StatusCode}; use hyper::rt::Future; @@ -65,10 +64,7 @@ fn handle_request(req: Request) -> Response { // extract param let param = match query { Some(data) => { - let param_list: Vec<(String, String)> = - form_urlencoded::parse(data.as_bytes()).into_owned().collect(); - - match parse_parameter_strings(¶m_list, &api_method.parameters) { + match parse_query_string(data, &api_method.parameters) { Ok(query) => query, Err(ref error_list) => { let mut msg = String::from("");