(*--------------------------------------------------------------------------- Copyright (c) 2025 Anil Madhavapeddy . All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** Tests for Proxy module *) module Proxy = Requests.Proxy (** {1 HTTP Proxy Constructor Tests} *) let test_http_default_port () = let config = Proxy.http "proxy.example.com" in Alcotest.(check string) "host" "proxy.example.com" config.host; Alcotest.(check int) "default port" 8080 config.port; Alcotest.(check bool) "no auth" true (Option.is_none config.auth); Alcotest.(check (list string)) "no no_proxy" [] config.no_proxy let test_http_custom_port () = let config = Proxy.http ~port:3128 "proxy.example.com" in Alcotest.(check string) "host" "proxy.example.com" config.host; Alcotest.(check int) "custom port" 3128 config.port (** {1 host_port Tests} *) let test_host_port () = let config = Proxy.http ~port:3128 "proxy.example.com" in let host, port = Proxy.host_port config in Alcotest.(check string) "host" "proxy.example.com" host; Alcotest.(check int) "port" 3128 port (** {1 should_bypass Tests} *) let test_should_bypass_exact_match () = let config = Proxy.http ~no_proxy:[ "localhost" ] "proxy.example.com" in Alcotest.(check bool) "bypass localhost" true (Proxy.should_bypass config "http://localhost/foo"); Alcotest.(check bool) "don't bypass example.com" false (Proxy.should_bypass config "http://example.com/foo") let test_should_bypass_dot_prefix () = let config = Proxy.http ~no_proxy:[ ".example.com" ] "proxy.example.com" in Alcotest.(check bool) "bypass foo.example.com" true (Proxy.should_bypass config "http://foo.example.com/path"); Alcotest.(check bool) "don't bypass other.com" false (Proxy.should_bypass config "http://other.com/path") let test_should_bypass_wildcard () = let config = Proxy.http ~no_proxy:[ "*.example.com" ] "proxy.example.com" in Alcotest.(check bool) "bypass foo.example.com" true (Proxy.should_bypass config "http://foo.example.com/path"); Alcotest.(check bool) "don't bypass other.com" false (Proxy.should_bypass config "http://other.com/path") (** {1 validate_supported Tests} *) let test_validate_supported_http () = let config = Proxy.http "proxy.example.com" in (* Should not raise *) Proxy.validate_supported config let test_validate_supported_socks5 () = let config = Proxy.socks5 "proxy.example.com" in Alcotest.check_raises "SOCKS5 raises" (Requests.Error.err (Requests.Error.Proxy_error { host = "proxy.example.com"; reason = "SOCKS5 proxy is not yet implemented"; })) (fun () -> Proxy.validate_supported config) (** {1 from_env Tests} *) let test_from_env_no_vars () = (* With no proxy env vars set, should return None *) let result = Proxy.from_env () in (* This test depends on environment; if HTTP_PROXY etc. are set it may return Some. We check the return type is option. *) ignore result (** {1 Test Suite} *) let suite = ( "proxy", [ Alcotest.test_case "default port" `Quick test_http_default_port; Alcotest.test_case "custom port" `Quick test_http_custom_port; Alcotest.test_case "returns host and port" `Quick test_host_port; Alcotest.test_case "exact match" `Quick test_should_bypass_exact_match; Alcotest.test_case "dot prefix" `Quick test_should_bypass_dot_prefix; Alcotest.test_case "wildcard" `Quick test_should_bypass_wildcard; Alcotest.test_case "HTTP doesn't raise" `Quick test_validate_supported_http; Alcotest.test_case "SOCKS5 raises" `Quick test_validate_supported_socks5; Alcotest.test_case "no env vars" `Quick test_from_env_no_vars; ] )