| 1 | package lfshttp |
| 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 := 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 | |
| 18 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 19 | require.Nil(t, err) |
| 20 | |
| 21 | proxyURL, err := proxyFromClient(c)(req) |
| 22 | assert.Equal(t, "proxy-from-git-config:8080", proxyURL.Host) |
| 23 | assert.Nil(t, err) |
| 24 | } |
| 25 | |
| 26 | func TestProxyForURL(t *testing.T) { |
| 27 | c := NewClient(NewContext(nil, nil, map[string]string{ |
| 28 | "http.proxy": "https://proxy-for-everyone:8080", |
| 29 | "http.https://some-host.com:123.proxy": "https://proxy-for-some-host:8080", |
| 30 | })) |
| 31 | |
| 32 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 33 | require.Nil(t, err) |
| 34 | |
| 35 | proxyURL, err := proxyFromClient(c)(req) |
| 36 | assert.Equal(t, "proxy-for-some-host:8080", proxyURL.Host) |
| 37 | assert.Nil(t, err) |
| 38 | } |
| 39 | |
| 40 | func TestHttpProxyFromGitConfig(t *testing.T) { |
| 41 | c := NewClient(NewContext(nil, map[string]string{ |
| 42 | "HTTPS_PROXY": "https://proxy-from-env:8080", |
| 43 | }, map[string]string{ |
| 44 | "http.proxy": "http://proxy-from-git-config:8080", |
| 45 | })) |
| 46 | |
| 47 | req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) |
| 48 | require.Nil(t, err) |
| 49 | |
| 50 | proxyURL, err := proxyFromClient(c)(req) |
| 51 | assert.Equal(t, "proxy-from-git-config:8080", proxyURL.Host) |
| 52 | assert.Nil(t, err) |
| 53 | } |
| 54 | |
| 55 | func TestProxyFromEnvironment(t *testing.T) { |
| 56 | c := NewClient(NewContext(nil, map[string]string{ |
| 57 | "HTTPS_PROXY": "https://proxy-from-env:8080", |
| 58 | }, nil)) |
| 59 | |
| 60 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 61 | require.Nil(t, err) |
| 62 | |
| 63 | proxyURL, err := proxyFromClient(c)(req) |
| 64 | assert.Equal(t, "proxy-from-env:8080", proxyURL.Host) |
| 65 | assert.Nil(t, err) |
| 66 | } |
| 67 | |
| 68 | func TestHTTPSProxyFromEnvironment(t *testing.T) { |
| 69 | c := NewClient(NewContext(nil, map[string]string{ |
| 70 | "HTTPS_PROXY": "http://proxy-from-env:8080", |
| 71 | }, nil)) |
| 72 | |
| 73 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 74 | require.Nil(t, err) |
| 75 | |
| 76 | proxyURL, err := proxyFromClient(c)(req) |
| 77 | assert.Equal(t, "proxy-from-env:8080", proxyURL.Host) |
| 78 | assert.Nil(t, err) |
| 79 | } |
| 80 | |
| 81 | func TestHTTPProxyFromEnvironment(t *testing.T) { |
| 82 | c := NewClient(NewContext(nil, map[string]string{ |
| 83 | "HTTPS_PROXY": "http://proxy-from-env:8080", |
| 84 | }, nil)) |
| 85 | |
| 86 | req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) |
| 87 | require.Nil(t, err) |
| 88 | |
| 89 | proxyURL, err := proxyFromClient(c)(req) |
| 90 | assert.Nil(t, proxyURL) |
| 91 | assert.Nil(t, err) |
| 92 | } |
| 93 | |
| 94 | func TestProxyIsNil(t *testing.T) { |
| 95 | c := NewClient(nil) |
| 96 | |
| 97 | req, err := http.NewRequest("GET", "http://some-host.com:123/foo/bar", nil) |
| 98 | require.Nil(t, err) |
| 99 | |
| 100 | proxyURL, err := proxyFromClient(c)(req) |
| 101 | assert.Nil(t, proxyURL) |
| 102 | assert.Nil(t, err) |
| 103 | } |
| 104 | |
| 105 | func TestProxyNoProxy(t *testing.T) { |
| 106 | c := NewClient(NewContext(nil, map[string]string{ |
| 107 | "NO_PROXY": "some-host", |
| 108 | }, map[string]string{ |
| 109 | "http.proxy": "https://proxy-from-git-config:8080", |
| 110 | })) |
| 111 | |
| 112 | req, err := http.NewRequest("GET", "https://some-host:8080", nil) |
| 113 | require.Nil(t, err) |
| 114 | |
| 115 | proxyURL, err := proxyFromClient(c)(req) |
| 116 | assert.Nil(t, proxyURL) |
| 117 | assert.Nil(t, err) |
| 118 | } |
| 119 | |
| 120 | func TestProxyNoProxyWithWildcard(t *testing.T) { |
| 121 | c := NewClient(NewContext(nil, map[string]string{ |
| 122 | "NO_PROXY": "*.example.com", |
| 123 | }, map[string]string{ |
| 124 | "http.proxy": "https://proxy-from-git-config:8080", |
| 125 | })) |
| 126 | |
| 127 | req, err := http.NewRequest("GET", "https://foo.example.com:8080", nil) |
| 128 | require.Nil(t, err) |
| 129 | |
| 130 | proxyURL, err := proxyFromClient(c)(req) |
| 131 | assert.Nil(t, proxyURL) |
| 132 | assert.Nil(t, err) |
| 133 | } |
| 134 | |
| 135 | func TestSocksProxyFromEnvironment(t *testing.T) { |
| 136 | c := NewClient(NewContext(nil, map[string]string{ |
| 137 | "HTTPS_PROXY": "socks5://proxy-from-env:3128", |
| 138 | }, nil)) |
| 139 | |
| 140 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 141 | require.Nil(t, err) |
| 142 | |
| 143 | proxyURL, err := proxyFromClient(c)(req) |
| 144 | assert.Equal(t, "socks5", proxyURL.Scheme) |
| 145 | assert.Equal(t, "proxy-from-env:3128", proxyURL.Host) |
| 146 | assert.Nil(t, err) |
| 147 | } |
| 148 | |
| 149 | func TestSocks5hProxyFromEnvironment(t *testing.T) { |
| 150 | c := NewClient(NewContext(nil, map[string]string{ |
| 151 | "HTTPS_PROXY": "socks5h://proxy-from-env:3128", |
| 152 | }, nil)) |
| 153 | |
| 154 | req, err := http.NewRequest("GET", "https://some-host.com:123/foo/bar", nil) |
| 155 | require.Nil(t, err) |
| 156 | |
| 157 | proxyURL, err := proxyFromClient(c)(req) |
| 158 | assert.Equal(t, "socks5", proxyURL.Scheme) |
| 159 | assert.Equal(t, "proxy-from-env:3128", proxyURL.Host) |
| 160 | assert.Nil(t, err) |
| 161 | } |