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