structs_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package structs
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. // returns the "%p" representation of the thing passed in
  9. func ptr(p interface{}) string {
  10. return fmt.Sprintf("%p", p)
  11. }
  12. func TestSetDefaults(t *testing.T) {
  13. old := http.DefaultTransport.(*http.Transport)
  14. newT := new(http.Transport)
  15. SetDefaults(newT, old)
  16. // Can't use assert.Equal or reflect.DeepEqual for this as it has functions in
  17. // Check functions by comparing the "%p" representations of them
  18. assert.Equal(t, ptr(old.Proxy), ptr(newT.Proxy), "when checking .Proxy")
  19. assert.Equal(t, ptr(old.DialContext), ptr(newT.DialContext), "when checking .DialContext")
  20. assert.Equal(t, ptr(old.DialTLSContext), ptr(newT.DialTLSContext), "when checking .DialTLSContext")
  21. assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig")
  22. assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout")
  23. assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives")
  24. assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression")
  25. assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns")
  26. assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost")
  27. assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout")
  28. assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout")
  29. assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout")
  30. assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto")
  31. assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes")
  32. }
  33. type aType struct {
  34. Matching string
  35. OnlyA string
  36. MatchingInt int
  37. DifferentType string
  38. }
  39. type bType struct {
  40. Matching string
  41. OnlyB string
  42. MatchingInt int
  43. DifferentType int
  44. Unused string
  45. }
  46. func TestSetFrom(t *testing.T) {
  47. a := aType{
  48. Matching: "a",
  49. OnlyA: "onlyA",
  50. MatchingInt: 1,
  51. DifferentType: "surprise",
  52. }
  53. b := bType{
  54. Matching: "b",
  55. OnlyB: "onlyB",
  56. MatchingInt: 2,
  57. DifferentType: 7,
  58. Unused: "Ha",
  59. }
  60. bBefore := b
  61. SetFrom(&a, &b)
  62. assert.Equal(t, aType{
  63. Matching: "b",
  64. OnlyA: "onlyA",
  65. MatchingInt: 2,
  66. DifferentType: "surprise",
  67. }, a)
  68. assert.Equal(t, bBefore, b)
  69. }
  70. func TestSetFromReversed(t *testing.T) {
  71. a := aType{
  72. Matching: "a",
  73. OnlyA: "onlyA",
  74. MatchingInt: 1,
  75. DifferentType: "surprise",
  76. }
  77. aBefore := a
  78. b := bType{
  79. Matching: "b",
  80. OnlyB: "onlyB",
  81. MatchingInt: 2,
  82. DifferentType: 7,
  83. Unused: "Ha",
  84. }
  85. SetFrom(&b, &a)
  86. assert.Equal(t, bType{
  87. Matching: "a",
  88. OnlyB: "onlyB",
  89. MatchingInt: 1,
  90. DifferentType: 7,
  91. Unused: "Ha",
  92. }, b)
  93. assert.Equal(t, aBefore, a)
  94. }