8ca4fcbade
The `SetAllEnv` function is one of two functions that allow for mutable behavior within the `*config.Configuration` type. It is desirable for us to remove that function, and all of its uses throughout the LFS codebase. Unfortunately, a lot of `SetAllEnv` uses are coupled to initializing the `config.Configuration` instance with custom `.gitconfig` data, a-la `NewFromValues`. This coupling makes it difficult to write an atomic commit that *only* removes the usage of `SetAllEnv`. As a compromise, the signature of `NewFromValues` changed from: ``` func NewFromValues(gitconfig map[string]strimg) *Configuration ``` to... ``` type Values struct { Git, Env map[string]string } func NewFrom(v Values) *Configuration ``` To support reading fixed data as a part of the `Env` fetcher, a new Fetcher type was introduced: ``` type mapFetcher map[string]string func (m mapFetcher) Get(key string) (val string) { ... } ``` and is used in place of the old `*EnvFetcher` to retrieve data from the "environment".
105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package httputil
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/github/git-lfs/config"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestProxyFromGitConfig(t *testing.T) {
|
|
cfg := config.NewFrom(config.Values{
|
|
Git: map[string]string{
|
|
"http.proxy": "https://proxy-from-git-config:8080",
|
|
},
|
|
Env: 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-git-config:8080", proxyURL.Host)
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestHttpProxyFromGitConfig(t *testing.T) {
|
|
cfg := config.NewFrom(config.Values{
|
|
Git: map[string]string{
|
|
"http.proxy": "http://proxy-from-git-config:8080",
|
|
},
|
|
Env: 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)
|
|
}
|
|
|
|
func TestProxyFromEnvironment(t *testing.T) {
|
|
cfg := config.NewFrom(config.Values{
|
|
Env: 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)
|
|
}
|
|
|
|
func TestProxyIsNil(t *testing.T) {
|
|
cfg := config.New()
|
|
|
|
req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
proxyURL, err := ProxyFromGitConfigOrEnvironment(cfg)(req)
|
|
|
|
assert.Nil(t, proxyURL)
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestProxyNoProxy(t *testing.T) {
|
|
cfg := config.NewFrom(config.Values{
|
|
Git: map[string]string{
|
|
"http.proxy": "https://proxy-from-git-config:8080",
|
|
},
|
|
Env: 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)
|
|
|
|
assert.Nil(t, proxyUrl)
|
|
assert.Nil(t, err)
|
|
}
|