Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.todo.md
/target
Cargo.lock
.idea/
46 changes: 44 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub struct ClientIp(pub IpAddr);

/// [`ClientIp`] source configuration
#[non_exhaustive]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum ClientIpSource {
/// IP from the `CF-Connecting-IP` header
Expand Down Expand Up @@ -170,6 +170,23 @@ impl FromStr for ClientIpSource {
}
}

// ensure to update tests::client_ip_source_display_impl_matches_from_str_impl
impl fmt::Display for ClientIpSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ClientIpSource::CfConnectingIp => "CfConnectingIp",
ClientIpSource::CloudFrontViewerAddress => "CloudFrontViewerAddress",
ClientIpSource::ConnectInfo => "ConnectInfo",
ClientIpSource::FlyClientIp => "FlyClientIp",
#[cfg(feature = "forwarded-header")]
ClientIpSource::RightmostForwarded => "RightmostForwarded",
ClientIpSource::RightmostXForwardedFor => "RightmostXForwardedFor",
ClientIpSource::TrueClientIp => "TrueClientIp",
ClientIpSource::XRealIp => "XRealIp",
})
}
}

impl<S> FromRequestParts<S> for ClientIp
where
S: Sync,
Expand Down Expand Up @@ -266,7 +283,9 @@ mod tests {

#[cfg(feature = "forwarded-header")]
use super::RightmostForwarded;
use super::{CfConnectingIp, FlyClientIp, RightmostXForwardedFor, TrueClientIp, XRealIp};
use super::{
CfConnectingIp, ClientIpSource, FlyClientIp, RightmostXForwardedFor, TrueClientIp, XRealIp,
};
use crate::CloudFrontViewerAddress;

const VALID_IPV4: &str = "1.2.3.4";
Expand Down Expand Up @@ -492,4 +511,27 @@ mod tests {
let resp = app().oneshot(req).await.unwrap();
assert_eq!(body_to_string(resp.into_body()).await, VALID_IPV6);
}

#[test]
fn client_ip_source_display_impl_matches_from_str_impl() {
use std::str::FromStr;

#[inline]
fn assert_match(variant: ClientIpSource) {
assert_eq!(
variant,
ClientIpSource::from_str(variant.to_string().as_str()).unwrap()
);
}

assert_match(ClientIpSource::CfConnectingIp);
assert_match(ClientIpSource::CloudFrontViewerAddress);
assert_match(ClientIpSource::ConnectInfo);
assert_match(ClientIpSource::FlyClientIp);
#[cfg(feature = "forwarded-header")]
assert_match(ClientIpSource::RightmostForwarded);
assert_match(ClientIpSource::RightmostXForwardedFor);
assert_match(ClientIpSource::TrueClientIp);
assert_match(ClientIpSource::XRealIp);
}
}