github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/lfsapi/proxy_test.go (about) 1 package lfsapi 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestHttpsProxyFromGitConfig(t *testing.T) { 12 c, err := NewClient(NewContext(nil, map[string]string{ 13 "HTTPS_PROXY": "https://proxy-from-env:8080", 14 }, map[string]string{ 15 "http.proxy": "https://proxy-from-git-config:8080", 16 })) 17 require.Nil(t, err) 18 19 req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) 20 require.Nil(t, err) 21 22 proxyURL, err := proxyFromClient(c)(req) 23 assert.Equal(t, "proxy-from-git-config:8080", proxyURL.Host) 24 assert.Nil(t, err) 25 } 26 27 func TestProxyForURL(t *testing.T) { 28 c, err := NewClient(NewContext(nil, nil, map[string]string{ 29 "http.proxy": "https://proxy-for-everyone:8080", 30 "http.https://some-host.com:123.proxy": "https://proxy-for-some-host:8080", 31 })) 32 require.Nil(t, err) 33 34 req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) 35 require.Nil(t, err) 36 37 proxyURL, err := proxyFromClient(c)(req) 38 assert.Equal(t, "proxy-for-some-host:8080", proxyURL.Host) 39 assert.Nil(t, err) 40 } 41 42 func TestHttpProxyFromGitConfig(t *testing.T) { 43 c, err := NewClient(NewContext(nil, map[string]string{ 44 "HTTPS_PROXY": "https://proxy-from-env:8080", 45 }, map[string]string{ 46 "http.proxy": "http://proxy-from-git-config:8080", 47 })) 48 require.Nil(t, err) 49 50 req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) 51 require.Nil(t, err) 52 53 proxyURL, err := proxyFromClient(c)(req) 54 assert.Equal(t, "proxy-from-git-config:8080", proxyURL.Host) 55 assert.Nil(t, err) 56 } 57 58 func TestProxyFromEnvironment(t *testing.T) { 59 c, err := NewClient(NewContext(nil, map[string]string{ 60 "HTTPS_PROXY": "https://proxy-from-env:8080", 61 }, nil)) 62 require.Nil(t, err) 63 64 req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) 65 require.Nil(t, err) 66 67 proxyURL, err := proxyFromClient(c)(req) 68 assert.Equal(t, "proxy-from-env:8080", proxyURL.Host) 69 assert.Nil(t, err) 70 } 71 72 func TestProxyIsNil(t *testing.T) { 73 c, _ := NewClient(nil) 74 75 req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) 76 require.Nil(t, err) 77 78 proxyURL, err := proxyFromClient(c)(req) 79 assert.Nil(t, proxyURL) 80 assert.Nil(t, err) 81 } 82 83 func TestProxyNoProxy(t *testing.T) { 84 c, err := NewClient(NewContext(nil, map[string]string{ 85 "NO_PROXY": "some-host", 86 }, map[string]string{ 87 "http.proxy": "https://proxy-from-git-config:8080", 88 })) 89 require.Nil(t, err) 90 91 req, err := http.NewRequest("GET", "https://some-host:8080", nil) 92 require.Nil(t, err) 93 94 proxyURL, err := proxyFromClient(c)(req) 95 assert.Nil(t, proxyURL) 96 assert.Nil(t, err) 97 }