summaryrefslogtreecommitdiff
path: root/tests/test_deserialize.rs
blob: 2e2b8fd92d3c06f84ae08d479b47eaa50ce3ddb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#[macro_use]
extern crate serde_derive;
extern crate serde_qs as qs;


#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct A { b: B, c: C }
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct B { b1: u8, b2: String }
#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct C { c1: String, c2: u8 }

#[test]
fn deserialize_struct() {
    let params = A {
      b: B {
        b1: 10,
        b2: "Ten".to_owned()
      },
      c: C {
        c1: "Seven".to_owned(),
        c2: 7
      }
    };
    let input = "b[b1]=10&b[b2]=Ten&c[c1]=Seven&c[c2]=7";
    let input2 = "c[c1]=Seven&b[b2]=Ten&b[b1]=10&c[c2]=7";
    let result: A = qs::from_str(&urlencode(input)).unwrap();
    assert_eq!(result, params);
    let result: A = qs::from_str(&input).unwrap();
    assert_eq!(result, params);
    let result: A = qs::from_str(&urlencode(input2)).unwrap();
    assert_eq!(result, params);
    let result: A = qs::from_str(&input2).unwrap();
    assert_eq!(result, params);

}

fn urlencode(input: &str) -> String {
  str::replace(&str::replace(input, "[", "%5B"), "]", "%5D")
}