Skip to content

Commit

Permalink
add cors test
Browse files Browse the repository at this point in the history
  • Loading branch information
timzaak committed Sep 4, 2024
1 parent 9258253 commit f1ba899
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
20 changes: 20 additions & 0 deletions tests/data/server_config_cors.toml
Original file line number Diff line number Diff line change
@@ -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"]
1 change: 1 addition & 0 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
34 changes: 33 additions & 1 deletion tests/tests/http_test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

0 comments on commit f1ba899

Please sign in to comment.