mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-26 23:17:42 +02:00
use actix_rt::test for test setup
This commit is contained in:
@ -6,19 +6,16 @@
|
||||
//! use actix_rt::System;
|
||||
//! use awc::Client;
|
||||
//!
|
||||
//! fn main() {
|
||||
//! System::new("test").block_on(async {
|
||||
//! let mut client = Client::default();
|
||||
//! #[actix_rt::main]
|
||||
//! async fn main() {
|
||||
//! let mut client = Client::default();
|
||||
//!
|
||||
//! client.get("http://www.rust-lang.org") // <- Create request builder
|
||||
//! .header("User-Agent", "Actix-web")
|
||||
//! .send() // <- Send http request
|
||||
//! .await
|
||||
//! .and_then(|response| { // <- server http response
|
||||
//! println!("Response: {:?}", response);
|
||||
//! Ok(())
|
||||
//! })
|
||||
//! });
|
||||
//! let response = client.get("http://www.rust-lang.org") // <- Create request builder
|
||||
//! .header("User-Agent", "Actix-web")
|
||||
//! .send() // <- Send http request
|
||||
//! .await;
|
||||
//!
|
||||
//! println!("Response: {:?}", response);
|
||||
//! }
|
||||
//! ```
|
||||
use std::cell::RefCell;
|
||||
|
@ -39,19 +39,18 @@ const HTTPS_ENCODING: &str = "gzip, deflate";
|
||||
/// ```rust
|
||||
/// use actix_rt::System;
|
||||
///
|
||||
/// fn main() {
|
||||
/// System::new("test").block_on(async {
|
||||
/// let response = awc::Client::new()
|
||||
/// .get("http://www.rust-lang.org") // <- Create request builder
|
||||
/// .header("User-Agent", "Actix-web")
|
||||
/// .send() // <- Send http request
|
||||
/// .await;
|
||||
/// #[actix_rt::main]
|
||||
/// async fn main() {
|
||||
/// let response = awc::Client::new()
|
||||
/// .get("http://www.rust-lang.org") // <- Create request builder
|
||||
/// .header("User-Agent", "Actix-web")
|
||||
/// .send() // <- Send http request
|
||||
/// .await;
|
||||
///
|
||||
/// response.and_then(|response| { // <- server http response
|
||||
/// println!("Response: {:?}", response);
|
||||
/// Ok(())
|
||||
/// })
|
||||
/// });
|
||||
/// response.and_then(|response| { // <- server http response
|
||||
/// println!("Response: {:?}", response);
|
||||
/// Ok(())
|
||||
/// });
|
||||
/// }
|
||||
/// ```
|
||||
pub struct ClientRequest {
|
||||
@ -308,25 +307,21 @@ impl ClientRequest {
|
||||
/// Set a cookie
|
||||
///
|
||||
/// ```rust
|
||||
/// # use actix_rt::System;
|
||||
/// fn main() {
|
||||
/// System::new("test").block_on(async {
|
||||
/// awc::Client::new().get("https://www.rust-lang.org")
|
||||
/// .cookie(
|
||||
/// awc::http::Cookie::build("name", "value")
|
||||
/// .domain("www.rust-lang.org")
|
||||
/// .path("/")
|
||||
/// .secure(true)
|
||||
/// .http_only(true)
|
||||
/// .finish(),
|
||||
/// )
|
||||
/// .send()
|
||||
/// .await
|
||||
/// .and_then(|response| {
|
||||
/// println!("Response: {:?}", response);
|
||||
/// Ok(())
|
||||
/// })
|
||||
/// });
|
||||
/// #[actix_rt::main]
|
||||
/// async fn main() {
|
||||
/// let resp = awc::Client::new().get("https://www.rust-lang.org")
|
||||
/// .cookie(
|
||||
/// awc::http::Cookie::build("name", "value")
|
||||
/// .domain("www.rust-lang.org")
|
||||
/// .path("/")
|
||||
/// .secure(true)
|
||||
/// .http_only(true)
|
||||
/// .finish(),
|
||||
/// )
|
||||
/// .send()
|
||||
/// .await;
|
||||
///
|
||||
/// println!("Response: {:?}", resp);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn cookie(mut self, cookie: Cookie<'_>) -> Self {
|
||||
|
@ -358,41 +358,37 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_http_test::block_on;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{http::header, test::TestResponse};
|
||||
|
||||
#[test]
|
||||
fn test_body() {
|
||||
block_on(async {
|
||||
let mut req =
|
||||
TestResponse::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::UnknownLength => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
#[actix_rt::test]
|
||||
async fn test_body() {
|
||||
let mut req = TestResponse::with_header(header::CONTENT_LENGTH, "xxxx").finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::UnknownLength => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let mut req =
|
||||
TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::Overflow => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
let mut req =
|
||||
TestResponse::with_header(header::CONTENT_LENGTH, "1000000").finish();
|
||||
match req.body().await.err().unwrap() {
|
||||
PayloadError::Overflow => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"test"))
|
||||
.finish();
|
||||
assert_eq!(req.body().await.ok().unwrap(), Bytes::from_static(b"test"));
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"test"))
|
||||
.finish();
|
||||
assert_eq!(req.body().await.ok().unwrap(), Bytes::from_static(b"test"));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"11111111111111"))
|
||||
.finish();
|
||||
match req.body().limit(5).await.err().unwrap() {
|
||||
PayloadError::Overflow => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
})
|
||||
let mut req = TestResponse::default()
|
||||
.set_payload(Bytes::from_static(b"11111111111111"))
|
||||
.finish();
|
||||
match req.body().limit(5).await.err().unwrap() {
|
||||
PayloadError::Overflow => (),
|
||||
_ => unreachable!("error"),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
@ -414,58 +410,56 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_json_body() {
|
||||
block_on(async {
|
||||
let mut req = TestResponse::default().finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
#[actix_rt::test]
|
||||
async fn test_json_body() {
|
||||
let mut req = TestResponse::default().finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/text"),
|
||||
)
|
||||
.finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/text"),
|
||||
)
|
||||
.finish();
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert!(json_eq(json.err().unwrap(), JsonPayloadError::ContentType));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("10000"),
|
||||
)
|
||||
.finish();
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("10000"),
|
||||
)
|
||||
.finish();
|
||||
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
|
||||
assert!(json_eq(
|
||||
json.err().unwrap(),
|
||||
JsonPayloadError::Payload(PayloadError::Overflow)
|
||||
));
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).limit(100).await;
|
||||
assert!(json_eq(
|
||||
json.err().unwrap(),
|
||||
JsonPayloadError::Payload(PayloadError::Overflow)
|
||||
));
|
||||
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
)
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.finish();
|
||||
let mut req = TestResponse::default()
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
)
|
||||
.header(
|
||||
header::CONTENT_LENGTH,
|
||||
header::HeaderValue::from_static("16"),
|
||||
)
|
||||
.set_payload(Bytes::from_static(b"{\"name\": \"test\"}"))
|
||||
.finish();
|
||||
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert_eq!(
|
||||
json.ok().unwrap(),
|
||||
MyObject {
|
||||
name: "test".to_owned()
|
||||
}
|
||||
);
|
||||
})
|
||||
let json = JsonBody::<_, MyObject>::new(&mut req).await;
|
||||
assert_eq!(
|
||||
json.ok().unwrap(),
|
||||
MyObject {
|
||||
name: "test".to_owned()
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,12 @@ use std::rc::Rc;
|
||||
use std::task::{Context, Poll};
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_rt::time::{delay_for, Delay};
|
||||
use bytes::Bytes;
|
||||
use derive_more::From;
|
||||
use futures::{future::LocalBoxFuture, ready, Future, Stream};
|
||||
use serde::Serialize;
|
||||
use serde_json;
|
||||
use tokio_timer::{delay_for, Delay};
|
||||
|
||||
use actix_http::body::{Body, BodyStream};
|
||||
use actix_http::encoding::Decoder;
|
||||
|
@ -7,8 +7,8 @@ use std::{fmt, str};
|
||||
use actix_codec::Framed;
|
||||
use actix_http::cookie::{Cookie, CookieJar};
|
||||
use actix_http::{ws, Payload, RequestHead};
|
||||
use actix_rt::time::Timeout;
|
||||
use percent_encoding::percent_encode;
|
||||
use tokio_timer::Timeout;
|
||||
|
||||
use actix_http::cookie::USERINFO;
|
||||
pub use actix_http::ws::{CloseCode, CloseReason, Codec, Frame, Message};
|
||||
@ -389,21 +389,19 @@ impl fmt::Debug for WebsocketsRequest {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use actix_web::test::block_on;
|
||||
|
||||
use super::*;
|
||||
use crate::Client;
|
||||
|
||||
#[test]
|
||||
fn test_debug() {
|
||||
#[actix_rt::test]
|
||||
async fn test_debug() {
|
||||
let request = Client::new().ws("/").header("x-test", "111");
|
||||
let repr = format!("{:?}", request);
|
||||
assert!(repr.contains("WebsocketsRequest"));
|
||||
assert!(repr.contains("x-test"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_header_override() {
|
||||
#[actix_rt::test]
|
||||
async fn test_header_override() {
|
||||
let req = Client::build()
|
||||
.header(header::CONTENT_TYPE, "111")
|
||||
.finish()
|
||||
@ -421,8 +419,8 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basic_auth() {
|
||||
#[actix_rt::test]
|
||||
async fn basic_auth() {
|
||||
let req = Client::new()
|
||||
.ws("/")
|
||||
.basic_auth("username", Some("password"));
|
||||
@ -448,8 +446,8 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bearer_auth() {
|
||||
#[actix_rt::test]
|
||||
async fn bearer_auth() {
|
||||
let req = Client::new().ws("/").bearer_auth("someS3cr3tAutht0k3n");
|
||||
assert_eq!(
|
||||
req.head
|
||||
@ -463,35 +461,33 @@ mod tests {
|
||||
let _ = req.connect();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn basics() {
|
||||
block_on(async {
|
||||
let req = Client::new()
|
||||
.ws("http://localhost/")
|
||||
.origin("test-origin")
|
||||
.max_frame_size(100)
|
||||
.server_mode()
|
||||
.protocols(&["v1", "v2"])
|
||||
.set_header_if_none(header::CONTENT_TYPE, "json")
|
||||
.set_header_if_none(header::CONTENT_TYPE, "text")
|
||||
.cookie(Cookie::build("cookie1", "value1").finish());
|
||||
assert_eq!(
|
||||
req.origin.as_ref().unwrap().to_str().unwrap(),
|
||||
"test-origin"
|
||||
);
|
||||
assert_eq!(req.max_size, 100);
|
||||
assert_eq!(req.server_mode, true);
|
||||
assert_eq!(req.protocols, Some("v1,v2".to_string()));
|
||||
assert_eq!(
|
||||
req.head.headers.get(header::CONTENT_TYPE).unwrap(),
|
||||
header::HeaderValue::from_static("json")
|
||||
);
|
||||
#[actix_rt::test]
|
||||
async fn basics() {
|
||||
let req = Client::new()
|
||||
.ws("http://localhost/")
|
||||
.origin("test-origin")
|
||||
.max_frame_size(100)
|
||||
.server_mode()
|
||||
.protocols(&["v1", "v2"])
|
||||
.set_header_if_none(header::CONTENT_TYPE, "json")
|
||||
.set_header_if_none(header::CONTENT_TYPE, "text")
|
||||
.cookie(Cookie::build("cookie1", "value1").finish());
|
||||
assert_eq!(
|
||||
req.origin.as_ref().unwrap().to_str().unwrap(),
|
||||
"test-origin"
|
||||
);
|
||||
assert_eq!(req.max_size, 100);
|
||||
assert_eq!(req.server_mode, true);
|
||||
assert_eq!(req.protocols, Some("v1,v2".to_string()));
|
||||
assert_eq!(
|
||||
req.head.headers.get(header::CONTENT_TYPE).unwrap(),
|
||||
header::HeaderValue::from_static("json")
|
||||
);
|
||||
|
||||
let _ = req.connect().await;
|
||||
let _ = req.connect().await;
|
||||
|
||||
assert!(Client::new().ws("/").connect().await.is_err());
|
||||
assert!(Client::new().ws("http:///test").connect().await.is_err());
|
||||
assert!(Client::new().ws("hmm://test.com/").connect().await.is_err());
|
||||
})
|
||||
assert!(Client::new().ws("/").connect().await.is_err());
|
||||
assert!(Client::new().ws("http:///test").connect().await.is_err());
|
||||
assert!(Client::new().ws("hmm://test.com/").connect().await.is_err());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user