From 9b7d2f7e22953065ab8af00c973a1fe6c19ad65a Mon Sep 17 00:00:00 2001 From: timzaak Date: Wed, 4 Sep 2024 18:35:10 +0800 Subject: [PATCH] add cors test --- tests/data/server_config_cors.toml | 20 ++++++++++++++++++ tests/src/main.rs | 1 + tests/tests/http_test.rs | 34 +++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/data/server_config_cors.toml diff --git a/tests/data/server_config_cors.toml b/tests/data/server_config_cors.toml new file mode 100644 index 0000000..3c432d3 --- /dev/null +++ b/tests/data/server_config_cors.toml @@ -0,0 +1,20 @@ +file_dir = "./data/web" + +[http] +port = 8080 +addr = "0.0.0.0" + + +[admin_config] +# bind host +port = 9000 +addr = "127.0.0.1" + +# this is used to check client request +# put it in http header, Authorization: Bearer $token +token = "token" + + +[[domains]] +domain = "local.fornetcode.com" +cors = ["http://localhost:9292"] diff --git a/tests/src/main.rs b/tests/src/main.rs index 206b5ba..09b983f 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -4,6 +4,7 @@ use anyhow::Result; use tracing::Level; use tracing_subscriber::EnvFilter; +//This does not work because of file_dir config. #[tokio::main] async fn main() -> Result<()> { env::set_var( diff --git a/tests/tests/http_test.rs b/tests/tests/http_test.rs index dffd938..c16f436 100644 --- a/tests/tests/http_test.rs +++ b/tests/tests/http_test.rs @@ -1,7 +1,7 @@ #![allow(unused_variables)] use reqwest::header::LOCATION; use reqwest::redirect::Policy; -use reqwest::{ClientBuilder, StatusCode}; +use reqwest::{ClientBuilder, Method, StatusCode}; use std::time::Duration; use tokio::time::sleep; use tracing::log::debug; @@ -333,3 +333,35 @@ async fn alias_start_server_and_client_upload_file() { ) .await } + + +#[tokio::test] +async fn cors() { + clean_web_domain_dir(LOCAL_HOST); + run_server_with_config("server_config_cors.toml"); + + let domain = LOCAL_HOST.to_owned() + "/27"; + let domain = &domain; + let request_prefix = format!("http://{LOCAL_HOST}:8080/27"); + let request_prefix = &request_prefix; + + tokio::time::sleep(Duration::from_secs(1)).await; + upload_file_and_check(domain, request_prefix, 1, vec!["index.html"]).await; + + let client = get_http_client(); + let request = client.request(Method::OPTIONS, request_prefix) + .header("Origin", "http://localhost:9292") + .header("Access-Control-Request-Headers","Origin, Accept, Content-Type") + .header("Access-Control-Request-Method", "GET") + .build().unwrap(); + let result = client.execute(request).await.unwrap(); + assert_eq!(result.status(), StatusCode::OK); + + let request = client.request(Method::OPTIONS, request_prefix) + .header("Origin", "http://localhost:9291") + .header("Access-Control-Request-Headers","Origin, Accept, Content-Type") + .header("Access-Control-Request-Method", "GET") + .build().unwrap(); + let result = client.execute(request).await.unwrap(); + assert_ne!(result.status(), StatusCode::OK); +} \ No newline at end of file