github.com/newrelic/go-agent@v3.26.0+incompatible/internal/cat/id_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 "testing" 8 ) 9 10 func TestIDHeaderUnmarshal(t *testing.T) { 11 // Test error cases where the output is errUnexpectedArraySize. 12 for _, input := range []string{ 13 ``, 14 `1234`, 15 `1234#5678#90`, 16 `foo`, 17 } { 18 _, err := NewIDHeader([]byte(input)) 19 if _, ok := err.(errUnexpectedArraySize); !ok { 20 t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err) 21 } 22 } 23 24 // Test error cases where the output is errInvalidAccountID. 25 for _, input := range []string{ 26 `#1234`, 27 `foo#bar`, 28 } { 29 if _, err := NewIDHeader([]byte(input)); err != errInvalidAccountID { 30 t.Errorf("given %s: error expected to be %v; got %v", input, errInvalidAccountID, err) 31 } 32 } 33 34 // Test success cases. 35 for _, test := range []struct { 36 input string 37 expected IDHeader 38 }{ 39 {`1234#`, IDHeader{1234, ""}}, 40 {`1234#5678`, IDHeader{1234, "5678"}}, 41 {`1234#blob`, IDHeader{1234, "blob"}}, 42 {`0#5678`, IDHeader{0, "5678"}}, 43 } { 44 id, err := NewIDHeader([]byte(test.input)) 45 46 if err != nil { 47 t.Errorf("given %s: error expected to be nil; got %v", test.input, err) 48 } 49 if test.expected.AccountID != id.AccountID { 50 t.Errorf("given %s: account ID expected to be %d; got %d", test.input, test.expected.AccountID, id.AccountID) 51 } 52 if test.expected.Blob != id.Blob { 53 t.Errorf("given %s: account ID expected to be %s; got %s", test.input, test.expected.Blob, id.Blob) 54 } 55 } 56 }