mirror of
https://github.com/actix/actix-web.git
synced 2024-12-20 15:17:07 +00:00
Allow to set peer_addr for TestRequest #1074
This commit is contained in:
parent
a08d8dab70
commit
20248daeda
3 changed files with 28 additions and 5 deletions
|
@ -6,6 +6,8 @@
|
|||
|
||||
* Move `BodyEncoding` to `dev` module #1220
|
||||
|
||||
* Allow to set `peer_addr` for TestRequest #1074
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fix `AppConfig::secure()` is always false. #1202
|
||||
|
|
27
src/test.rs
27
src/test.rs
|
@ -1,5 +1,6 @@
|
|||
//! Various helpers for Actix applications to use during testing.
|
||||
use std::convert::TryFrom;
|
||||
use std::net::SocketAddr;
|
||||
use std::rc::Rc;
|
||||
use std::sync::mpsc;
|
||||
use std::{fmt, net, thread, time};
|
||||
|
@ -300,6 +301,7 @@ pub struct TestRequest {
|
|||
rmap: ResourceMap,
|
||||
config: AppConfig,
|
||||
path: Path<Url>,
|
||||
peer_addr: Option<SocketAddr>,
|
||||
app_data: Extensions,
|
||||
}
|
||||
|
||||
|
@ -310,6 +312,7 @@ impl Default for TestRequest {
|
|||
rmap: ResourceMap::new(ResourceDef::new("")),
|
||||
config: AppConfig::default(),
|
||||
path: Path::new(Url::new(Uri::default())),
|
||||
peer_addr: None,
|
||||
app_data: Extensions::new(),
|
||||
}
|
||||
}
|
||||
|
@ -409,6 +412,12 @@ impl TestRequest {
|
|||
self
|
||||
}
|
||||
|
||||
/// Set peer addr
|
||||
pub fn peer_addr(mut self, addr: SocketAddr) -> Self {
|
||||
self.peer_addr = Some(addr);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set request payload
|
||||
pub fn set_payload<B: Into<Bytes>>(mut self, data: B) -> Self {
|
||||
self.req.set_payload(data);
|
||||
|
@ -451,12 +460,15 @@ impl TestRequest {
|
|||
|
||||
/// Complete request creation and generate `Request` instance
|
||||
pub fn to_request(mut self) -> Request {
|
||||
self.req.finish()
|
||||
let mut req = self.req.finish();
|
||||
req.head_mut().peer_addr = self.peer_addr;
|
||||
req
|
||||
}
|
||||
|
||||
/// Complete request creation and generate `ServiceRequest` instance
|
||||
pub fn to_srv_request(mut self) -> ServiceRequest {
|
||||
let (head, payload) = self.req.finish().into_parts();
|
||||
let (mut head, payload) = self.req.finish().into_parts();
|
||||
head.peer_addr = self.peer_addr;
|
||||
self.path.get_mut().update(&head.uri);
|
||||
|
||||
ServiceRequest::new(HttpRequest::new(
|
||||
|
@ -477,7 +489,8 @@ impl TestRequest {
|
|||
|
||||
/// Complete request creation and generate `HttpRequest` instance
|
||||
pub fn to_http_request(mut self) -> HttpRequest {
|
||||
let (head, payload) = self.req.finish().into_parts();
|
||||
let (mut head, payload) = self.req.finish().into_parts();
|
||||
head.peer_addr = self.peer_addr;
|
||||
self.path.get_mut().update(&head.uri);
|
||||
|
||||
HttpRequest::new(
|
||||
|
@ -493,7 +506,8 @@ impl TestRequest {
|
|||
|
||||
/// Complete request creation and generate `HttpRequest` and `Payload` instances
|
||||
pub fn to_http_parts(mut self) -> (HttpRequest, Payload) {
|
||||
let (head, payload) = self.req.finish().into_parts();
|
||||
let (mut head, payload) = self.req.finish().into_parts();
|
||||
head.peer_addr = self.peer_addr;
|
||||
self.path.get_mut().update(&head.uri);
|
||||
|
||||
let req = HttpRequest::new(
|
||||
|
@ -950,9 +964,14 @@ mod tests {
|
|||
.set(header::Date(SystemTime::now().into()))
|
||||
.param("test", "123")
|
||||
.data(10u32)
|
||||
.peer_addr("127.0.0.1:8081".parse().unwrap())
|
||||
.to_http_request();
|
||||
assert!(req.headers().contains_key(header::CONTENT_TYPE));
|
||||
assert!(req.headers().contains_key(header::DATE));
|
||||
assert_eq!(
|
||||
req.head().peer_addr,
|
||||
Some("127.0.0.1:8081".parse().unwrap())
|
||||
);
|
||||
assert_eq!(&req.match_info()["test"], "123");
|
||||
assert_eq!(req.version(), Version::HTTP_2);
|
||||
let data = req.get_app_data::<u32>().unwrap();
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::{net, thread, time::Duration};
|
|||
#[cfg(feature = "openssl")]
|
||||
use open_ssl::ssl::SslAcceptorBuilder;
|
||||
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
fn unused_addr() -> net::SocketAddr {
|
||||
let addr: net::SocketAddr = "127.0.0.1:0".parse().unwrap();
|
||||
|
@ -90,6 +90,8 @@ fn ssl_acceptor() -> std::io::Result<SslAcceptorBuilder> {
|
|||
#[actix_rt::test]
|
||||
#[cfg(feature = "openssl")]
|
||||
async fn test_start_ssl() {
|
||||
use actix_web::HttpRequest;
|
||||
|
||||
let addr = unused_addr();
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
|
|
Loading…
Reference in a new issue