Skip to content

Commit 55c1a57

Browse files
author
Wei Fu
authored
Merge pull request #1786 from JeromeTan1997/develop
test: add unit test cases for image_proxy_util.go
2 parents 44a5af1 + 51dfea7 commit 55c1a57

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

ctrd/image_proxy_util_test.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package ctrd
22

3-
import "testing"
3+
import (
4+
"net/url"
5+
"testing"
6+
)
47

58
func TestHasPort(t *testing.T) {
69
type args struct {
@@ -33,9 +36,60 @@ func TestHasPort(t *testing.T) {
3336
}
3437

3538
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+
}
3760
}
3861

3962
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+
}
4195
}

0 commit comments

Comments
 (0)