headers_ext/common/
expect.rs

1use std::fmt;
2
3use util::IterExt;
4
5/// The `Expect` header.
6///
7/// > The "Expect" header field in a request indicates a certain set of
8/// > behaviors (expectations) that need to be supported by the server in
9/// > order to properly handle this request.  The only such expectation
10/// > defined by this specification is 100-continue.
11/// >
12/// >    Expect  = "100-continue"
13///
14/// # Example
15///
16/// ```
17/// # extern crate headers_ext as headers;
18/// use headers::Expect;
19///
20/// let expect = Expect::CONTINUE;
21/// ```
22#[derive(Clone, PartialEq)]
23pub struct Expect(());
24
25impl Expect {
26    /// "100-continue"
27    pub const CONTINUE: Expect = Expect(());
28}
29
30impl ::Header for Expect {
31    const NAME: &'static ::HeaderName = &::http::header::EXPECT;
32
33    fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
34        values
35            .just_one()
36            .and_then(|value| {
37                if value == "100-continue" {
38                    Some(Expect::CONTINUE)
39                } else {
40                    None
41                }
42            })
43            .ok_or_else(::Error::invalid)
44    }
45
46    fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
47        values.extend(::std::iter::once(::HeaderValue::from_static("100-continue")));
48    }
49}
50
51impl fmt::Debug for Expect {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        f.debug_tuple("Expect")
54            .field(&"100-continue")
55            .finish()
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::Expect;
62    use super::super::test_decode;
63
64    #[test]
65    fn expect_continue() {
66        assert_eq!(
67            test_decode::<Expect>(&["100-continue"]),
68            Some(Expect::CONTINUE),
69        );
70    }
71
72    #[test]
73    fn expectation_failed() {
74        assert_eq!(
75            test_decode::<Expect>(&["sandwich"]),
76            None,
77        );
78    }
79
80    #[test]
81    fn too_many_values() {
82        assert_eq!(
83            test_decode::<Expect>(&["100-continue", "100-continue"]),
84            None,
85        );
86    }
87}