idempotency_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package protocol_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/aws/aws-sdk-go/private/protocol"
  6. )
  7. func TestCanSetIdempotencyToken(t *testing.T) {
  8. cases := []struct {
  9. CanSet bool
  10. Case interface{}
  11. }{
  12. {
  13. true,
  14. struct {
  15. Field *string `idempotencyToken:"true"`
  16. }{},
  17. },
  18. {
  19. true,
  20. struct {
  21. Field string `idempotencyToken:"true"`
  22. }{},
  23. },
  24. {
  25. false,
  26. struct {
  27. Field *string `idempotencyToken:"true"`
  28. }{Field: new(string)},
  29. },
  30. {
  31. false,
  32. struct {
  33. Field string `idempotencyToken:"true"`
  34. }{Field: "value"},
  35. },
  36. {
  37. false,
  38. struct {
  39. Field *int `idempotencyToken:"true"`
  40. }{},
  41. },
  42. {
  43. false,
  44. struct {
  45. Field *string
  46. }{},
  47. },
  48. }
  49. for i, c := range cases {
  50. v := reflect.Indirect(reflect.ValueOf(c.Case))
  51. ty := v.Type()
  52. canSet := protocol.CanSetIdempotencyToken(v.Field(0), ty.Field(0))
  53. if e, a := c.CanSet, canSet; e != a {
  54. t.Errorf("%d, expect %v, got %v", i, e, a)
  55. }
  56. }
  57. }
  58. func TestSetIdempotencyToken(t *testing.T) {
  59. cases := []struct {
  60. Case interface{}
  61. }{
  62. {
  63. &struct {
  64. Field *string `idempotencyToken:"true"`
  65. }{},
  66. },
  67. {
  68. &struct {
  69. Field string `idempotencyToken:"true"`
  70. }{},
  71. },
  72. {
  73. &struct {
  74. Field *string `idempotencyToken:"true"`
  75. }{Field: new(string)},
  76. },
  77. {
  78. &struct {
  79. Field string `idempotencyToken:"true"`
  80. }{Field: ""},
  81. },
  82. }
  83. for i, c := range cases {
  84. v := reflect.Indirect(reflect.ValueOf(c.Case))
  85. protocol.SetIdempotencyToken(v.Field(0))
  86. if v.Field(0).Interface() == nil {
  87. t.Errorf("%d, expect not nil", i)
  88. }
  89. }
  90. }
  91. func TestUUIDVersion4(t *testing.T) {
  92. uuid := protocol.UUIDVersion4(make([]byte, 16))
  93. if e, a := `00000000-0000-4000-8000-000000000000`, uuid; e != a {
  94. t.Errorf("expect %v, got %v", e, a)
  95. }
  96. b := make([]byte, 16)
  97. for i := 0; i < len(b); i++ {
  98. b[i] = 1
  99. }
  100. uuid = protocol.UUIDVersion4(b)
  101. if e, a := `01010101-0101-4101-8101-010101010101`, uuid; e != a {
  102. t.Errorf("expect %v, got %v", e, a)
  103. }
  104. }