gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/gmhttp/proxy_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gmhttp
     6  
     7  import (
     8  	"net/url"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  // TODO(mattn):
    14  //	test ProxyAuth
    15  
    16  //goland:noinspection HttpUrlsUsage
    17  var cacheKeysTests = []struct {
    18  	proxy  string
    19  	scheme string
    20  	addr   string
    21  	key    string
    22  }{
    23  	{"", "http", "foo.com", "|http|foo.com"},
    24  	{"", "https", "foo.com", "|https|foo.com"},
    25  	{"http://foo.com", "http", "foo.com", "http://foo.com|http|"},
    26  	{"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"},
    27  }
    28  
    29  func TestCacheKeys(t *testing.T) {
    30  	for _, tt := range cacheKeysTests {
    31  		var proxy *url.URL
    32  		if tt.proxy != "" {
    33  			u, err := url.Parse(tt.proxy)
    34  			if err != nil {
    35  				t.Fatal(err)
    36  			}
    37  			proxy = u
    38  		}
    39  		cm := connectMethod{proxyURL: proxy, targetScheme: tt.scheme, targetAddr: tt.addr}
    40  		if got := cm.key().String(); got != tt.key {
    41  			t.Fatalf("{%q, %q, %q} cache key = %q; want %q", tt.proxy, tt.scheme, tt.addr, got, tt.key)
    42  		}
    43  	}
    44  }
    45  
    46  func ResetProxyEnv() {
    47  	for _, v := range []string{"HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy", "REQUEST_METHOD"} {
    48  		_ = os.Unsetenv(v)
    49  	}
    50  	ResetCachedEnvironment()
    51  }