github.com/drone/runner-go@v1.12.0/environ/proxy_test.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package environ
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  )
    13  
    14  func TestProxy(t *testing.T) {
    15  	defer func() {
    16  		getenv = os.Getenv
    17  	}()
    18  
    19  	getenv = func(s string) string {
    20  		switch s {
    21  		case "no_proxy":
    22  			return "http://dummy.no.proxy"
    23  		case "http_proxy":
    24  			return "http://dummy.http.proxy"
    25  		case "https_proxy":
    26  			return "http://dummy.https.proxy"
    27  		case "all_proxy":
    28  			return "http://dummy.https.proxy"
    29  		default:
    30  			return ""
    31  		}
    32  	}
    33  
    34  	a := map[string]string{
    35  		"no_proxy":    "http://dummy.no.proxy",
    36  		"NO_PROXY":    "http://dummy.no.proxy",
    37  		"http_proxy":  "http://dummy.http.proxy",
    38  		"HTTP_PROXY":  "http://dummy.http.proxy",
    39  		"https_proxy": "http://dummy.https.proxy",
    40  		"HTTPS_PROXY": "http://dummy.https.proxy",
    41  		"all_proxy":   "http://dummy.https.proxy",
    42  		"ALL_PROXY":   "http://dummy.https.proxy",
    43  	}
    44  	b := Proxy()
    45  	if diff := cmp.Diff(a, b); diff != "" {
    46  		t.Fail()
    47  		t.Log(diff)
    48  	}
    49  }
    50  
    51  func Test_envAnyCase(t *testing.T) {
    52  	defer func() {
    53  		getenv = os.Getenv
    54  	}()
    55  
    56  	getenv = func(s string) string {
    57  		switch s {
    58  		case "foo":
    59  			return "bar"
    60  		default:
    61  			return ""
    62  		}
    63  	}
    64  
    65  	if envAnyCase("FOO") != "bar" {
    66  		t.Errorf("Expect environment variable sourced from lowercase variant")
    67  	}
    68  
    69  	getenv = func(s string) string {
    70  		switch s {
    71  		case "FOO":
    72  			return "bar"
    73  		default:
    74  			return ""
    75  		}
    76  	}
    77  
    78  	if envAnyCase("foo") != "bar" {
    79  		t.Errorf("Expect environment variable sourced from uppercase variant")
    80  	}
    81  
    82  	if envAnyCase("bar") != "" {
    83  		t.Errorf("Expect zero value string when environment variable does not exit")
    84  	}
    85  }