git-lfs/httputil/proxy_test.go

98 lines
2.2 KiB
Go
Raw Normal View History

package httputil
import (
"net/http"
"testing"
2016-07-08 19:56:55 +00:00
"github.com/github/git-lfs/config"
"github.com/stretchr/testify/assert"
)
2016-07-21 16:06:18 +00:00
func TestProxyFromGitConfig(t *testing.T) {
cfg := config.NewFromValues(map[string]string{
"http.proxy": "https://proxy-from-git-config:8080",
})
cfg.SetAllEnv(map[string]string{
"HTTPS_PROXY": "https://proxy-from-env:8080",
})
req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil)
if err != nil {
t.Fatal(err)
}
proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
2016-07-21 16:06:18 +00:00
assert.Equal(t, "proxy-from-git-config:8080", proxyURL.Host)
assert.Nil(t, err)
}
2016-07-21 16:06:18 +00:00
func TestHttpProxyFromGitConfig(t *testing.T) {
2016-07-08 19:56:55 +00:00
cfg := config.NewFromValues(map[string]string{
2016-07-21 16:06:18 +00:00
"http.proxy": "http://proxy-from-git-config:8080",
})
cfg.SetAllEnv(map[string]string{
"HTTPS_PROXY": "https://proxy-from-env:8080",
2016-07-08 19:56:55 +00:00
})
2016-07-21 16:06:18 +00:00
req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil)
if err != nil {
t.Fatal(err)
}
2016-07-08 19:56:55 +00:00
proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
2016-07-21 16:06:18 +00:00
assert.Equal(t, "proxy-from-env:8080", proxyURL.Host)
assert.Nil(t, err)
}
func TestProxyFromEnvironment(t *testing.T) {
2016-07-21 23:38:44 +00:00
cfg := config.New()
2016-07-21 16:06:18 +00:00
cfg.SetAllEnv(map[string]string{
"HTTPS_PROXY": "https://proxy-from-env:8080",
})
req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil)
if err != nil {
t.Fatal(err)
}
proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
assert.Equal(t, "proxy-from-env:8080", proxyURL.Host)
assert.Nil(t, err)
}
2016-07-08 19:56:55 +00:00
func TestProxyIsNil(t *testing.T) {
2016-07-21 23:38:44 +00:00
cfg := config.New()
req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil)
2016-07-08 19:56:55 +00:00
if err != nil {
t.Fatal(err)
}
proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
2016-07-08 19:56:55 +00:00
assert.Nil(t, proxyURL)
assert.Nil(t, err)
}
2016-07-14 16:42:35 +00:00
func TestProxyNoProxy(t *testing.T) {
cfg := config.NewFromValues(map[string]string{
"http.proxy": "https://proxy-from-git-config:8080",
})
cfg.SetAllEnv(map[string]string{
"NO_PROXY": "some-host",
})
req, err := http.NewRequest("GET", "https://some-host:8080", nil)
if err != nil {
t.Fatal(err)
}
proxyUrl, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
2016-07-14 16:42:35 +00:00
assert.Nil(t, proxyUrl)
assert.Nil(t, err)
}