|
| 1 | +//! Adapted version of `headers::IfNoneMatch`. |
| 2 | +//! |
| 3 | +//! The combination of `TypedHeader` and `IfNoneMatch` works in odd ways. |
| 4 | +//! They are built in a way that a _missing_ `If-None-Match` header will lead to: |
| 5 | +//! |
| 6 | +//! 1. extractor with `TypedHeader<IfNoneMatch>` returning `IfNoneMatch("")` |
| 7 | +//! 2. extractor with `Option<TypedHeader<IfNoneMatch>>` returning `Some(IfNoneMatch(""))` |
| 8 | +//! |
| 9 | +//! Where I would expect: |
| 10 | +//! 1. a failure because of the missing header |
| 11 | +//! 2. `None` for the missing header |
| 12 | +//! |
| 13 | +//! This could be solved by either adapting `TypedHeader` or `IfNoneMatch`, I'm not sure which is |
| 14 | +//! right. |
| 15 | +//! |
| 16 | +//! Some reading material for those interested: |
| 17 | +//! * https://github.com/hyperium/headers/issues/204 |
| 18 | +//! * https://github.com/hyperium/headers/pull/165 |
| 19 | +//! * https://github.com/tokio-rs/axum/issues/1781 |
| 20 | +//! * https://github.com/tokio-rs/axum/pull/1810 |
| 21 | +//! * https://github.com/tokio-rs/axum/pull/2475 |
| 22 | +//! |
| 23 | +//! Right now I feel like adapting `IfNoneMatch` is the "most correct-ish" option. |
| 24 | +
|
| 25 | +#[allow(clippy::disallowed_types)] |
| 26 | +mod header_impl { |
| 27 | + use axum_extra::headers::{self, ETag, Header, IfNoneMatch as OriginalIfNoneMatch}; |
| 28 | + use derive_more::Deref; |
| 29 | + |
| 30 | + #[derive(Debug, Clone, PartialEq, Deref)] |
| 31 | + pub(crate) struct IfNoneMatch(pub axum_extra::headers::IfNoneMatch); |
| 32 | + |
| 33 | + impl Header for IfNoneMatch { |
| 34 | + fn name() -> &'static http::HeaderName { |
| 35 | + OriginalIfNoneMatch::name() |
| 36 | + } |
| 37 | + |
| 38 | + fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error> |
| 39 | + where |
| 40 | + Self: Sized, |
| 41 | + I: Iterator<Item = &'i http::HeaderValue>, |
| 42 | + { |
| 43 | + let mut values = values.peekable(); |
| 44 | + |
| 45 | + // NOTE: this is the difference to the original implementation. |
| 46 | + // When there is no header in the request, I want the decoding to fail. |
| 47 | + // This makes Option<TypedHeader<H>> return `None`, and also matches |
| 48 | + // most other header implementations. |
| 49 | + if values.peek().is_none() { |
| 50 | + Err(headers::Error::invalid()) |
| 51 | + } else { |
| 52 | + OriginalIfNoneMatch::decode(&mut values).map(IfNoneMatch) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn encode<E: Extend<http::HeaderValue>>(&self, values: &mut E) { |
| 57 | + self.0.encode(values) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + impl From<ETag> for IfNoneMatch { |
| 62 | + fn from(value: ETag) -> Self { |
| 63 | + Self(value.into()) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub(crate) use header_impl::IfNoneMatch; |
| 69 | + |
| 70 | +#[cfg(test)] |
| 71 | +mod tests { |
| 72 | + use super::*; |
| 73 | + use anyhow::Result; |
| 74 | + use axum::{RequestPartsExt, body::Body, extract::Request}; |
| 75 | + use axum_extra::{ |
| 76 | + TypedHeader, |
| 77 | + headers::{ETag, HeaderMapExt as _}, |
| 78 | + }; |
| 79 | + use http::{HeaderMap, request}; |
| 80 | + |
| 81 | + fn parts(if_none_match: Option<IfNoneMatch>) -> request::Parts { |
| 82 | + let mut builder = Request::builder(); |
| 83 | + |
| 84 | + if let Some(if_none_match) = if_none_match { |
| 85 | + let headers = builder.headers_mut().unwrap(); |
| 86 | + headers.typed_insert(if_none_match.clone()); |
| 87 | + } |
| 88 | + |
| 89 | + let (parts, _body) = builder.uri("/").body(Body::empty()).unwrap().into_parts(); |
| 90 | + |
| 91 | + parts |
| 92 | + } |
| 93 | + |
| 94 | + fn example_header() -> IfNoneMatch { |
| 95 | + IfNoneMatch::from("\"some-etag-value\"".parse::<ETag>().unwrap()) |
| 96 | + } |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_normal_typed_get_with_empty_headers() { |
| 100 | + let map = HeaderMap::new(); |
| 101 | + assert!(map.typed_get::<IfNoneMatch>().is_none()); |
| 102 | + assert!(map.typed_try_get::<IfNoneMatch>().unwrap().is_none()); |
| 103 | + } |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn test_normal_typed_get_with_value_headers() -> Result<()> { |
| 107 | + let if_none_match = example_header(); |
| 108 | + |
| 109 | + let mut map = HeaderMap::new(); |
| 110 | + map.typed_insert(if_none_match.clone()); |
| 111 | + |
| 112 | + assert_eq!(map.typed_get::<IfNoneMatch>(), Some(if_none_match.clone())); |
| 113 | + assert_eq!(map.typed_try_get::<IfNoneMatch>()?, Some(if_none_match)); |
| 114 | + |
| 115 | + Ok(()) |
| 116 | + } |
| 117 | + |
| 118 | + #[tokio::test] |
| 119 | + async fn test_extract_from_empty_request_via_optional_typed_header() -> Result<()> { |
| 120 | + let mut parts = parts(None); |
| 121 | + |
| 122 | + assert!( |
| 123 | + parts |
| 124 | + .extract::<Option<TypedHeader<IfNoneMatch>>>() |
| 125 | + .await? |
| 126 | + // this is what we want, and the default `headers::IfNoneMatch` header can't |
| 127 | + // offer. Or the impl of the `TypedHeader` extractor, depending on |
| 128 | + // interpretation. |
| 129 | + .is_none() |
| 130 | + ); |
| 131 | + |
| 132 | + Ok(()) |
| 133 | + } |
| 134 | + |
| 135 | + #[tokio::test] |
| 136 | + async fn test_extract_from_empty_request_via_mandatory_typed_header() -> Result<()> { |
| 137 | + let mut parts = parts(None); |
| 138 | + |
| 139 | + // mandatory extractor leads to error when the header is missing. |
| 140 | + assert!(parts.extract::<TypedHeader<IfNoneMatch>>().await.is_err()); |
| 141 | + |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | + |
| 145 | + #[tokio::test] |
| 146 | + async fn test_extract_from_header_via_optional_typed_header() -> Result<()> { |
| 147 | + let if_none_match = example_header(); |
| 148 | + let mut parts = parts(Some(if_none_match.clone())); |
| 149 | + |
| 150 | + assert_eq!( |
| 151 | + parts |
| 152 | + .extract::<Option<TypedHeader<IfNoneMatch>>>() |
| 153 | + .await? |
| 154 | + .map(|th| th.0), |
| 155 | + Some(if_none_match) |
| 156 | + ); |
| 157 | + |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | + |
| 161 | + #[tokio::test] |
| 162 | + async fn test_extract_from_header_via_mandatory_typed_header() -> Result<()> { |
| 163 | + let if_none_match = example_header(); |
| 164 | + let mut parts = parts(Some(if_none_match.clone())); |
| 165 | + |
| 166 | + assert_eq!( |
| 167 | + parts.extract::<TypedHeader<IfNoneMatch>>().await?.0, |
| 168 | + if_none_match |
| 169 | + ); |
| 170 | + |
| 171 | + Ok(()) |
| 172 | + } |
| 173 | +} |
0 commit comments