git » dnss » commit 38f7ea6

test: Set HTTPS_PROXY earlier to prevent accidental caching

author Alberto Bertogli
2018-07-17 00:14:37 UTC
committer Alberto Bertogli
2018-07-17 00:14:37 UTC
parent 9ef528c72690862297851edeb6c84cfac2e8979d

test: Set HTTPS_PROXY earlier to prevent accidental caching

http.ProxyFromEnvironment will check the environment upon first call,
and then cache the results.

Today, we have a test, TestProxyServerDomain, which sets $HTTPS_PROXY
and assumes it is the first to call http.ProxyFromEnvironment.

In upcoming Go versions, that function might get called
indirectly from the http library, which means setting $HTTPS_PROXY
inside TestProxyServerDomain may no longer work.

To work around this issue and make tests more robust, this patch moves
the setting of the environment to TestMain.

dnss_test.go +6 -1

diff --git a/dnss_test.go b/dnss_test.go
index 5150ed0..f6ad15f 100644
--- a/dnss_test.go
+++ b/dnss_test.go
@@ -25,6 +25,9 @@ func TestMain(m *testing.M) {
 	flag.Parse()
 	log.Init()
 	log.Default.Level = log.Error
+
+	// We need to do this early, see TestProxyServerDomain.
+	os.Setenv("HTTPS_PROXY", "http://proxy:1234/p")
 	os.Exit(m.Run())
 }
 
@@ -202,7 +205,9 @@ func BenchmarkSimple(b *testing.B) {
 // for a single case.
 func TestProxyServerDomain(t *testing.T) {
 	*httpsUpstream = "https://montoto/xyz"
-	os.Setenv("HTTPS_PROXY", "http://proxy:1234/p")
+	// In TestMain we set: HTTPS_PROXY=http://proxy:1234/p
+	// We have to do that earlier to prevent other tests from (indirectly)
+	// calling http.ProxyFromEnvironment and have it cache a nil result.
 	if got := proxyServerDomain(); got != "proxy" {
 		t.Errorf("got %q, expected 'proxy'", got)
 	}