github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/cat/appdata_test.go (about)

     1  package cat
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  func TestAppDataRoundTrip(t *testing.T) {
     9  	for _, test := range []struct {
    10  		json    string
    11  		appData AppDataHeader
    12  	}{
    13  		{
    14  			json: `["xpid","txn",1,2,4096,"guid",false]`,
    15  			appData: AppDataHeader{
    16  				CrossProcessID:        "xpid",
    17  				TransactionName:       "txn",
    18  				QueueTimeInSeconds:    1.0,
    19  				ResponseTimeInSeconds: 2.0,
    20  				ContentLength:         4096,
    21  				TransactionGUID:       "guid",
    22  			},
    23  		},
    24  	} {
    25  		// Test unmarshalling.
    26  		appData := &AppDataHeader{}
    27  		if err := json.Unmarshal([]byte(test.json), appData); err != nil {
    28  			t.Errorf("given %s: error expected to be nil; got %v", test.json, err)
    29  		}
    30  
    31  		if test.appData.CrossProcessID != appData.CrossProcessID {
    32  			t.Errorf("given %s: CrossProcessID expected to be %s; got %s", test.json, test.appData.CrossProcessID, appData.CrossProcessID)
    33  		}
    34  
    35  		if test.appData.TransactionName != appData.TransactionName {
    36  			t.Errorf("given %s: TransactionName expected to be %s; got %s", test.json, test.appData.TransactionName, appData.TransactionName)
    37  		}
    38  
    39  		if test.appData.QueueTimeInSeconds != appData.QueueTimeInSeconds {
    40  			t.Errorf("given %s: QueueTimeInSeconds expected to be %f; got %f", test.json, test.appData.QueueTimeInSeconds, appData.QueueTimeInSeconds)
    41  		}
    42  
    43  		if test.appData.ResponseTimeInSeconds != appData.ResponseTimeInSeconds {
    44  			t.Errorf("given %s: ResponseTimeInSeconds expected to be %f; got %f", test.json, test.appData.ResponseTimeInSeconds, appData.ResponseTimeInSeconds)
    45  		}
    46  
    47  		if test.appData.ContentLength != appData.ContentLength {
    48  			t.Errorf("given %s: ContentLength expected to be %d; got %d", test.json, test.appData.ContentLength, appData.ContentLength)
    49  		}
    50  
    51  		if test.appData.TransactionGUID != appData.TransactionGUID {
    52  			t.Errorf("given %s: TransactionGUID expected to be %s; got %s", test.json, test.appData.TransactionGUID, appData.TransactionGUID)
    53  		}
    54  
    55  		// Test marshalling.
    56  		data, err := json.Marshal(&test.appData)
    57  		if err != nil {
    58  			t.Errorf("given %s: error expected to be nil; got %v", test.json, err)
    59  		}
    60  
    61  		if string(data) != test.json {
    62  			t.Errorf("given %s: unexpected JSON %s", test.json, string(data))
    63  		}
    64  	}
    65  }
    66  
    67  func TestAppDataUnmarshal(t *testing.T) {
    68  	// Test error cases where we get a generic error from the JSON package.
    69  	for _, input := range []string{
    70  		// Basic malformed JSON test: beyond this, we're not going to unit test the
    71  		// Go standard library's JSON package.
    72  		``,
    73  	} {
    74  		appData := &AppDataHeader{}
    75  
    76  		if err := json.Unmarshal([]byte(input), appData); err == nil {
    77  			t.Errorf("given %s: error expected to be non-nil; got nil", input)
    78  		}
    79  	}
    80  
    81  	// Test error cases where a specific variable is returned.
    82  	for _, tc := range []struct {
    83  		input string
    84  		err   error
    85  	}{
    86  		// Unexpected JSON types.
    87  		{`false`, errInvalidAppDataJSON},
    88  		{`true`, errInvalidAppDataJSON},
    89  		{`1234`, errInvalidAppDataJSON},
    90  		{`{}`, errInvalidAppDataJSON},
    91  		{`""`, errInvalidAppDataJSON},
    92  
    93  		// Invalid data types for each field in turn.
    94  		{`[0,"txn",1.0,2.0,4096,"guid",false]`, errInvalidAppDataCrossProcessID},
    95  		{`["xpid",0,1.0,2.0,4096,"guid",false]`, errInvalidAppDataTransactionName},
    96  		{`["xpid","txn","queue",2.0,4096,"guid",false]`, errInvalidAppDataQueueTimeInSeconds},
    97  		{`["xpid","txn",1.0,"response",4096,"guid",false]`, errInvalidAppDataResponseTimeInSeconds},
    98  		{`["xpid","txn",1.0,2.0,"content length","guid",false]`, errInvalidAppDataContentLength},
    99  		{`["xpid","txn",1.0,2.0,4096,0,false]`, errInvalidAppDataTransactionGUID},
   100  	} {
   101  		appData := &AppDataHeader{}
   102  
   103  		if err := json.Unmarshal([]byte(tc.input), appData); err != tc.err {
   104  			t.Errorf("given %s: error expected to be %v; got %v", tc.input, tc.err, err)
   105  		}
   106  	}
   107  
   108  	// Test error cases where the incorrect number of elements was provided.
   109  	for _, input := range []string{
   110  		`[]`,
   111  		`[1,2,3,4,5,6]`,
   112  	} {
   113  		appData := &AppDataHeader{}
   114  
   115  		err := json.Unmarshal([]byte(input), appData)
   116  		if _, ok := err.(errUnexpectedArraySize); !ok {
   117  			t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err)
   118  		}
   119  	}
   120  }