github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/lib/structs/structs_test.go (about) 1 package structs 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 assert.Equal(t, ptr(old.DialTLSContext), ptr(newT.DialTLSContext), "when checking .DialTLSContext") 25 assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig") 26 assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout") 27 assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives") 28 assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression") 29 assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns") 30 assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost") 31 assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout") 32 assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout") 33 assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout") 34 assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto") 35 assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes") 36 } 37 38 type aType struct { 39 Matching string 40 OnlyA string 41 MatchingInt int 42 DifferentType string 43 } 44 45 type bType struct { 46 Matching string 47 OnlyB string 48 MatchingInt int 49 DifferentType int 50 Unused string 51 } 52 53 func TestSetFrom(t *testing.T) { 54 a := aType{ 55 Matching: "a", 56 OnlyA: "onlyA", 57 MatchingInt: 1, 58 DifferentType: "surprise", 59 } 60 61 b := bType{ 62 Matching: "b", 63 OnlyB: "onlyB", 64 MatchingInt: 2, 65 DifferentType: 7, 66 Unused: "Ha", 67 } 68 bBefore := b 69 70 SetFrom(&a, &b) 71 72 assert.Equal(t, aType{ 73 Matching: "b", 74 OnlyA: "onlyA", 75 MatchingInt: 2, 76 DifferentType: "surprise", 77 }, a) 78 79 assert.Equal(t, bBefore, b) 80 } 81 82 func TestSetFromReversed(t *testing.T) { 83 a := aType{ 84 Matching: "a", 85 OnlyA: "onlyA", 86 MatchingInt: 1, 87 DifferentType: "surprise", 88 } 89 aBefore := a 90 91 b := bType{ 92 Matching: "b", 93 OnlyB: "onlyB", 94 MatchingInt: 2, 95 DifferentType: 7, 96 Unused: "Ha", 97 } 98 99 SetFrom(&b, &a) 100 101 assert.Equal(t, bType{ 102 Matching: "a", 103 OnlyB: "onlyB", 104 MatchingInt: 1, 105 DifferentType: 7, 106 Unused: "Ha", 107 }, b) 108 109 assert.Equal(t, aBefore, a) 110 }