Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions ctrd/image_proxy_util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ctrd

import "testing"
import (
"net/url"
"testing"
)

func TestHasPort(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -33,9 +36,60 @@ func TestHasPort(t *testing.T) {
}

func TestCanonicalAddr(t *testing.T) {
// TODO
tests := []struct {
name string
urlString string
want string
}{
{name: "test1", urlString: "http://google.com", want: "google.com:80"},
{name: "test2", urlString: "https://www.github.com", want: "www.github.com:443"},
{name: "test3", urlString: "socks5://shadowsocks.com", want: "shadowsocks.com:1080"},
{name: "test4", urlString: "ftp://127.0.0.1", want: "127.0.0.1:"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exampleURL, _ := url.Parse(tt.urlString)
got := canonicalAddr(exampleURL)

if got != tt.want {
t.Errorf("canonicalAddr() = %v, want %v", got, tt.want)
}
})
}
}

func TestUseProxy(t *testing.T) {
// TODO
tests := []struct {
name string
addr string
noProxy string
want bool
}{
{name: "test1", addr: "", want: true},
{name: "test2", addr: "google.com", want: false},
{name: "test3", addr: "localhost:80", want: false},
{name: "test4", addr: "127.0.0.1:80", want: false},
{name: "test5", addr: "10.0.0.1:80", want: true},
{name: "test6", addr: "golang.org:80", noProxy: "*", want: false},
{name: "test7", addr: "maps.google.com:443", noProxy: ".google.com", want: false},
{name: "test8", addr: "google.com:443", noProxy: ".google.com", want: false},
{name: "test9", addr: "maps.google.com:443", noProxy: "google.com", want: false},
{name: "test10", addr: "google.com:443", noProxy: "google.com", want: false},
{name: "test11", addr: "maps.google.com:443", want: true},
}

// simulate the fetch of environmental variable no_proxy
noProxyEnv.once.Do(func() {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little tricky, but LGTM


for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
noProxyEnv.val = tt.noProxy
got := useProxy(tt.addr)

if got != tt.want {
t.Errorf("useProxy() = %v, want %v", got, tt.want)
}
})
}
}