aboutsummaryrefslogtreecommitdiffstats
path: root/components/hyper_serde/tests/tokens.rs
blob: d7ad09f4e9c9a7e6fda5d857788644f5e6a9552e (plain) (blame)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2023 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use cookie::Cookie;
use headers::ContentType;
use http::header::{self, HeaderMap, HeaderValue};
use http::StatusCode;
use hyper::{Method, Uri};
use hyper_serde::{De, Ser};
use serde_test::{assert_de_tokens, assert_ser_tokens, Token};
use time::Duration;

#[test]
fn test_content_type() {
    let content_type = ContentType::from("Application/Json".parse::<mime::Mime>().unwrap());
    let tokens = &[Token::Str("application/json")];

    assert_ser_tokens(&Ser::new(&content_type), tokens);
    assert_de_tokens(&De::new(content_type), tokens);
}

#[test]
fn test_cookie() {
    // Unfortunately we have to do the to_string().parse() dance here to avoid the object being a
    // string with a bunch of indices in it which apparently is different from the exact same
    // cookie but parsed as a bunch of strings.
    let cookie: Cookie = Cookie::build("Hello", "World!")
        .max_age(Duration::seconds(42))
        .domain("servo.org")
        .path("/")
        .secure(true)
        .http_only(false)
        .finish()
        .to_string()
        .parse()
        .unwrap();

    let tokens = &[Token::Str(
        "Hello=World!; Secure; Path=/; Domain=servo.org; Max-Age=42",
    )];

    assert_ser_tokens(&Ser::new(&cookie), tokens);
    assert_de_tokens(&De::new(cookie), tokens);
}

#[test]
fn test_headers_empty() {
    let headers = HeaderMap::new();

    let tokens = &[Token::Map { len: Some(0) }, Token::MapEnd];

    assert_ser_tokens(&Ser::new(&headers), tokens);
    assert_de_tokens(&De::new(headers), tokens);
}

#[test]
fn test_headers_not_empty() {
    let mut headers = HeaderMap::new();
    headers.insert(header::HOST, HeaderValue::from_static("baguette"));

    // In http, HeaderMap is internally a HashMap and thus testing this
    // with multiple headers is non-deterministic.

    let tokens = &[
        Token::Map { len: Some(1) },
        Token::Str("host"),
        Token::Seq { len: Some(1) },
        Token::Bytes(b"baguette"),
        Token::SeqEnd,
        Token::MapEnd,
    ];

    assert_ser_tokens(&Ser::new(&headers), tokens);
    assert_de_tokens(&De::new(headers.clone()), tokens);

    let pretty = &[
        Token::Map { len: Some(1) },
        Token::Str("host"),
        Token::Seq { len: Some(1) },
        Token::Str("baguette"),
        Token::SeqEnd,
        Token::MapEnd,
    ];

    assert_ser_tokens(&Ser::new_pretty(&headers), pretty);
    assert_de_tokens(&De::new(headers), pretty);
}

#[test]
fn test_method() {
    let method = Method::PUT;
    let tokens = &[Token::Str("PUT")];

    assert_ser_tokens(&Ser::new(&method), tokens);
    assert_de_tokens(&De::new(method), tokens);
}

#[test]
fn test_raw_status() {
    let raw_status = StatusCode::from_u16(200).unwrap();
    let tokens = &[Token::U16(200)];

    assert_ser_tokens(&Ser::new(&raw_status), tokens);
    assert_de_tokens(&De::new(raw_status), tokens);
}

#[test]
fn test_tm() {
    use time::strptime;

    let time = strptime("2017-02-22T12:03:31Z", "%Y-%m-%dT%H:%M:%SZ").unwrap();
    let tokens = &[Token::Str("2017-02-22T12:03:31Z")];

    assert_ser_tokens(&Ser::new(&time), tokens);
    assert_de_tokens(&De::new(time), tokens);
}

#[test]
fn test_uri() {
    use std::str::FromStr;

    // Note that fragment is not serialized / deserialized
    let uri_string = "abc://username:password@example.com:123/path/data?key=value&key2=value2";
    let uri = Uri::from_str(uri_string).unwrap();
    let tokens = &[Token::Str(uri_string)];

    assert_ser_tokens(&Ser::new(&uri), tokens);
    assert_de_tokens(&De::new(uri), tokens);
}