github.com/newrelic/go-agent@v3.26.0+incompatible/internal/cat/appdata_test.go (about)

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