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

     1  package cat
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  func TestSyntheticsUnmarshalInvalid(t *testing.T) {
     9  	// Test error cases where we get a generic error from the JSON package.
    10  	for _, input := range []string{
    11  		// Basic malformed JSON test: beyond this, we're not going to unit test the
    12  		// Go standard library's JSON package.
    13  		``,
    14  	} {
    15  		synthetics := &SyntheticsHeader{}
    16  
    17  		if err := json.Unmarshal([]byte(input), synthetics); err == nil {
    18  			t.Errorf("given %s: error expected to be non-nil; got nil", input)
    19  		}
    20  	}
    21  
    22  	// Test error cases where the incorrect number of elements was provided.
    23  	for _, input := range []string{
    24  		`[]`,
    25  		`[1,2,3,4]`,
    26  	} {
    27  		synthetics := &SyntheticsHeader{}
    28  
    29  		err := json.Unmarshal([]byte(input), synthetics)
    30  		if _, ok := err.(errUnexpectedArraySize); !ok {
    31  			t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err)
    32  		}
    33  	}
    34  
    35  	// Test error cases with invalid version numbers.
    36  	for _, input := range []string{
    37  		`[0,1234,"resource","job","monitor"]`,
    38  		`[2,1234,"resource","job","monitor"]`,
    39  	} {
    40  		synthetics := &SyntheticsHeader{}
    41  
    42  		err := json.Unmarshal([]byte(input), synthetics)
    43  		if _, ok := err.(errUnexpectedSyntheticsVersion); !ok {
    44  			t.Errorf("given %s: error expected to be errUnexpectedSyntheticsVersion; got %v", input, err)
    45  		}
    46  	}
    47  
    48  	// Test error cases where a specific variable is returned.
    49  	for _, tc := range []struct {
    50  		input string
    51  		err   error
    52  	}{
    53  		// Unexpected JSON types.
    54  		{`false`, errInvalidSyntheticsJSON},
    55  		{`true`, errInvalidSyntheticsJSON},
    56  		{`1234`, errInvalidSyntheticsJSON},
    57  		{`{}`, errInvalidSyntheticsJSON},
    58  		{`""`, errInvalidSyntheticsJSON},
    59  
    60  		// Invalid data types for each field in turn.
    61  		{`["version",1234,"resource","job","monitor"]`, errInvalidSyntheticsVersion},
    62  		{`[1,"account","resource","job","monitor"]`, errInvalidSyntheticsAccountID},
    63  		{`[1,1234,0,"job","monitor"]`, errInvalidSyntheticsResourceID},
    64  		{`[1,1234,"resource",-1,"monitor"]`, errInvalidSyntheticsJobID},
    65  		{`[1,1234,"resource","job",false]`, errInvalidSyntheticsMonitorID},
    66  	} {
    67  		synthetics := &SyntheticsHeader{}
    68  
    69  		if err := json.Unmarshal([]byte(tc.input), synthetics); err != tc.err {
    70  			t.Errorf("given %s: error expected to be %v; got %v", tc.input, tc.err, err)
    71  		}
    72  	}
    73  }
    74  
    75  func TestSyntheticsUnmarshalValid(t *testing.T) {
    76  	for _, test := range []struct {
    77  		json       string
    78  		synthetics SyntheticsHeader
    79  	}{
    80  		{
    81  			json: `[1,1234,"resource","job","monitor"]`,
    82  			synthetics: SyntheticsHeader{
    83  				Version:    1,
    84  				AccountID:  1234,
    85  				ResourceID: "resource",
    86  				JobID:      "job",
    87  				MonitorID:  "monitor",
    88  			},
    89  		},
    90  	} {
    91  		// Test unmarshalling.
    92  		synthetics := &SyntheticsHeader{}
    93  		if err := json.Unmarshal([]byte(test.json), synthetics); err != nil {
    94  			t.Errorf("given %s: error expected to be nil; got %v", test.json, err)
    95  		}
    96  
    97  		if test.synthetics.Version != synthetics.Version {
    98  			t.Errorf("given %s: Version expected to be %d; got %d", test.json, test.synthetics.Version, synthetics.Version)
    99  		}
   100  
   101  		if test.synthetics.AccountID != synthetics.AccountID {
   102  			t.Errorf("given %s: AccountID expected to be %d; got %d", test.json, test.synthetics.AccountID, synthetics.AccountID)
   103  		}
   104  
   105  		if test.synthetics.ResourceID != synthetics.ResourceID {
   106  			t.Errorf("given %s: ResourceID expected to be %s; got %s", test.json, test.synthetics.ResourceID, synthetics.ResourceID)
   107  		}
   108  
   109  		if test.synthetics.JobID != synthetics.JobID {
   110  			t.Errorf("given %s: JobID expected to be %s; got %s", test.json, test.synthetics.JobID, synthetics.JobID)
   111  		}
   112  
   113  		if test.synthetics.MonitorID != synthetics.MonitorID {
   114  			t.Errorf("given %s: MonitorID expected to be %s; got %s", test.json, test.synthetics.MonitorID, synthetics.MonitorID)
   115  		}
   116  	}
   117  }