File: utilities_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 (139 lines) | stat: -rw-r--r-- 3,309 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package newrelic

import (
	"net/http"
	"testing"
	"time"
)

func TestRemoveFirstSegment(t *testing.T) {
	testcases := []struct {
		input    string
		expected string
	}{
		{input: "no_seperators", expected: "no_seperators"},
		{input: "heyo/zip/zap", expected: "zip/zap"},
		{input: "ends_in_slash/", expected: ""},
		{input: "☃☃☃/✓✓✓/heyo", expected: "✓✓✓/heyo"},
		{input: "☃☃☃/", expected: ""},
		{input: "/", expected: ""},
		{input: "", expected: ""},
	}

	for _, tc := range testcases {
		out := removeFirstSegment(tc.input)
		if out != tc.expected {
			t.Fatal(tc.input, out, tc.expected)
		}
	}
}

func TestTimeToFloatMilliseconds(t *testing.T) {
	tm := time.Unix(123, 456789000)
	if ms := timeToFloatMilliseconds(tm); ms != 123456.789 {
		t.Error(ms)
	}
}

func TestCompactJSON(t *testing.T) {
	in := `
	{   "zip":	1}`
	out := compactJSONString(in)
	if out != `{"zip":1}` {
		t.Fatal(in, out)
	}
}

func TestGetContentLengthFromHeader(t *testing.T) {
	// Nil header.
	if cl := getContentLengthFromHeader(nil); cl != -1 {
		t.Errorf("unexpected content length: expected -1; got %d", cl)
	}

	// Empty header.
	header := make(http.Header)
	if cl := getContentLengthFromHeader(header); cl != -1 {
		t.Errorf("unexpected content length: expected -1; got %d", cl)
	}

	// Invalid header.
	header.Set("Content-Length", "foo")
	if cl := getContentLengthFromHeader(header); cl != -1 {
		t.Errorf("unexpected content length: expected -1; got %d", cl)
	}

	// Zero header.
	header.Set("Content-Length", "0")
	if cl := getContentLengthFromHeader(header); cl != 0 {
		t.Errorf("unexpected content length: expected 0; got %d", cl)
	}

	// Valid, non-zero header.
	header.Set("Content-Length", "1024")
	if cl := getContentLengthFromHeader(header); cl != 1024 {
		t.Errorf("unexpected content length: expected 1024; got %d", cl)
	}
}

func TestStringLengthByteLimit(t *testing.T) {
	testcases := []struct {
		input  string
		limit  int
		expect string
	}{
		{"", 255, ""},
		{"awesome", -1, ""},
		{"awesome", 0, ""},
		{"awesome", 1, "a"},
		{"awesome", 7, "awesome"},
		{"awesome", 20, "awesome"},
		{"日本\x80語", 10, "日本\x80語"}, // bad unicode
		{"日本", 1, ""},
		{"日本", 2, ""},
		{"日本", 3, "日"},
		{"日本", 4, "日"},
		{"日本", 5, "日"},
		{"日本", 6, "日本"},
		{"日本", 7, "日本"},
	}

	for _, tc := range testcases {
		out := stringLengthByteLimit(tc.input, tc.limit)
		if out != tc.expect {
			t.Error(tc.input, tc.limit, tc.expect, out)
		}
	}
}

func TestTimeToAndFromUnixMilliseconds(t *testing.T) {
	t1 := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
	millis := timeToUnixMilliseconds(t1)
	if millis != 1417136460000 {
		t.Fatal(millis)
	}
	t2 := timeFromUnixMilliseconds(millis)
	if t1.UnixNano() != t2.UnixNano() {
		t.Fatal(t1, t2)
	}
}

func TestMinorVersion(t *testing.T) {
	testcases := []struct {
		input  string
		expect string
	}{
		{"go1.13", "go1.13"},
		{"go1.13.1", "go1.13"},
		{"go1.13.1.0", "go1.13"},
		{"purple", "purple"},
	}

	for _, test := range testcases {
		if actual := minorVersion(test.input); actual != test.expect {
			t.Errorf("incorrect result: expect=%s actual=%s", test.expect, actual)
		}
	}
}