github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/utils/httpclient_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"net/url"
    14  	"testing"
    15  )
    16  
    17  func TestHTTPClient(t *testing.T) {
    18  	for _, allowInternal := range []bool{true, false} {
    19  		c := NewHTTPClient(false, func(_ string) bool { return false }, func(ip net.IP) bool { return allowInternal || !IsReservedIP(ip) })
    20  		for _, tc := range []struct {
    21  			URL        string
    22  			IsInternal bool
    23  		}{
    24  			{
    25  				URL:        "https://google.com",
    26  				IsInternal: false,
    27  			},
    28  			{
    29  				URL:        "https://127.0.0.1",
    30  				IsInternal: true,
    31  			},
    32  		} {
    33  			_, err := c.Get(tc.URL)
    34  			if !tc.IsInternal {
    35  				if err != nil {
    36  					t.Fatal("google is down?")
    37  				}
    38  			} else {
    39  				allowed := !tc.IsInternal || allowInternal
    40  				success := err == nil
    41  				switch e := err.(type) {
    42  				case *net.OpError:
    43  					success = e.Err != AddressForbidden
    44  				case *url.Error:
    45  					success = e.Err != AddressForbidden
    46  				}
    47  				if success != allowed {
    48  					t.Fatalf("failed for %v. allowed: %v, success %v", tc.URL, allowed, success)
    49  				}
    50  			}
    51  		}
    52  	}
    53  }
    54  
    55  func TestHTTPClientWithProxy(t *testing.T) {
    56  	proxy := createProxyServer()
    57  	defer proxy.Close()
    58  
    59  	c := NewHTTPClient(true, nil, nil)
    60  	purl, _ := url.Parse(proxy.URL)
    61  	c.Transport.(*http.Transport).Proxy = http.ProxyURL(purl)
    62  
    63  	resp, err := c.Get("http://acme.com")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	defer resp.Body.Close()
    68  
    69  	body, err := ioutil.ReadAll(resp.Body)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	if string(body) != "proxy" {
    74  		t.FailNow()
    75  	}
    76  }
    77  
    78  func createProxyServer() *httptest.Server {
    79  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    80  		w.WriteHeader(200)
    81  		w.Header().Set("Content-Type", "text/plain; charset=us-ascii")
    82  		fmt.Fprint(w, "proxy")
    83  	}))
    84  }
    85  
    86  func TestDialContextFilter(t *testing.T) {
    87  	for _, tc := range []struct {
    88  		Addr    string
    89  		IsValid bool
    90  	}{
    91  		{
    92  			Addr:    "google.com:80",
    93  			IsValid: true,
    94  		},
    95  		{
    96  			Addr:    "8.8.8.8:53",
    97  			IsValid: true,
    98  		},
    99  		{
   100  			Addr: "127.0.0.1:80",
   101  		},
   102  		{
   103  			Addr:    "10.0.0.1:80",
   104  			IsValid: true,
   105  		},
   106  	} {
   107  		didDial := false
   108  		filter := dialContextFilter(func(ctx context.Context, network, addr string) (net.Conn, error) {
   109  			didDial = true
   110  			return nil, nil
   111  		}, func(host string) bool { return host == "10.0.0.1" }, func(ip net.IP) bool { return !IsReservedIP(ip) })
   112  		_, err := filter(context.Background(), "", tc.Addr)
   113  		switch {
   114  		case tc.IsValid == (err == AddressForbidden) || (err != nil && err != AddressForbidden):
   115  			t.Errorf("unexpected err for %v (%v)", tc.Addr, err)
   116  		case tc.IsValid != didDial:
   117  			t.Errorf("unexpected didDial for %v", tc.Addr)
   118  		}
   119  	}
   120  }