diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..659b109 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,42 @@ +name: "Run Format" + +on: + push: + branches: [main] + pull_request: + branches: [main] + +# Check formatting with rustfmt +jobs: + format: + name: cargo fmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Cache cargo dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Set up rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + components: "clippy, rustfmt" + toolchain: 1.83 + + - name: Add wasm32-wasi target + run: rustup target add wasm32-wasip1 + + - name: Run cargo fmt + uses: actions-rust-lang/rustfmt@v1 + + - name: Run cargo clipply + run: cargo clippy --all-targets --all-features + diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..648bf16 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,39 @@ +name: "Run Tests" + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + name: cargo test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Cache Cargo dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cargo/bin/ + ~/.cargo/registry/index/ + ~/.cargo/registry/cache/ + ~/.cargo/git/db/ + target/ + key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} + + - name: Set up Rust tool chain + uses: actions-rust-lang/setup-rust-toolchain@v1 + with: + toolchain: 1.83 + + - name: Add wasm32-wasi target + run: rustup target add wasm32-wasip1 + + - name: Setup Viceroy + run: cargo install viceroy + + - name: Run tests + run: cargo test diff --git a/Cargo.toml b/Cargo.toml index 6a50fa4..07523ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" # Otherwise, `publish = false` prevents an accidental `cargo publish` from revealing private source. publish = false license = "Apache-2.0" -license-file = "LICENSE" [profile.release] debug = 1 diff --git a/src/cookies.rs b/src/cookies.rs index 08ea00f..3ec61fa 100644 --- a/src/cookies.rs +++ b/src/cookies.rs @@ -1,9 +1,8 @@ use cookie::{Cookie, CookieJar}; use fastly::http::header; use fastly::Request; -use log; -const COOKIE_MAX_AGE: i32 = 1 * 365 * 24 * 60 * 60; // 1 year +const COOKIE_MAX_AGE: i32 = 365 * 24 * 60 * 60; // 1 year // return empty cookie jar for unparsable cookies pub fn parse_cookies_to_jar(s: &str) -> CookieJar { @@ -80,8 +79,8 @@ mod tests { #[test] fn test_handle_request_cookies() { - let mut req = Request::get("http://example.com").with_header(header::COOKIE, "c1=v1;c2=v2"); - let jar = handle_request_cookies(&mut req).unwrap(); + let req = Request::get("http://example.com").with_header(header::COOKIE, "c1=v1;c2=v2"); + let jar = handle_request_cookies(&req).unwrap(); assert!(jar.iter().count() == 2); assert_eq!(jar.get("c1").unwrap().value(), "v1"); @@ -90,24 +89,24 @@ mod tests { #[test] fn test_handle_request_cookies_with_empty_cookie() { - let mut req = Request::get("http://example.com").with_header(header::COOKIE, ""); - let jar = handle_request_cookies(&mut req).unwrap(); + let req = Request::get("http://example.com").with_header(header::COOKIE, ""); + let jar = handle_request_cookies(&req).unwrap(); assert!(jar.iter().count() == 0); } #[test] fn test_handle_request_cookies_no_cookie_header() { - let mut req: Request = Request::get("https://example.com"); - let jar = handle_request_cookies(&mut req); + let req: Request = Request::get("https://example.com"); + let jar = handle_request_cookies(&req); assert!(jar.is_none()); } #[test] fn test_handle_request_cookies_invalid_cookie_header() { - let mut req = Request::get("http://example.com").with_header(header::COOKIE, "invalid"); - let jar = handle_request_cookies(&mut req).unwrap(); + let req = Request::get("http://example.com").with_header(header::COOKIE, "invalid"); + let jar = handle_request_cookies(&req).unwrap(); assert!(jar.iter().count() == 0); } diff --git a/src/main.rs b/src/main.rs index e4ccefd..5cf0f28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,22 +129,22 @@ fn handle_ad_request(settings: &Settings, req: Request) -> Result { println!("Value from KV store: {}", s); - Ok(Some(s)) + Some(s) } Err(e) => { println!("Error converting bytes to string: {}", e); - Ok(None) + None } } }) - .and_then(|opt_s| { + .map(|opt_s| { println!("Parsing string value: {:?}", opt_s); - Ok(opt_s.and_then(|s| s.parse().ok())) + opt_s.and_then(|s| s.parse().ok()) }) .unwrap_or_else(|_| { println!("No existing count found, starting at 0"); @@ -163,7 +163,10 @@ fn handle_ad_request(settings: &Settings, req: Request) -> Result Strin return potsi; } - let req_cookie_jar: Option = handle_request_cookies(&req); + let req_cookie_jar: Option = handle_request_cookies(req); match req_cookie_jar { Some(jar) => { let potsi_cookie = jar.get("synthetic_id"); @@ -121,11 +120,11 @@ mod tests { fn test_generate_synthetic_id() { let settings: Settings = create_settings(); let req = create_test_request(vec![ - (&header::USER_AGENT.to_string(), "Mozilla/5.0"), - (&header::COOKIE.to_string(), "pub_userid=12345"), + (header::USER_AGENT.as_ref(), "Mozilla/5.0"), + (header::COOKIE.as_ref(), "pub_userid=12345"), ("X-Pub-User-ID", "67890"), - (&header::HOST.to_string(), "example.com"), - (&header::ACCEPT_LANGUAGE.to_string(), "en-US,en;q=0.9"), + (header::HOST.as_ref(), "example.com"), + (header::ACCEPT_LANGUAGE.as_ref(), "en-US,en;q=0.9"), ]); let synthetic_id = generate_synthetic_id(&settings, &req); @@ -148,7 +147,7 @@ mod tests { fn test_get_or_generate_synthetic_id_with_cookie() { let settings = create_settings(); let req = create_test_request(vec![( - &header::COOKIE.to_string(), + header::COOKIE.as_ref(), "synthetic_id=existing_cookie_id", )]);