|
1 | 1 | package ctrd |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | +) |
4 | 7 |
|
5 | 8 | func TestHasPort(t *testing.T) { |
6 | 9 | type args struct { |
@@ -33,9 +36,60 @@ func TestHasPort(t *testing.T) { |
33 | 36 | } |
34 | 37 |
|
35 | 38 | func TestCanonicalAddr(t *testing.T) { |
36 | | - // TODO |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + urlString string |
| 42 | + want string |
| 43 | + }{ |
| 44 | + {name: "test1", urlString: "http://google.com", want: "google.com:80"}, |
| 45 | + {name: "test2", urlString: "https://www.github.com", want: "www.github.com:443"}, |
| 46 | + {name: "test3", urlString: "socks5://shadowsocks.com", want: "shadowsocks.com:1080"}, |
| 47 | + {name: "test4", urlString: "ftp://127.0.0.1", want: "127.0.0.1:"}, |
| 48 | + } |
| 49 | + |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + exampleURL, _ := url.Parse(tt.urlString) |
| 53 | + got := canonicalAddr(exampleURL) |
| 54 | + |
| 55 | + if got != tt.want { |
| 56 | + t.Errorf("canonicalAddr() = %v, want %v", got, tt.want) |
| 57 | + } |
| 58 | + }) |
| 59 | + } |
37 | 60 | } |
38 | 61 |
|
39 | 62 | func TestUseProxy(t *testing.T) { |
40 | | - // TODO |
| 63 | + tests := []struct { |
| 64 | + name string |
| 65 | + addr string |
| 66 | + noProxy string |
| 67 | + want bool |
| 68 | + }{ |
| 69 | + {name: "test1", addr: "", want: true}, |
| 70 | + {name: "test2", addr: "google.com", want: false}, |
| 71 | + {name: "test3", addr: "localhost:80", want: false}, |
| 72 | + {name: "test4", addr: "127.0.0.1:80", want: false}, |
| 73 | + {name: "test5", addr: "10.0.0.1:80", want: true}, |
| 74 | + {name: "test6", addr: "golang.org:80", noProxy: "*", want: false}, |
| 75 | + {name: "test7", addr: "maps.google.com:443", noProxy: ".google.com", want: false}, |
| 76 | + {name: "test8", addr: "google.com:443", noProxy: ".google.com", want: false}, |
| 77 | + {name: "test9", addr: "maps.google.com:443", noProxy: "google.com", want: false}, |
| 78 | + {name: "test10", addr: "google.com:443", noProxy: "google.com", want: false}, |
| 79 | + {name: "test11", addr: "maps.google.com:443", want: true}, |
| 80 | + } |
| 81 | + |
| 82 | + // simulate the fetch of environmental variable no_proxy |
| 83 | + noProxyEnv.once.Do(func() {}) |
| 84 | + |
| 85 | + for _, tt := range tests { |
| 86 | + t.Run(tt.name, func(t *testing.T) { |
| 87 | + noProxyEnv.val = tt.noProxy |
| 88 | + got := useProxy(tt.addr) |
| 89 | + |
| 90 | + if got != tt.want { |
| 91 | + t.Errorf("useProxy() = %v, want %v", got, tt.want) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
41 | 95 | } |
0 commit comments