diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/headers/src/common/expect.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/headers/src/common/expect.rs')
-rw-r--r-- | third_party/rust/headers/src/common/expect.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/third_party/rust/headers/src/common/expect.rs b/third_party/rust/headers/src/common/expect.rs new file mode 100644 index 0000000000..a1caf2530d --- /dev/null +++ b/third_party/rust/headers/src/common/expect.rs @@ -0,0 +1,86 @@ +use std::fmt; + +use util::IterExt; + +/// The `Expect` header. +/// +/// > The "Expect" header field in a request indicates a certain set of +/// > behaviors (expectations) that need to be supported by the server in +/// > order to properly handle this request. The only such expectation +/// > defined by this specification is 100-continue. +/// > +/// > Expect = "100-continue" +/// +/// # Example +/// +/// ``` +/// # extern crate headers; +/// use headers::Expect; +/// +/// let expect = Expect::CONTINUE; +/// ``` +#[derive(Clone, PartialEq)] +pub struct Expect(()); + +impl Expect { + /// "100-continue" + pub const CONTINUE: Expect = Expect(()); +} + +impl ::Header for Expect { + fn name() -> &'static ::HeaderName { + &::http::header::EXPECT + } + + fn decode<'i, I: Iterator<Item = &'i ::HeaderValue>>(values: &mut I) -> Result<Self, ::Error> { + values + .just_one() + .and_then(|value| { + if value == "100-continue" { + Some(Expect::CONTINUE) + } else { + None + } + }) + .ok_or_else(::Error::invalid) + } + + fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { + values.extend(::std::iter::once(::HeaderValue::from_static( + "100-continue", + ))); + } +} + +impl fmt::Debug for Expect { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Expect").field(&"100-continue").finish() + } +} + +#[cfg(test)] +mod tests { + use super::super::test_decode; + use super::Expect; + + #[test] + fn expect_continue() { + assert_eq!( + test_decode::<Expect>(&["100-continue"]), + Some(Expect::CONTINUE), + ); + } + + #[test] + fn expectation_failed() { + assert_eq!(test_decode::<Expect>(&["sandwich"]), None,); + } + + #[test] + fn too_many_values() { + assert_eq!( + test_decode::<Expect>(&["100-continue", "100-continue"]), + None, + ); + } +} |