File: id_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 1,517 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package cat

import (
	"testing"
)

func TestIDHeaderUnmarshal(t *testing.T) {
	// Test error cases where the output is errUnexpectedArraySize.
	for _, input := range []string{
		``,
		`1234`,
		`1234#5678#90`,
		`foo`,
	} {
		_, err := NewIDHeader([]byte(input))
		if _, ok := err.(errUnexpectedArraySize); !ok {
			t.Errorf("given %s: error expected to be errUnexpectedArraySize; got %v", input, err)
		}
	}

	// Test error cases where the output is errInvalidAccountID.
	for _, input := range []string{
		`#1234`,
		`foo#bar`,
	} {
		if _, err := NewIDHeader([]byte(input)); err != errInvalidAccountID {
			t.Errorf("given %s: error expected to be %v; got %v", input, errInvalidAccountID, err)
		}
	}

	// Test success cases.
	for _, test := range []struct {
		input    string
		expected IDHeader
	}{
		{`1234#`, IDHeader{1234, ""}},
		{`1234#5678`, IDHeader{1234, "5678"}},
		{`1234#blob`, IDHeader{1234, "blob"}},
		{`0#5678`, IDHeader{0, "5678"}},
	} {
		id, err := NewIDHeader([]byte(test.input))

		if err != nil {
			t.Errorf("given %s: error expected to be nil; got %v", test.input, err)
		}
		if test.expected.AccountID != id.AccountID {
			t.Errorf("given %s: account ID expected to be %d; got %d", test.input, test.expected.AccountID, id.AccountID)
		}
		if test.expected.Blob != id.Blob {
			t.Errorf("given %s: account ID expected to be %s; got %s", test.input, test.expected.Blob, id.Blob)
		}
	}
}