1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

update tests

This commit is contained in:
Nikolay Kim 2019-04-18 21:28:23 -07:00
parent aa255298ef
commit bfe0df5ab0
4 changed files with 83 additions and 22 deletions

View File

@ -1,5 +1,5 @@
use actix_codec::{AsyncRead, AsyncWrite}; use actix_codec::{AsyncRead, AsyncWrite};
use actix_http::{body, ws, Error, HttpService, Response}; use actix_http::{body, http::StatusCode, ws, Error, HttpService, Response};
use actix_http_test::TestServer; use actix_http_test::TestServer;
use actix_service::{IntoNewService, NewService}; use actix_service::{IntoNewService, NewService};
use actix_utils::framed::FramedTransport; use actix_utils::framed::FramedTransport;
@ -99,6 +99,11 @@ fn test_service() {
) )
}); });
// non ws request
let res = srv.block_on(srv.get("/index.html").send()).unwrap();
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
// not found
assert!(srv.ws_at("/test").is_err()); assert!(srv.ws_at("/test").is_err());
// client service // client service

View File

@ -566,9 +566,21 @@ mod tests {
.version(Version::HTTP_2) .version(Version::HTTP_2)
.set(header::Date(SystemTime::now().into())) .set(header::Date(SystemTime::now().into()))
.content_type("plain/text") .content_type("plain/text")
.if_true(true, |req| req.header(header::SERVER, "awc"))
.if_true(false, |req| req.header(header::EXPECT, "awc"))
.if_some(Some("server"), |val, req| {
req.header(header::USER_AGENT, val)
})
.if_some(Option::<&str>::None, |_, req| {
req.header(header::ALLOW, "1")
})
.content_length(100); .content_length(100);
assert!(req.headers().contains_key(header::CONTENT_TYPE)); assert!(req.headers().contains_key(header::CONTENT_TYPE));
assert!(req.headers().contains_key(header::DATE)); assert!(req.headers().contains_key(header::DATE));
assert!(req.headers().contains_key(header::SERVER));
assert!(req.headers().contains_key(header::USER_AGENT));
assert!(!req.headers().contains_key(header::ALLOW));
assert!(!req.headers().contains_key(header::EXPECT));
assert_eq!(req.head.version, Version::HTTP_2); assert_eq!(req.head.version, Version::HTTP_2);
let _ = req.headers_mut(); let _ = req.headers_mut();
let _ = req.send_body(""); let _ = req.send_body("");

View File

@ -445,30 +445,41 @@ mod tests {
.unwrap(), .unwrap(),
"Bearer someS3cr3tAutht0k3n" "Bearer someS3cr3tAutht0k3n"
); );
let _ = req.connect();
} }
#[test] #[test]
fn basics() { fn basics() {
let req = Client::new() actix_http_test::run_on(|| {
.ws("/") let req = Client::new()
.origin("test-origin") .ws("http://localhost/")
.max_frame_size(100) .origin("test-origin")
.server_mode() .max_frame_size(100)
.protocols(&["v1", "v2"]) .server_mode()
.set_header_if_none(header::CONTENT_TYPE, "json") .protocols(&["v1", "v2"])
.set_header_if_none(header::CONTENT_TYPE, "text") .set_header_if_none(header::CONTENT_TYPE, "json")
.cookie(Cookie::build("cookie1", "value1").finish()); .set_header_if_none(header::CONTENT_TYPE, "text")
assert_eq!( .cookie(Cookie::build("cookie1", "value1").finish());
req.origin.as_ref().unwrap().to_str().unwrap(), assert_eq!(
"test-origin" 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.max_size, 100);
assert_eq!(req.protocols, Some("v1,v2".to_string())); assert_eq!(req.server_mode, true);
assert_eq!( assert_eq!(req.protocols, Some("v1,v2".to_string()));
req.head.headers.get(header::CONTENT_TYPE).unwrap(), assert_eq!(
header::HeaderValue::from_static("json") req.head.headers.get(header::CONTENT_TYPE).unwrap(),
); header::HeaderValue::from_static("json")
let _ = req.connect(); );
let _ = req.connect();
});
assert!(Client::new().ws("/").connect().poll().is_err());
assert!(Client::new().ws("http:///test").connect().poll().is_err());
assert!(Client::new()
.ws("hmm://test.com/")
.connect()
.poll()
.is_err());
} }
} }

View File

@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::time::Duration; use std::time::Duration;
@ -63,6 +64,38 @@ fn test_simple() {
assert_eq!(bytes, Bytes::from_static(STR.as_ref())); assert_eq!(bytes, Bytes::from_static(STR.as_ref()));
} }
#[test]
fn test_json() {
let mut srv = TestServer::new(|| {
HttpService::new(App::new().service(
web::resource("/").route(web::to(|_: web::Json<String>| HttpResponse::Ok())),
))
});
let request = srv
.get("/")
.header("x-test", "111")
.send_json(&"TEST".to_string());
let response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
}
#[test]
fn test_form() {
let mut srv = TestServer::new(|| {
HttpService::new(App::new().service(web::resource("/").route(web::to(
|_: web::Form<HashMap<String, String>>| HttpResponse::Ok(),
))))
});
let mut data = HashMap::new();
let _ = data.insert("key".to_string(), "TEST".to_string());
let request = srv.get("/").header("x-test", "111").send_form(&data);
let response = srv.block_on(request).unwrap();
assert!(response.status().is_success());
}
#[test] #[test]
fn test_timeout() { fn test_timeout() {
let mut srv = TestServer::new(|| { let mut srv = TestServer::new(|| {