customizations_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package dynamodb_test
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/aws/awserr"
  11. "github.com/aws/aws-sdk-go/aws/request"
  12. "github.com/aws/aws-sdk-go/awstesting/unit"
  13. "github.com/aws/aws-sdk-go/service/dynamodb"
  14. )
  15. var db *dynamodb.DynamoDB
  16. func TestMain(m *testing.M) {
  17. db = dynamodb.New(unit.Session, &aws.Config{
  18. MaxRetries: aws.Int(2),
  19. })
  20. db.Handlers.Send.Clear() // mock sending
  21. os.Exit(m.Run())
  22. }
  23. func mockCRCResponse(svc *dynamodb.DynamoDB, status int, body, crc string) (req *request.Request) {
  24. header := http.Header{}
  25. header.Set("x-amz-crc32", crc)
  26. req, _ = svc.ListTablesRequest(nil)
  27. req.Handlers.Send.PushBack(func(*request.Request) {
  28. req.HTTPResponse = &http.Response{
  29. ContentLength: int64(len(body)),
  30. StatusCode: status,
  31. Body: ioutil.NopCloser(bytes.NewReader([]byte(body))),
  32. Header: header,
  33. }
  34. })
  35. req.Send()
  36. return
  37. }
  38. func TestDefaultRetryRules(t *testing.T) {
  39. d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(-1)})
  40. assert.Equal(t, d.MaxRetries(), 10)
  41. }
  42. func TestCustomRetryRules(t *testing.T) {
  43. d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(2)})
  44. assert.Equal(t, d.MaxRetries(), 2)
  45. }
  46. func TestValidateCRC32NoHeaderSkip(t *testing.T) {
  47. req := mockCRCResponse(db, 200, "{}", "")
  48. assert.NoError(t, req.Error)
  49. }
  50. func TestValidateCRC32InvalidHeaderSkip(t *testing.T) {
  51. req := mockCRCResponse(db, 200, "{}", "ABC")
  52. assert.NoError(t, req.Error)
  53. }
  54. func TestValidateCRC32AlreadyErrorSkip(t *testing.T) {
  55. req := mockCRCResponse(db, 400, "{}", "1234")
  56. assert.Error(t, req.Error)
  57. assert.NotEqual(t, "CRC32CheckFailed", req.Error.(awserr.Error).Code())
  58. }
  59. func TestValidateCRC32IsValid(t *testing.T) {
  60. req := mockCRCResponse(db, 200, `{"TableNames":["A"]}`, "3090163698")
  61. assert.NoError(t, req.Error)
  62. // CRC check does not affect output parsing
  63. out := req.Data.(*dynamodb.ListTablesOutput)
  64. assert.Equal(t, "A", *out.TableNames[0])
  65. }
  66. func TestValidateCRC32DoesNotMatch(t *testing.T) {
  67. req := mockCRCResponse(db, 200, "{}", "1234")
  68. assert.Error(t, req.Error)
  69. assert.Equal(t, "CRC32CheckFailed", req.Error.(awserr.Error).Code())
  70. assert.Equal(t, 2, req.RetryCount)
  71. }
  72. func TestValidateCRC32DoesNotMatchNoComputeChecksum(t *testing.T) {
  73. svc := dynamodb.New(unit.Session, &aws.Config{
  74. MaxRetries: aws.Int(2),
  75. DisableComputeChecksums: aws.Bool(true),
  76. })
  77. svc.Handlers.Send.Clear() // mock sending
  78. req := mockCRCResponse(svc, 200, `{"TableNames":["A"]}`, "1234")
  79. assert.NoError(t, req.Error)
  80. assert.Equal(t, 0, int(req.RetryCount))
  81. // CRC check disabled. Does not affect output parsing
  82. out := req.Data.(*dynamodb.ListTablesOutput)
  83. assert.Equal(t, "A", *out.TableNames[0])
  84. }