idempotency_test.go 1.9 KB

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