github.com/newrelic/go-agent@v3.26.0+incompatible/internal/utilities_test.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package internal
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestRemoveFirstSegment(t *testing.T) {
    13  	testcases := []struct {
    14  		input    string
    15  		expected string
    16  	}{
    17  		{input: "no_seperators", expected: "no_seperators"},
    18  		{input: "heyo/zip/zap", expected: "zip/zap"},
    19  		{input: "ends_in_slash/", expected: ""},
    20  		{input: "☃☃☃/✓✓✓/heyo", expected: "✓✓✓/heyo"},
    21  		{input: "☃☃☃/", expected: ""},
    22  		{input: "/", expected: ""},
    23  		{input: "", expected: ""},
    24  	}
    25  
    26  	for _, tc := range testcases {
    27  		out := removeFirstSegment(tc.input)
    28  		if out != tc.expected {
    29  			t.Fatal(tc.input, out, tc.expected)
    30  		}
    31  	}
    32  }
    33  
    34  func TestFloatSecondsToDuration(t *testing.T) {
    35  	if d := FloatSecondsToDuration(0.123); d != 123*time.Millisecond {
    36  		t.Error(d)
    37  	}
    38  	if d := FloatSecondsToDuration(456.0); d != 456*time.Second {
    39  		t.Error(d)
    40  	}
    41  }
    42  
    43  func TestAbsTimeDiff(t *testing.T) {
    44  	diff := 5 * time.Second
    45  	before := time.Now()
    46  	after := before.Add(5 * time.Second)
    47  
    48  	if out := absTimeDiff(before, after); out != diff {
    49  		t.Error(out, diff)
    50  	}
    51  	if out := absTimeDiff(after, before); out != diff {
    52  		t.Error(out, diff)
    53  	}
    54  	if out := absTimeDiff(after, after); out != 0 {
    55  		t.Error(out)
    56  	}
    57  }
    58  
    59  func TestTimeToFloatMilliseconds(t *testing.T) {
    60  	tm := time.Unix(123, 456789000)
    61  	if ms := timeToFloatMilliseconds(tm); ms != 123456.789 {
    62  		t.Error(ms)
    63  	}
    64  }
    65  
    66  func TestCompactJSON(t *testing.T) {
    67  	in := `
    68  	{   "zip":	1}`
    69  	out := CompactJSONString(in)
    70  	if out != `{"zip":1}` {
    71  		t.Fatal(in, out)
    72  	}
    73  }
    74  
    75  func TestGetContentLengthFromHeader(t *testing.T) {
    76  	// Nil header.
    77  	if cl := GetContentLengthFromHeader(nil); cl != -1 {
    78  		t.Errorf("unexpected content length: expected -1; got %d", cl)
    79  	}
    80  
    81  	// Empty header.
    82  	header := make(http.Header)
    83  	if cl := GetContentLengthFromHeader(header); cl != -1 {
    84  		t.Errorf("unexpected content length: expected -1; got %d", cl)
    85  	}
    86  
    87  	// Invalid header.
    88  	header.Set("Content-Length", "foo")
    89  	if cl := GetContentLengthFromHeader(header); cl != -1 {
    90  		t.Errorf("unexpected content length: expected -1; got %d", cl)
    91  	}
    92  
    93  	// Zero header.
    94  	header.Set("Content-Length", "0")
    95  	if cl := GetContentLengthFromHeader(header); cl != 0 {
    96  		t.Errorf("unexpected content length: expected 0; got %d", cl)
    97  	}
    98  
    99  	// Valid, non-zero header.
   100  	header.Set("Content-Length", "1024")
   101  	if cl := GetContentLengthFromHeader(header); cl != 1024 {
   102  		t.Errorf("unexpected content length: expected 1024; got %d", cl)
   103  	}
   104  }
   105  
   106  func TestStringLengthByteLimit(t *testing.T) {
   107  	testcases := []struct {
   108  		input  string
   109  		limit  int
   110  		expect string
   111  	}{
   112  		{"", 255, ""},
   113  		{"awesome", -1, ""},
   114  		{"awesome", 0, ""},
   115  		{"awesome", 1, "a"},
   116  		{"awesome", 7, "awesome"},
   117  		{"awesome", 20, "awesome"},
   118  		{"日本\x80語", 10, "日本\x80語"}, // bad unicode
   119  		{"日本", 1, ""},
   120  		{"日本", 2, ""},
   121  		{"日本", 3, "日"},
   122  		{"日本", 4, "日"},
   123  		{"日本", 5, "日"},
   124  		{"日本", 6, "日本"},
   125  		{"日本", 7, "日本"},
   126  	}
   127  
   128  	for _, tc := range testcases {
   129  		out := StringLengthByteLimit(tc.input, tc.limit)
   130  		if out != tc.expect {
   131  			t.Error(tc.input, tc.limit, tc.expect, out)
   132  		}
   133  	}
   134  }
   135  
   136  func TestTimeToAndFromUnixMilliseconds(t *testing.T) {
   137  	t1 := time.Date(2014, time.November, 28, 1, 1, 0, 0, time.UTC)
   138  	millis := TimeToUnixMilliseconds(t1)
   139  	if millis != 1417136460000 {
   140  		t.Fatal(millis)
   141  	}
   142  	t2 := timeFromUnixMilliseconds(millis)
   143  	if t1.UnixNano() != t2.UnixNano() {
   144  		t.Fatal(t1, t2)
   145  	}
   146  }
   147  
   148  func TestMinorVersion(t *testing.T) {
   149  	testcases := []struct {
   150  		input  string
   151  		expect string
   152  	}{
   153  		{"go1.13", "go1.13"},
   154  		{"go1.13.1", "go1.13"},
   155  		{"go1.13.1.0", "go1.13"},
   156  		{"purple", "purple"},
   157  	}
   158  
   159  	for _, test := range testcases {
   160  		if actual := MinorVersion(test.input); actual != test.expect {
   161  			t.Errorf("incorrect result: expect=%s actual=%s", test.expect, actual)
   162  		}
   163  	}
   164  }