1 use std::fmt;
2
3 use 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;
18 /// use headers::Expect;
19 ///
20 /// let expect = Expect::CONTINUE;
21 /// ```
22 #[derive(Clone, PartialEq)]
23 pub struct Expect(());
24
25 impl Expect {
26 /// "100-continue"
27 pub const CONTINUE: Expect = Expect(());
28 }
29
30 impl ::Header for Expect {
31 fn name() -> &'static ::HeaderName {
32 &::http::header::EXPECT
33 }
34
35 fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> {
36 values
37 .just_one()
38 .and_then(|value| {
39 if value == "100-continue" {
40 Some(Expect::CONTINUE)
41 } else {
42 None
43 }
44 })
45 .ok_or_else(::Error::invalid)
46 }
47
48 fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) {
49 values.extend(::std::iter::once(::HeaderValue::from_static(
50 "100-continue",
51 )));
52 }
main()53 }
54
55 impl fmt::Debug for Expect {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 f.debug_tuple("Expect").field(&"100-continue").finish()
58 }
59 }
60
61 #[cfg(test)]
62 mod tests {
63 use super::super::test_decode;
64 use super::Expect;
65
66 #[test]
67 fn expect_continue() {
68 assert_eq!(
69 test_decode::<Expect>(&["100-continue"]),
70 Some(Expect::CONTINUE),
71 );
72 }
73
74 #[test]
75 fn expectation_failed() {
76 assert_eq!(test_decode::<Expect>(&["sandwich"]), None,);
77 }
78
79 #[test]
80 fn too_many_values() {
81 assert_eq!(
82 test_decode::<Expect>(&["100-continue", "100-continue"]),
83 None,
84 );
85 }
86 }
87