github.com/iDevoid/mattermost-server@v5.11.1+incompatible/services/httpservice/client_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package httpservice
     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(NewTransport(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(NewTransport(true, nil, nil))
    60  	purl, _ := url.Parse(proxy.URL)
    61  	c.Transport.(*MattermostTransport).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  }
   121  
   122  func TestUserAgentIsSet(t *testing.T) {
   123  	testUserAgent := "test-user-agent"
   124  	defaultUserAgent = testUserAgent
   125  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
   126  		ua := req.UserAgent()
   127  		if ua == "" {
   128  			t.Error("expected user-agent to be non-empty")
   129  		}
   130  		if ua != testUserAgent {
   131  			t.Errorf("expected user-agent to be %q but was %q", testUserAgent, ua)
   132  		}
   133  	}))
   134  	defer ts.Close()
   135  	client := NewHTTPClient(NewTransport(true, nil, nil))
   136  	req, err := http.NewRequest("GET", ts.URL, nil)
   137  	if err != nil {
   138  		t.Fatal("NewRequest failed", err)
   139  	}
   140  	client.Do(req)
   141  }
   142  
   143  func NewHTTPClient(transport http.RoundTripper) *http.Client {
   144  	return &http.Client{
   145  		Transport: transport,
   146  	}
   147  }
   148  
   149  func TestIsReservedIP(t *testing.T) {
   150  	tests := []struct {
   151  		name string
   152  		ip net.IP
   153  		want bool
   154  	}{
   155  		{"127.8.3.5", net.IPv4(127, 8, 3, 5), true},
   156  		{"192.168.0.1", net.IPv4(192, 168, 0, 1), true},
   157  		{"169.254.0.6", net.IPv4(169, 254, 0, 6), true},
   158  		{"127.120.6.3", net.IPv4(127, 120, 6, 3), true},
   159  		{"8.8.8.8", net.IPv4(8, 8, 8, 8), false},
   160  		{"9.9.9.9", net.IPv4(9, 9, 9, 8), false},
   161  	}
   162  	for _, tt := range tests {
   163  		t.Run(tt.name, func(t *testing.T) {
   164  			if got := IsReservedIP(tt.ip); got != tt.want {
   165  				t.Errorf("IsReservedIP() = %v, want %v", got, tt.want)
   166  			}
   167  		})
   168  	}
   169  }
   170  
   171  func TestIsOwnIP(t *testing.T) {
   172  	tests := []struct {
   173  		name string
   174  		ip net.IP
   175  		want bool
   176  	}{
   177  		{"127.0.0.1", net.IPv4(127, 0, 0, 1), true},
   178  		{"8.8.8.8", net.IPv4(8, 0, 0, 8), false},
   179  	}
   180  	for _, tt := range tests {
   181  		t.Run(tt.name, func(t *testing.T) {
   182  			if got, _ := IsOwnIP(tt.ip); got != tt.want {
   183  				t.Errorf("IsOwnIP() = %v, want %v", got, tt.want)
   184  				t.Errorf(tt.ip.String());
   185  			}
   186  		})
   187  	}
   188  }