summaryrefslogtreecommitdiff
path: root/tests/test_deserialize.rs
blob: 776d562521b24262b0e4ef8ef662a014132244a3 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#[macro_use]
extern crate serde_derive;
extern crate serde_urlencoded;

#[test]
fn deserialize_bytes() {
    let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];

    assert_eq!(serde_urlencoded::from_bytes(b"first=23&last=42"),
               Ok(result));
}

#[test]
fn deserialize_str() {
    let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];

    assert_eq!(serde_urlencoded::from_str("first=23&last=42"),
               Ok(result));
}

#[test]
fn deserialize_reader() {
    let result = vec![("first".to_owned(), 23), ("last".to_owned(), 42)];

    assert_eq!(serde_urlencoded::from_reader(b"first=23&last=42" as &[_]),
               Ok(result));
}


#[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 = serde_urlencoded::from_str(&urlencode(input)).unwrap();
    assert_eq!(result, params);
    let result: A = serde_urlencoded::from_str(&input).unwrap();
    assert_eq!(result, params);
    let result: A = serde_urlencoded::from_str(&urlencode(input2)).unwrap();
    assert_eq!(result, params);
    let result: A = serde_urlencoded::from_str(&input2).unwrap();
    assert_eq!(result, params);

}

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