summaryrefslogtreecommitdiff
path: root/tests/test_serialize.rs
blob: 4a9964a491520f7799677a2e9f10e1f7760a8db1 (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
#[macro_use]
extern crate serde_derive;
extern crate serde_qs as qs;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Address {
    city: String,
    postcode: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct QueryParams {
    id: u8,
    name: String,
    phone: u32,
    address: Address,
    user_ids: Vec<u8>,
}


#[test]
fn serialize_struct() {
    let params = QueryParams {
        id: 42,
        name: "Acme".to_string(),
        phone: 12345,
        address: Address {
            city: "Carrot City".to_string(),
            postcode: "12345".to_string(),
        },
        user_ids: vec![1, 2, 3, 4],
    };

    assert_eq!(qs::to_string(&params).unwrap(),
               urlencode("\
        id=42&name=Acme&phone=12345&address[city]=Carrot+City&\
        address[postcode]=12345&user_ids[0]=1&user_ids[1]=2&\
        user_ids[2]=3&user_ids[3]=4"));
}

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