github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/fshttp/http_test.go (about)

     1  package fshttp
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // returns the "%p" representation of the thing passed in
    12  func ptr(p interface{}) string {
    13  	return fmt.Sprintf("%p", p)
    14  }
    15  
    16  func TestSetDefaults(t *testing.T) {
    17  	old := http.DefaultTransport.(*http.Transport)
    18  	newT := new(http.Transport)
    19  	setDefaults(newT, old)
    20  	// Can't use assert.Equal or reflect.DeepEqual for this as it has functions in
    21  	// Check functions by comparing the "%p" representations of them
    22  	assert.Equal(t, ptr(old.Proxy), ptr(newT.Proxy), "when checking .Proxy")
    23  	assert.Equal(t, ptr(old.DialContext), ptr(newT.DialContext), "when checking .DialContext")
    24  	// Check the other public fields
    25  	assert.Equal(t, ptr(old.Dial), ptr(newT.Dial), "when checking .Dial")
    26  	assert.Equal(t, ptr(old.DialTLS), ptr(newT.DialTLS), "when checking .DialTLS")
    27  	assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig")
    28  	assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout")
    29  	assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives")
    30  	assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression")
    31  	assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns")
    32  	assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost")
    33  	assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout")
    34  	assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout")
    35  	assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout")
    36  	assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto")
    37  	assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes")
    38  }
    39  
    40  func TestCleanAuth(t *testing.T) {
    41  	for _, test := range []struct {
    42  		in   string
    43  		want string
    44  	}{
    45  		{"", ""},
    46  		{"floo", "floo"},
    47  		{"Authorization: ", "Authorization: "},
    48  		{"Authorization: \n", "Authorization: \n"},
    49  		{"Authorization: A", "Authorization: X"},
    50  		{"Authorization: A\n", "Authorization: X\n"},
    51  		{"Authorization: AAAA", "Authorization: XXXX"},
    52  		{"Authorization: AAAA\n", "Authorization: XXXX\n"},
    53  		{"Authorization: AAAAA", "Authorization: XXXX"},
    54  		{"Authorization: AAAAA\n", "Authorization: XXXX\n"},
    55  		{"Authorization: AAAA\n", "Authorization: XXXX\n"},
    56  		{"Authorization: AAAAAAAAA\nPotato: Help\n", "Authorization: XXXX\nPotato: Help\n"},
    57  		{"Sausage: 1\nAuthorization: AAAAAAAAA\nPotato: Help\n", "Sausage: 1\nAuthorization: XXXX\nPotato: Help\n"},
    58  	} {
    59  		got := string(cleanAuth([]byte(test.in), authBufs[0]))
    60  		assert.Equal(t, test.want, got, test.in)
    61  	}
    62  }
    63  
    64  func TestCleanAuths(t *testing.T) {
    65  	for _, test := range []struct {
    66  		in   string
    67  		want string
    68  	}{
    69  		{"", ""},
    70  		{"floo", "floo"},
    71  		{"Authorization: AAAAAAAAA\nPotato: Help\n", "Authorization: XXXX\nPotato: Help\n"},
    72  		{"X-Auth-Token: AAAAAAAAA\nPotato: Help\n", "X-Auth-Token: XXXX\nPotato: Help\n"},
    73  		{"X-Auth-Token: AAAAAAAAA\nAuthorization: AAAAAAAAA\nPotato: Help\n", "X-Auth-Token: XXXX\nAuthorization: XXXX\nPotato: Help\n"},
    74  	} {
    75  		got := string(cleanAuths([]byte(test.in)))
    76  		assert.Equal(t, test.want, got, test.in)
    77  	}
    78  }