| 1937 | } |
| 1938 | |
| 1939 | func TestProxyConfig_Proxy(t *testing.T) { |
| 1940 | var proxyServer *httptest.Server |
| 1941 | |
| 1942 | defer func() { |
| 1943 | if proxyServer != nil { |
| 1944 | proxyServer.Close() |
| 1945 | } |
| 1946 | }() |
| 1947 | |
| 1948 | proxyServerHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1949 | fmt.Fprintf(w, "Hello, %s", r.URL.Path) |
| 1950 | }) |
| 1951 | |
| 1952 | proxyServer = httptest.NewServer(proxyServerHandler) |
| 1953 | |
| 1954 | testCases := []struct { |
| 1955 | name string |
| 1956 | proxyConfig string |
| 1957 | expectedProxyURL string |
| 1958 | targetURL string |
| 1959 | proxyEnv string |
| 1960 | noProxyEnv string |
| 1961 | }{ |
| 1962 | { |
| 1963 | name: "proxy from environment", |
| 1964 | proxyConfig: `proxy_from_environment: true`, |
| 1965 | expectedProxyURL: proxyServer.URL, |
| 1966 | proxyEnv: proxyServer.URL, |
| 1967 | targetURL: "http://prometheus.io/", |
| 1968 | }, |
| 1969 | { |
| 1970 | name: "proxy_from_environment with no_proxy", |
| 1971 | proxyConfig: `proxy_from_environment: true`, |
| 1972 | expectedProxyURL: "", |
| 1973 | proxyEnv: proxyServer.URL, |
| 1974 | noProxyEnv: "prometheus.io", |
| 1975 | targetURL: "http://prometheus.io/", |
| 1976 | }, |
| 1977 | { |
| 1978 | name: "proxy_from_environment and localhost", |
| 1979 | proxyConfig: `proxy_from_environment: true`, |
| 1980 | expectedProxyURL: "", |
| 1981 | proxyEnv: proxyServer.URL, |
| 1982 | targetURL: "http://localhost/", |
| 1983 | }, |
| 1984 | { |
| 1985 | name: "valid proxy_url and localhost", |
| 1986 | proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL), |
| 1987 | expectedProxyURL: proxyServer.URL, |
| 1988 | targetURL: "http://localhost/", |
| 1989 | }, |
| 1990 | { |
| 1991 | name: "valid proxy_url and no_proxy and localhost", |
| 1992 | proxyConfig: fmt.Sprintf(`proxy_url: %s |
| 1993 | no_proxy: prometheus.io`, proxyServer.URL), |
| 1994 | expectedProxyURL: "", |
| 1995 | targetURL: "http://localhost/", |
| 1996 | }, |