1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 10:42:55 +01:00

docs: better docs for peer_addr methods

This commit is contained in:
Rob Ede 2024-01-08 15:17:36 +00:00
parent d2bd549eec
commit ac04d80d8e
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
5 changed files with 60 additions and 45 deletions

View File

@ -16,7 +16,10 @@ pub struct RequestHead {
pub uri: Uri, pub uri: Uri,
pub version: Version, pub version: Version,
pub headers: HeaderMap, pub headers: HeaderMap,
/// Will only be None when called in unit tests unless [`TestRequest::peer_addr`] is used.
pub peer_addr: Option<net::SocketAddr>, pub peer_addr: Option<net::SocketAddr>,
flags: Flags, flags: Flags,
} }

View File

@ -173,7 +173,7 @@ impl<P> Request<P> {
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of /// Peer address is the directly connected peer's socket address. If a proxy is used in front of
/// the Actix Web server, then it would be address of this proxy. /// the Actix Web server, then it would be address of this proxy.
/// ///
/// Will only return None when called in unit tests. /// Will only return None when called in unit tests unless set manually.
#[inline] #[inline]
pub fn peer_addr(&self) -> Option<net::SocketAddr> { pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr self.head().peer_addr

View File

@ -221,12 +221,9 @@ impl ServiceRequest {
/// Returns peer's socket address. /// Returns peer's socket address.
/// ///
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of /// See [`HttpRequest::peer_addr`] for more details.
/// the Actix Web server, then it would be address of this proxy.
/// ///
/// To get client connection information `ConnectionInfo` should be used. /// [`HttpRequest::peer_addr`]: crate::HttpRequest::peer_addr
///
/// Will only return None when called in unit tests.
#[inline] #[inline]
pub fn peer_addr(&self) -> Option<net::SocketAddr> { pub fn peer_addr(&self) -> Option<net::SocketAddr> {
self.head().peer_addr self.head().peer_addr

View File

@ -86,76 +86,77 @@ impl Default for TestRequest {
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
impl TestRequest { impl TestRequest {
/// Create TestRequest and set request uri /// Constructs test request and sets request URI.
pub fn with_uri(path: &str) -> TestRequest { pub fn with_uri(uri: &str) -> TestRequest {
TestRequest::default().uri(path) TestRequest::default().uri(uri)
} }
/// Create TestRequest and set method to `Method::GET` /// Constructs test request with GET method.
pub fn get() -> TestRequest { pub fn get() -> TestRequest {
TestRequest::default().method(Method::GET) TestRequest::default().method(Method::GET)
} }
/// Create TestRequest and set method to `Method::POST` /// Constructs test request with POST method.
pub fn post() -> TestRequest { pub fn post() -> TestRequest {
TestRequest::default().method(Method::POST) TestRequest::default().method(Method::POST)
} }
/// Create TestRequest and set method to `Method::PUT` /// Constructs test request with PUT method.
pub fn put() -> TestRequest { pub fn put() -> TestRequest {
TestRequest::default().method(Method::PUT) TestRequest::default().method(Method::PUT)
} }
/// Create TestRequest and set method to `Method::PATCH` /// Constructs test request with PATCH method.
pub fn patch() -> TestRequest { pub fn patch() -> TestRequest {
TestRequest::default().method(Method::PATCH) TestRequest::default().method(Method::PATCH)
} }
/// Create TestRequest and set method to `Method::DELETE` /// Constructs test request with DELETE method.
pub fn delete() -> TestRequest { pub fn delete() -> TestRequest {
TestRequest::default().method(Method::DELETE) TestRequest::default().method(Method::DELETE)
} }
/// Set HTTP version of this request /// Sets HTTP version of this request.
pub fn version(mut self, ver: Version) -> Self { pub fn version(mut self, ver: Version) -> Self {
self.req.version(ver); self.req.version(ver);
self self
} }
/// Set HTTP method of this request /// Sets method of this request.
pub fn method(mut self, meth: Method) -> Self { pub fn method(mut self, meth: Method) -> Self {
self.req.method(meth); self.req.method(meth);
self self
} }
/// Set HTTP URI of this request /// Sets URI of this request.
pub fn uri(mut self, path: &str) -> Self { pub fn uri(mut self, path: &str) -> Self {
self.req.uri(path); self.req.uri(path);
self self
} }
/// Insert a header, replacing any that were set with an equivalent field name. /// Inserts a header, replacing any that were set with an equivalent field name.
pub fn insert_header(mut self, header: impl TryIntoHeaderPair) -> Self { pub fn insert_header(mut self, header: impl TryIntoHeaderPair) -> Self {
self.req.insert_header(header); self.req.insert_header(header);
self self
} }
/// Append a header, keeping any that were set with an equivalent field name. /// Appends a header, keeping any that were set with an equivalent field name.
pub fn append_header(mut self, header: impl TryIntoHeaderPair) -> Self { pub fn append_header(mut self, header: impl TryIntoHeaderPair) -> Self {
self.req.append_header(header); self.req.append_header(header);
self self
} }
/// Set cookie for this request. /// Sets cookie for this request.
#[cfg(feature = "cookies")] #[cfg(feature = "cookies")]
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self { pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
self.cookies.add(cookie.into_owned()); self.cookies.add(cookie.into_owned());
self self
} }
/// Set request path pattern parameter. /// Sets request path pattern parameter.
/// ///
/// # Examples /// # Examples
///
/// ``` /// ```
/// use actix_web::test::TestRequest; /// use actix_web::test::TestRequest;
/// ///
@ -171,19 +172,19 @@ impl TestRequest {
self self
} }
/// Set peer addr. /// Sets peer address.
pub fn peer_addr(mut self, addr: SocketAddr) -> Self { pub fn peer_addr(mut self, addr: SocketAddr) -> Self {
self.peer_addr = Some(addr); self.peer_addr = Some(addr);
self self
} }
/// Set request payload. /// Sets request payload.
pub fn set_payload(mut self, data: impl Into<Bytes>) -> Self { pub fn set_payload(mut self, data: impl Into<Bytes>) -> Self {
self.req.set_payload(data); self.req.set_payload(data);
self self
} }
/// Serialize `data` to a URL encoded form and set it as the request payload. /// Serializes `data` to a URL encoded form and set it as the request payload.
/// ///
/// The `Content-Type` header is set to `application/x-www-form-urlencoded`. /// The `Content-Type` header is set to `application/x-www-form-urlencoded`.
pub fn set_form(mut self, data: impl Serialize) -> Self { pub fn set_form(mut self, data: impl Serialize) -> Self {
@ -194,7 +195,7 @@ impl TestRequest {
self self
} }
/// Serialize `data` to JSON and set it as the request payload. /// Serializes `data` to JSON and set it as the request payload.
/// ///
/// The `Content-Type` header is set to `application/json`. /// The `Content-Type` header is set to `application/json`.
pub fn set_json(mut self, data: impl Serialize) -> Self { pub fn set_json(mut self, data: impl Serialize) -> Self {
@ -204,27 +205,33 @@ impl TestRequest {
self self
} }
/// Set application data. This is equivalent of `App::data()` method /// Inserts application data.
/// for testing purpose. ///
pub fn data<T: 'static>(mut self, data: T) -> Self { /// This is equivalent of `App::app_data()` method for testing purpose.
self.app_data.insert(Data::new(data));
self
}
/// Set application data. This is equivalent of `App::app_data()` method
/// for testing purpose.
pub fn app_data<T: 'static>(mut self, data: T) -> Self { pub fn app_data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(data); self.app_data.insert(data);
self self
} }
/// Inserts application data.
///
/// This is equivalent of `App::data()` method for testing purpose.
#[doc(hidden)]
pub fn data<T: 'static>(mut self, data: T) -> Self {
self.app_data.insert(Data::new(data));
self
}
/// Sets resource map.
#[cfg(test)] #[cfg(test)]
/// Set request config
pub(crate) fn rmap(mut self, rmap: ResourceMap) -> Self { pub(crate) fn rmap(mut self, rmap: ResourceMap) -> Self {
self.rmap = rmap; self.rmap = rmap;
self self
} }
/// Finalizes test request.
///
/// This request builder will be useless after calling `finish()`.
fn finish(&mut self) -> Request { fn finish(&mut self) -> Request {
// mut used when cookie feature is enabled // mut used when cookie feature is enabled
#[allow(unused_mut)] #[allow(unused_mut)]
@ -251,14 +258,14 @@ impl TestRequest {
req req
} }
/// Complete request creation and generate `Request` instance /// Finalizes request creation and returns `Request` instance.
pub fn to_request(mut self) -> Request { pub fn to_request(mut self) -> Request {
let mut req = self.finish(); let mut req = self.finish();
req.head_mut().peer_addr = self.peer_addr; req.head_mut().peer_addr = self.peer_addr;
req req
} }
/// Complete request creation and generate `ServiceRequest` instance /// Finalizes request creation and returns `ServiceRequest` instance.
pub fn to_srv_request(mut self) -> ServiceRequest { pub fn to_srv_request(mut self) -> ServiceRequest {
let (mut head, payload) = self.finish().into_parts(); let (mut head, payload) = self.finish().into_parts();
head.peer_addr = self.peer_addr; head.peer_addr = self.peer_addr;
@ -279,12 +286,12 @@ impl TestRequest {
) )
} }
/// Complete request creation and generate `ServiceResponse` instance /// Finalizes request creation and returns `ServiceResponse` instance.
pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B> { pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B> {
self.to_srv_request().into_response(res) self.to_srv_request().into_response(res)
} }
/// Complete request creation and generate `HttpRequest` instance /// Finalizes request creation and returns `HttpRequest` instance.
pub fn to_http_request(mut self) -> HttpRequest { pub fn to_http_request(mut self) -> HttpRequest {
let (mut head, _) = self.finish().into_parts(); let (mut head, _) = self.finish().into_parts();
head.peer_addr = self.peer_addr; head.peer_addr = self.peer_addr;
@ -302,7 +309,7 @@ impl TestRequest {
) )
} }
/// Complete request creation and generate `HttpRequest` and `Payload` instances /// Finalizes request creation and returns `HttpRequest` and `Payload` pair.
pub fn to_http_parts(mut self) -> (HttpRequest, Payload) { pub fn to_http_parts(mut self) -> (HttpRequest, Payload) {
let (mut head, payload) = self.finish().into_parts(); let (mut head, payload) = self.finish().into_parts();
head.peer_addr = self.peer_addr; head.peer_addr = self.peer_addr;
@ -322,7 +329,7 @@ impl TestRequest {
(req, payload) (req, payload)
} }
/// Complete request creation, calls service and waits for response future completion. /// Finalizes request creation, calls service, and waits for response future completion.
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response pub async fn send_request<S, B, E>(self, app: &S) -> S::Response
where where
S: Service<Request, Response = ServiceResponse<B>, Error = E>, S: Service<Request, Response = ServiceResponse<B>, Error = E>,

View File

@ -112,17 +112,25 @@ echo
read -p "Update all references: (y/N) " UPDATE_REFERENCES read -p "Update all references: (y/N) " UPDATE_REFERENCES
UPDATE_REFERENCES="${UPDATE_REFERENCES:-n}" UPDATE_REFERENCES="${UPDATE_REFERENCES:-n}"
if [ "$UPDATE_REFERENCES" = 'y' ] || [ "$UPDATE_REFERENCES" = 'Y' ]; then if [ "$UPDATE_REFERENCES" = 'y' ] || [ "$UPDATE_REFERENCES" = 'Y' ]; then
if [[ $NEW_VERSION == *".0.0" ]]; then
NEW_VERSION_SPEC="${NEW_VERSION%.0.0}"
elif [[ $NEW_VERSION == *".0" ]]; then
NEW_VERSION_SPEC="${NEW_VERSION%.0}"
else
NEW_VERSION_SPEC="$NEW_VERSION"
fi
for f in $(fd Cargo.toml); do for f in $(fd Cargo.toml); do
sed -i.bak -E \ sed -i.bak -E \
"s/^(${PACKAGE_NAME} ?= ?\")[^\"]+(\")$/\1${NEW_VERSION}\2/g" $f "s/^(${PACKAGE_NAME} ?= ?\")[^\"]+(\")$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \ sed -i.bak -E \
"s/^(${PACKAGE_NAME} ?=.*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION}\2/g" $f "s/^(${PACKAGE_NAME} ?=.*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \ sed -i.bak -E \
"s/^(.*package ?= ?\"${PACKAGE_NAME}\".*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION}\2/g" $f "s/^(.*package ?= ?\"${PACKAGE_NAME}\".*version ?= ?\")[^\"]+(\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
sed -i.bak -E \ sed -i.bak -E \
"s/^(.*version ?= ?\")[^\"]+(\".*package ?= ?\"${PACKAGE_NAME}\".*)$/\1${NEW_VERSION}\2/g" $f "s/^(.*version ?= ?\")[^\"]+(\".*package ?= ?\"${PACKAGE_NAME}\".*)$/\1${NEW_VERSION_SPEC}\2/g" $f
# remove backup file # remove backup file
rm -f $f.bak rm -f $f.bak