File: test_proxy.ml

package info (click to toggle)
ocaml-cohttp 6.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,624 kB
  • sloc: ml: 13,107; makefile: 20; sh: 18; javascript: 18
file content (111 lines) | stat: -rw-r--r-- 4,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
module Proxy = Cohttp.Proxy.Forward

let http_proxy = "http://proxy.com"
let https_proxy = "http://https-proxy.com"
let fallback_proxy = "http://fallback-proxy.com"

let proxies ~no_proxy_patterns =
  Proxy.make_servers ~no_proxy_patterns
    ~default_proxy:(Some (Uri.of_string fallback_proxy))
    ~scheme_proxies:
      [
        ("http", Uri.of_string http_proxy); ("https", Uri.of_string https_proxy);
      ]
    ~direct:Uri.to_string ~tunnel:Uri.to_string

let proxy =
  let pp fmt = function
    | Proxy.Direct s -> Format.fprintf fmt "Direct(%S)" s
    | Proxy.Tunnel s -> Format.fprintf fmt "Tunnel(%S)" s
  in
  Alcotest.testable pp ( = )

let select_http_proxy () =
  let proxies = proxies ~no_proxy_patterns:None in
  let expected = Some (Proxy.Direct http_proxy) in
  let actual = Proxy.get proxies @@ Uri.of_string "http://example.com" in
  Alcotest.check' (Alcotest.option proxy)
    ~msg:"should select configured http proxy as Direct" ~actual ~expected

let select_https_proxy () =
  let proxies = proxies ~no_proxy_patterns:None in
  let expected = Some (Proxy.Tunnel https_proxy) in
  let actual = Proxy.get proxies @@ Uri.of_string "https://example.com" in
  Alcotest.check' (Alcotest.option proxy)
    ~msg:"should select configured https proxy as Tunnel" ~actual ~expected

let select_default_proxy () =
  let proxies = proxies ~no_proxy_patterns:None in
  let expected = Some (Proxy.Direct fallback_proxy) in
  let actual = Proxy.get proxies @@ Uri.of_string "ftp://example.com" in
  Alcotest.check' (Alcotest.option proxy)
    ~msg:"should select fallback proxy for unconfigured scheme" ~actual
    ~expected

let no_proxy_wildcard () =
  let proxies = proxies ~no_proxy_patterns:(Some "*") in
  let expected = None in
  let actual = Proxy.get proxies @@ Uri.of_string "http://example.com" in
  Alcotest.check' (Alcotest.option proxy)
    ~msg:"should ensure no proxy is selected" ~actual ~expected

let no_proxy_literal_pattern () =
  let proxies = proxies ~no_proxy_patterns:(Some "example.com") in
  let expected = None in
  let actual = Proxy.get proxies @@ Uri.of_string "http://example.com" in
  Alcotest.check' (Alcotest.option proxy)
    ~msg:"should ensure example.com is not proxied" ~actual ~expected

let no_proxy_list_of_patterns () =
  let proxies = proxies ~no_proxy_patterns:(Some "foo.com,example.com") in

  let msg = "should ensure example.com is not proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://example.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual ~expected:None;

  let msg = "should ensure foo.com is not proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://foo.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual ~expected:None

let no_proxy_subdomain_patterns () =
  (* As per https://everything.curl.dev/usingcurl/proxies/env.html#no-proxy

     > If a name in the exclusion list starts with a dot (.), then the name matches
     that entire domain. For example .example.com matches both www.example.com and
     home.example.com but not nonexample.com. *)
  let proxies = proxies ~no_proxy_patterns:(Some ".example.com") in

  let msg = "should ensure www.example.com is not proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://www.example.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual ~expected:None;

  let msg = "should ensure home.example.com is not proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://home.example.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual ~expected:None;

  let msg = "should ensure example.com is proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://example.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual ~expected:None;

  let msg = "nonexample.com should be proxied" in
  let actual = Proxy.get proxies @@ Uri.of_string "http://nonexample.com" in
  Alcotest.check' (Alcotest.option proxy) ~msg ~actual
    ~expected:(Some (Direct http_proxy))

let () =
  Alcotest.run "test_proxy"
    [
      ( "NO_PROXY",
        [
          ("wildcard pattern", `Quick, no_proxy_wildcard);
          ("literal pattern", `Quick, no_proxy_literal_pattern);
          ("list of patterns", `Quick, no_proxy_list_of_patterns);
          ("subdomain patterns", `Quick, no_proxy_subdomain_patterns);
        ] );
      ( "scheme proxies",
        [
          ("selects http proxy", `Quick, select_http_proxy);
          ("selects https proxy", `Quick, select_https_proxy);
          ("selects default proxy", `Quick, select_default_proxy);
        ] );
    ]