Source file src/net/http/proxy_test.go

     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 http
     6  
     7  import (
     8  	"net/url"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  var cacheKeysTests = []struct {
    14  	proxy  string
    15  	scheme string
    16  	addr   string
    17  	key    string
    18  }{
    19  	{"", "http", "foo.com", "|http|foo.com"},
    20  	{"", "https", "foo.com", "|https|foo.com"},
    21  	{"http://foo.com", "http", "foo.com", "http://foo.com|http|"},
    22  	{"http://foo.com", "https", "foo.com", "http://foo.com|https|foo.com"},
    23  }
    24  
    25  func TestCacheKeys(t *testing.T) {
    26  	for _, tt := range cacheKeysTests {
    27  		var proxy *url.URL
    28  		if tt.proxy != "" {
    29  			u, err := url.Parse(tt.proxy)
    30  			if err != nil {
    31  				t.Fatal(err)
    32  			}
    33  			proxy = u
    34  		}
    35  		cm := connectMethod{proxyURL: proxy, targetScheme: tt.scheme, targetAddr: tt.addr}
    36  		if got := cm.key().String(); got != tt.key {
    37  			t.Fatalf("{%q, %q, %q} cache key = %q; want %q", tt.proxy, tt.scheme, tt.addr, got, tt.key)
    38  		}
    39  	}
    40  }
    41  
    42  func ResetProxyEnv() {
    43  	for _, v := range []string{"HTTP_PROXY", "http_proxy", "NO_PROXY", "no_proxy", "REQUEST_METHOD"} {
    44  		os.Unsetenv(v)
    45  	}
    46  	ResetCachedEnvironment()
    47  }
    48  
    49  var proxyAuthTests = []struct {
    50  	proxy string
    51  	key   string
    52  }{
    53  	{
    54  		"",
    55  		"",
    56  	},
    57  	{
    58  		"http://bar.com",
    59  		"",
    60  	},
    61  	{
    62  		"http://foo@bar.com",
    63  		"Basic Zm9vOg==",
    64  	},
    65  	{
    66  		"http://foo:bar@bar.com",
    67  		"Basic Zm9vOmJhcg==",
    68  	},
    69  }
    70  
    71  func TestProxyAuthKeys(t *testing.T) {
    72  	for _, tt := range proxyAuthTests {
    73  		var proxy *url.URL
    74  		if tt.proxy != "" {
    75  			u, err := url.Parse(tt.proxy)
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  			proxy = u
    80  		}
    81  		cm := connectMethod{proxyURL: proxy}
    82  		if got := cm.proxyAuth(); got != tt.key {
    83  			t.Fatalf("{%q} proxyAuth key = %q; want %q", tt.proxy, got, tt.key)
    84  		}
    85  	}
    86  }
    87  

View as plain text