github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrawssdk/internal/internal_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	requestv2 "github.com/aws/aws-sdk-go-v2/aws"
    12  	restv2 "github.com/aws/aws-sdk-go-v2/private/protocol/rest"
    13  	"github.com/aws/aws-sdk-go-v2/service/lambda"
    14  	requestv1 "github.com/aws/aws-sdk-go/aws/request"
    15  	restv1 "github.com/aws/aws-sdk-go/private/protocol/rest"
    16  )
    17  
    18  func TestGetTableName(t *testing.T) {
    19  	str := "this is a string"
    20  	var emptyStr string
    21  	strPtr := &str
    22  	emptyStrPtr := &emptyStr
    23  
    24  	testcases := []struct {
    25  		params   interface{}
    26  		expected string
    27  	}{
    28  		{params: nil, expected: ""},
    29  		{params: str, expected: ""},
    30  		{params: strPtr, expected: ""},
    31  		{params: struct{ other string }{other: str}, expected: ""},
    32  		{params: &struct{ other string }{other: str}, expected: ""},
    33  		{params: struct{ TableName bool }{TableName: true}, expected: ""},
    34  		{params: &struct{ TableName bool }{TableName: true}, expected: ""},
    35  		{params: struct{ TableName string }{TableName: str}, expected: ""},
    36  		{params: &struct{ TableName string }{TableName: str}, expected: ""},
    37  		{params: struct{ TableName *string }{TableName: nil}, expected: ""},
    38  		{params: &struct{ TableName *string }{TableName: nil}, expected: ""},
    39  		{params: struct{ TableName *string }{TableName: emptyStrPtr}, expected: ""},
    40  		{params: &struct{ TableName *string }{TableName: emptyStrPtr}, expected: ""},
    41  		{params: struct{ TableName *string }{TableName: strPtr}, expected: ""},
    42  		{params: &struct{ TableName *string }{TableName: strPtr}, expected: str},
    43  	}
    44  
    45  	for i, test := range testcases {
    46  		if out := getTableName(test.params); test.expected != out {
    47  			t.Error(i, out, test.params, test.expected)
    48  		}
    49  	}
    50  }
    51  
    52  func TestGetRequestID(t *testing.T) {
    53  	primary := "X-Amzn-Requestid"
    54  	secondary := "X-Amz-Request-Id"
    55  
    56  	testcases := []struct {
    57  		hdr      http.Header
    58  		expected string
    59  	}{
    60  		{hdr: http.Header{
    61  			"hello": []string{"world"},
    62  		}, expected: ""},
    63  
    64  		{hdr: http.Header{
    65  			strings.ToUpper(primary): []string{"hello"},
    66  		}, expected: ""},
    67  
    68  		{hdr: http.Header{
    69  			primary: []string{"hello"},
    70  		}, expected: "hello"},
    71  
    72  		{hdr: http.Header{
    73  			secondary: []string{"hello"},
    74  		}, expected: "hello"},
    75  
    76  		{hdr: http.Header{
    77  			primary:   []string{"hello"},
    78  			secondary: []string{"world"},
    79  		}, expected: "hello"},
    80  	}
    81  
    82  	for i, test := range testcases {
    83  		if out := getRequestID(test.hdr); test.expected != out {
    84  			t.Error(i, out, test.hdr, test.expected)
    85  		}
    86  	}
    87  
    88  	// Make sure our assumptions still hold against aws-sdk-go
    89  	for _, test := range testcases {
    90  		req := &requestv1.Request{
    91  			HTTPResponse: &http.Response{
    92  				Header: test.hdr,
    93  			},
    94  		}
    95  		restv1.UnmarshalMeta(req)
    96  		if out := getRequestID(test.hdr); req.RequestID != out {
    97  			t.Error("requestId assumptions incorrect", out, req.RequestID,
    98  				test.hdr, test.expected)
    99  		}
   100  	}
   101  
   102  	// Make sure our assumptions still hold against aws-sdk-go-v2
   103  	for _, test := range testcases {
   104  		req := &requestv2.Request{
   105  			HTTPResponse: &http.Response{
   106  				Header: test.hdr,
   107  			},
   108  			Data: &lambda.InvokeOutput{},
   109  		}
   110  		restv2.UnmarshalMeta(req)
   111  		if out := getRequestID(test.hdr); req.RequestID != out {
   112  			t.Error("requestId assumptions incorrect", out, req.RequestID,
   113  				test.hdr, test.expected)
   114  		}
   115  	}
   116  }