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

#[derive(Serialize, Deserialize)]
struct Foo { bar: Bar, baz: Baz }
#[derive(Serialize, Deserialize)]
struct Bar { x: u8, y: String }
#[derive(Serialize, Deserialize)]
struct Baz { thing: String, other: u8 }

#[test]
fn serialize_struct() {
    let params = Foo {
      bar: Bar {
        x: 10,
        y: "Ten".to_owned()
      },
      baz: Baz {
        thing: "Thing".to_owned(),
        other: 12
      }
    };

    assert_eq!(qs::to_string(&params),
      Ok(urlencode("bar[x]=10&bar[y]=Ten&baz[thing]=Thing&baz[other]=12")));
}

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