forked from
anil.recoil.org/ocaml-requests
A batteries included HTTP/1.1 client in OCaml
1(*---------------------------------------------------------------------------
2 Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
3 SPDX-License-Identifier: ISC
4 ---------------------------------------------------------------------------*)
5
6(** Tests for Proxy module *)
7
8module Proxy = Requests.Proxy
9
10(** {1 HTTP Proxy Constructor Tests} *)
11
12let test_http_default_port () =
13 let config = Proxy.http "proxy.example.com" in
14 Alcotest.(check string) "host" "proxy.example.com" config.host;
15 Alcotest.(check int) "default port" 8080 config.port;
16 Alcotest.(check bool) "no auth" true (Option.is_none config.auth);
17 Alcotest.(check (list string)) "no no_proxy" [] config.no_proxy
18
19let test_http_custom_port () =
20 let config = Proxy.http ~port:3128 "proxy.example.com" in
21 Alcotest.(check string) "host" "proxy.example.com" config.host;
22 Alcotest.(check int) "custom port" 3128 config.port
23
24(** {1 host_port Tests} *)
25
26let test_host_port () =
27 let config = Proxy.http ~port:3128 "proxy.example.com" in
28 let host, port = Proxy.host_port config in
29 Alcotest.(check string) "host" "proxy.example.com" host;
30 Alcotest.(check int) "port" 3128 port
31
32(** {1 should_bypass Tests} *)
33
34let test_should_bypass_exact_match () =
35 let config = Proxy.http ~no_proxy:[ "localhost" ] "proxy.example.com" in
36 Alcotest.(check bool)
37 "bypass localhost" true
38 (Proxy.should_bypass config "http://localhost/foo");
39 Alcotest.(check bool)
40 "don't bypass example.com" false
41 (Proxy.should_bypass config "http://example.com/foo")
42
43let test_should_bypass_dot_prefix () =
44 let config = Proxy.http ~no_proxy:[ ".example.com" ] "proxy.example.com" in
45 Alcotest.(check bool)
46 "bypass foo.example.com" true
47 (Proxy.should_bypass config "http://foo.example.com/path");
48 Alcotest.(check bool)
49 "don't bypass other.com" false
50 (Proxy.should_bypass config "http://other.com/path")
51
52let test_should_bypass_wildcard () =
53 let config = Proxy.http ~no_proxy:[ "*.example.com" ] "proxy.example.com" in
54 Alcotest.(check bool)
55 "bypass foo.example.com" true
56 (Proxy.should_bypass config "http://foo.example.com/path");
57 Alcotest.(check bool)
58 "don't bypass other.com" false
59 (Proxy.should_bypass config "http://other.com/path")
60
61(** {1 validate_supported Tests} *)
62
63let test_validate_supported_http () =
64 let config = Proxy.http "proxy.example.com" in
65 (* Should not raise *)
66 Proxy.validate_supported config
67
68let test_validate_supported_socks5 () =
69 let config = Proxy.socks5 "proxy.example.com" in
70 Alcotest.check_raises "SOCKS5 raises"
71 (Requests.Error.err
72 (Requests.Error.Proxy_error
73 {
74 host = "proxy.example.com";
75 reason = "SOCKS5 proxy is not yet implemented";
76 }))
77 (fun () -> Proxy.validate_supported config)
78
79(** {1 from_env Tests} *)
80
81let test_from_env_no_vars () =
82 (* With no proxy env vars set, should return None *)
83 let result = Proxy.from_env () in
84 (* This test depends on environment; if HTTP_PROXY etc. are set it may return
85 Some. We check the return type is option. *)
86 ignore result
87
88(** {1 Test Suite} *)
89
90let suite =
91 ( "proxy",
92 [
93 Alcotest.test_case "default port" `Quick test_http_default_port;
94 Alcotest.test_case "custom port" `Quick test_http_custom_port;
95 Alcotest.test_case "returns host and port" `Quick test_host_port;
96 Alcotest.test_case "exact match" `Quick test_should_bypass_exact_match;
97 Alcotest.test_case "dot prefix" `Quick test_should_bypass_dot_prefix;
98 Alcotest.test_case "wildcard" `Quick test_should_bypass_wildcard;
99 Alcotest.test_case "HTTP doesn't raise" `Quick
100 test_validate_supported_http;
101 Alcotest.test_case "SOCKS5 raises" `Quick test_validate_supported_socks5;
102 Alcotest.test_case "no env vars" `Quick test_from_env_no_vars;
103 ] )