diff --git a/actix-framed/tests/test_server.rs b/actix-framed/tests/test_server.rs index 964403e1..00f6a97d 100644 --- a/actix-framed/tests/test_server.rs +++ b/actix-framed/tests/test_server.rs @@ -1,5 +1,5 @@ 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_service::{IntoNewService, NewService}; 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()); // client service diff --git a/awc/src/request.rs b/awc/src/request.rs index c868d052..a280dfce 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -566,9 +566,21 @@ mod tests { .version(Version::HTTP_2) .set(header::Date(SystemTime::now().into())) .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); assert!(req.headers().contains_key(header::CONTENT_TYPE)); 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); let _ = req.headers_mut(); let _ = req.send_body(""); diff --git a/awc/src/ws.rs b/awc/src/ws.rs index 1ab6d563..028330ab 100644 --- a/awc/src/ws.rs +++ b/awc/src/ws.rs @@ -445,30 +445,41 @@ mod tests { .unwrap(), "Bearer someS3cr3tAutht0k3n" ); + let _ = req.connect(); } #[test] fn basics() { - let req = Client::new() - .ws("/") - .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(); + actix_http_test::run_on(|| { + 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(); + }); + + 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()); } } diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index 0f0652c4..afccdff8 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::io::{Read, Write}; use std::time::Duration; @@ -63,6 +64,38 @@ fn test_simple() { 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| 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>| 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] fn test_timeout() { let mut srv = TestServer::new(|| {