support for http_proxy variables

pull/1/head
Brad Rydzewski 5 years ago
parent 33c8695c30
commit 2be861cc05

@ -0,0 +1,41 @@
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Parity Public License
// that can be found in the LICENSE file.
package environ
import (
"strings"
)
// Proxy returns the http_proxy variables.
func Proxy() map[string]string {
environ := map[string]string{}
if value := envAnyCase("no_proxy"); value != "" {
environ["no_proxy"] = value
environ["NO_PROXY"] = value
}
if value := envAnyCase("http_proxy"); value != "" {
environ["http_proxy"] = value
environ["HTTP_PROXY"] = value
}
if value := envAnyCase("https_proxy"); value != "" {
environ["https_proxy"] = value
environ["HTTPS_PROXY"] = value
}
return environ
}
// helper function returns the environment variable value
// using a case-insenstive environment name.
func envAnyCase(name string) (value string) {
name = strings.ToUpper(name)
if value := getenv(name); value != "" {
return value
}
name = strings.ToLower(name)
if value := getenv(name); value != "" {
return value
}
return
}

@ -0,0 +1,81 @@
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Parity Public License
// that can be found in the LICENSE file.
package environ
import (
"os"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestProxy(t *testing.T) {
defer func() {
getenv = os.Getenv
}()
getenv = func(s string) string {
switch s {
case "no_proxy":
return "http://dummy.no.proxy"
case "http_proxy":
return "http://dummy.http.proxy"
case "https_proxy":
return "http://dummy.https.proxy"
default:
return ""
}
}
a := map[string]string{
"no_proxy": "http://dummy.no.proxy",
"NO_PROXY": "http://dummy.no.proxy",
"http_proxy": "http://dummy.http.proxy",
"HTTP_PROXY": "http://dummy.http.proxy",
"https_proxy": "http://dummy.https.proxy",
"HTTPS_PROXY": "http://dummy.https.proxy",
}
b := Proxy()
if diff := cmp.Diff(a, b); diff != "" {
t.Fail()
t.Log(diff)
}
}
func Test_envAnyCase(t *testing.T) {
defer func() {
getenv = os.Getenv
}()
getenv = func(s string) string {
switch s {
case "foo":
return "bar"
default:
return ""
}
}
if envAnyCase("FOO") != "bar" {
t.Errorf("Expect environment variable sourced from lowercase variant")
}
getenv = func(s string) string {
switch s {
case "FOO":
return "bar"
default:
return ""
}
}
if envAnyCase("foo") != "bar" {
t.Errorf("Expect environment variable sourced from uppercase variant")
}
if envAnyCase("bar") != "" {
t.Errorf("Expect zero value string when environment variable does not exit")
}
}
Loading…
Cancel
Save