try to write unit test
This commit is contained in:
parent
cfa5886c33
commit
b8bcc7630c
|
@ -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<Value, Vec<Error>> {
|
||||
|
||||
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() {
|
||||
|
|
|
@ -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<Body>) -> Response<Body> {
|
|||
// 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("");
|
||||
|
|
Loading…
Reference in New Issue