From 2be861cc05176a985acc43663f29deb154eb75e8 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 9 Jul 2019 11:21:10 -0700 Subject: [PATCH] support for http_proxy variables --- environ/proxy.go | 41 ++++++++++++++++++++++ environ/proxy_test.go | 81 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 environ/proxy.go create mode 100644 environ/proxy_test.go diff --git a/environ/proxy.go b/environ/proxy.go new file mode 100644 index 0000000..281ee77 --- /dev/null +++ b/environ/proxy.go @@ -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 +} diff --git a/environ/proxy_test.go b/environ/proxy_test.go new file mode 100644 index 0000000..9f6765b --- /dev/null +++ b/environ/proxy_test.go @@ -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") + } +}