typed_headers/impls/
accept.rs

1use http::header::ACCEPT;
2use mime::Mime;
3
4use super::QualityItem;
5
6header! {
7    /// `Accept` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-5.3.2)
8    ///
9    /// The `Accept` header field can be used by user agents to specify
10    /// response media types that are acceptable.  Accept header fields can
11    /// be used to indicate that the request is specifically limited to a
12    /// small set of desired types, as in the case of a request for an
13    /// in-line image
14    ///
15    /// # ABNF
16    ///
17    /// ```text
18    /// Accept = #( media-range [ accept-params ] )
19    ///
20    /// media-range    = ( "*/*"
21    ///                  / ( type "/" "*" )
22    ///                  / ( type "/" subtype )
23    ///                  ) *( OWS ";" OWS parameter )
24    /// accept-params  = weight *( accept-ext )
25    /// accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]
26    /// ```
27    ///
28    /// # Example values
29    /// * `audio/*; q=0.2, audio/basic`
30    /// * `text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c`
31    (Accept, ACCEPT) => (QualityItem<Mime>)*
32}
33
34#[cfg(test)]
35mod test {
36    use crate::{util, Quality, QualityItem};
37
38    use super::*;
39
40    #[test]
41    fn rfc1() {
42        util::test_round_trip(
43            &Accept(vec![
44                QualityItem::new("audio/*".parse().unwrap(), Quality::from_u16(200)),
45                QualityItem::new("audio/basic".parse().unwrap(), Quality::from_u16(1000)),
46            ]),
47            &["audio/*; q=0.2, audio/basic"],
48        );
49    }
50
51    #[test]
52    fn rfc2() {
53        util::test_round_trip(
54            &Accept(vec![
55                QualityItem::new("text/plain".parse().unwrap(), Quality::from_u16(500)),
56                QualityItem::new("text/html".parse().unwrap(), Quality::from_u16(1000)),
57                QualityItem::new("text/x-dvi".parse().unwrap(), Quality::from_u16(800)),
58                QualityItem::new("text/x-c".parse().unwrap(), Quality::from_u16(1000)),
59            ]),
60            &["text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c"],
61        );
62    }
63}