github.com/aavshr/aws-sdk-go@v1.41.3/service/dynamodb/customizations_test.go (about)

     1  package dynamodb_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/aavshr/aws-sdk-go/aws"
    11  	"github.com/aavshr/aws-sdk-go/aws/awserr"
    12  	"github.com/aavshr/aws-sdk-go/aws/client"
    13  	"github.com/aavshr/aws-sdk-go/aws/request"
    14  	"github.com/aavshr/aws-sdk-go/awstesting/unit"
    15  	"github.com/aavshr/aws-sdk-go/service/dynamodb"
    16  )
    17  
    18  var db *dynamodb.DynamoDB
    19  
    20  func TestMain(m *testing.M) {
    21  	db = dynamodb.New(unit.Session, &aws.Config{
    22  		MaxRetries: aws.Int(2),
    23  	})
    24  	db.Handlers.Send.Clear() // mock sending
    25  
    26  	os.Exit(m.Run())
    27  }
    28  
    29  func mockCRCResponse(svc *dynamodb.DynamoDB, status int, body, crc string) (req *request.Request) {
    30  	header := http.Header{}
    31  	header.Set("x-amz-crc32", crc)
    32  
    33  	req, _ = svc.ListTablesRequest(nil)
    34  	req.Handlers.Build.RemoveByName("crr.endpointdiscovery")
    35  
    36  	req.Handlers.Send.PushBack(func(*request.Request) {
    37  		req.HTTPResponse = &http.Response{
    38  			ContentLength: int64(len(body)),
    39  			StatusCode:    status,
    40  			Body:          ioutil.NopCloser(bytes.NewReader([]byte(body))),
    41  			Header:        header,
    42  		}
    43  	})
    44  	req.Send()
    45  	return
    46  }
    47  
    48  func TestDefaultRetryRules(t *testing.T) {
    49  	d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(-1)})
    50  	if e, a := 10, d.MaxRetries(); e != a {
    51  		t.Errorf("expect %d max retries, got %d", e, a)
    52  	}
    53  }
    54  
    55  func TestCustomRetryRules(t *testing.T) {
    56  	d := dynamodb.New(unit.Session, &aws.Config{MaxRetries: aws.Int(2)})
    57  	if e, a := 2, d.MaxRetries(); e != a {
    58  		t.Errorf("expect %d max retries, got %d", e, a)
    59  	}
    60  }
    61  
    62  type testCustomRetryer struct {
    63  	client.DefaultRetryer
    64  }
    65  
    66  func TestCustomRetry_FromConfig(t *testing.T) {
    67  	d := dynamodb.New(unit.Session, &aws.Config{
    68  		Retryer: testCustomRetryer{client.DefaultRetryer{NumMaxRetries: 9}},
    69  	})
    70  
    71  	if _, ok := d.Retryer.(testCustomRetryer); !ok {
    72  		t.Errorf("expect retryer to be testCustomRetryer, but got %T", d.Retryer)
    73  	}
    74  
    75  	if e, a := 9, d.MaxRetries(); e != a {
    76  		t.Errorf("expect %d max retries from custom retryer, got %d", e, a)
    77  	}
    78  }
    79  
    80  func TestValidateCRC32NoHeaderSkip(t *testing.T) {
    81  	req := mockCRCResponse(db, 200, "{}", "")
    82  	if req.Error != nil {
    83  		t.Errorf("expect no error, got %v", req.Error)
    84  	}
    85  }
    86  
    87  func TestValidateCRC32InvalidHeaderSkip(t *testing.T) {
    88  	req := mockCRCResponse(db, 200, "{}", "ABC")
    89  	if req.Error != nil {
    90  		t.Errorf("expect no error, got %v", req.Error)
    91  	}
    92  }
    93  
    94  func TestValidateCRC32AlreadyErrorSkip(t *testing.T) {
    95  	req := mockCRCResponse(db, 400, "{}", "1234")
    96  	if req.Error == nil {
    97  		t.Fatalf("expect error, but got none")
    98  	}
    99  
   100  	aerr := req.Error.(awserr.Error)
   101  	if aerr.Code() == "CRC32CheckFailed" {
   102  		t.Errorf("expect error code not to be CRC32CheckFailed")
   103  	}
   104  }
   105  
   106  func TestValidateCRC32IsValid(t *testing.T) {
   107  	req := mockCRCResponse(db, 200, `{"TableNames":["A"]}`, "3090163698")
   108  	if req.Error != nil {
   109  		t.Fatalf("expect no error, got %v", req.Error)
   110  	}
   111  
   112  	// CRC check does not affect output parsing
   113  	out := req.Data.(*dynamodb.ListTablesOutput)
   114  	if e, a := "A", *out.TableNames[0]; e != a {
   115  		t.Errorf("expect %q table name, got %q", e, a)
   116  	}
   117  }
   118  
   119  func TestValidateCRC32DoesNotMatch(t *testing.T) {
   120  	req := mockCRCResponse(db, 200, "{}", "1234")
   121  	if req.Error == nil {
   122  		t.Fatalf("expect error, but got none")
   123  	}
   124  	req.Handlers.Build.RemoveByName("crr.endpointdiscovery")
   125  
   126  	aerr := req.Error.(awserr.Error)
   127  	if e, a := "CRC32CheckFailed", aerr.Code(); e != a {
   128  		t.Errorf("expect %s error code, got %s", e, a)
   129  	}
   130  	if e, a := 2, req.RetryCount; e != a {
   131  		t.Errorf("expect %d retry count, got %d", e, a)
   132  	}
   133  }
   134  
   135  func TestValidateCRC32DoesNotMatchNoComputeChecksum(t *testing.T) {
   136  	svc := dynamodb.New(unit.Session, &aws.Config{
   137  		MaxRetries:              aws.Int(2),
   138  		DisableComputeChecksums: aws.Bool(true),
   139  	})
   140  	svc.Handlers.Send.Clear() // mock sending
   141  
   142  	req := mockCRCResponse(svc, 200, `{"TableNames":["A"]}`, "1234")
   143  	if req.Error != nil {
   144  		t.Fatalf("expect no error, got %v", req.Error)
   145  	}
   146  
   147  	if e, a := 0, req.RetryCount; e != a {
   148  		t.Errorf("expect %d retry count, got %d", e, a)
   149  	}
   150  
   151  	// CRC check disabled. Does not affect output parsing
   152  	out := req.Data.(*dynamodb.ListTablesOutput)
   153  	if e, a := "A", *out.TableNames[0]; e != a {
   154  		t.Errorf("expect %q table name, got %q", e, a)
   155  	}
   156  }