github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/utilities_test.go (about) 1 package internal 2 3 import ( 4 "net/http" 5 "testing" 6 "time" 7 ) 8 9 func TestRemoveFirstSegment(t *testing.T) { 10 testcases := []struct { 11 input string 12 expected string 13 }{ 14 {input: "no_seperators", expected: "no_seperators"}, 15 {input: "heyo/zip/zap", expected: "zip/zap"}, 16 {input: "ends_in_slash/", expected: ""}, 17 {input: "☃☃☃/✓✓✓/heyo", expected: "✓✓✓/heyo"}, 18 {input: "☃☃☃/", expected: ""}, 19 {input: "/", expected: ""}, 20 {input: "", expected: ""}, 21 } 22 23 for _, tc := range testcases { 24 out := removeFirstSegment(tc.input) 25 if out != tc.expected { 26 t.Fatal(tc.input, out, tc.expected) 27 } 28 } 29 } 30 31 func TestFloatSecondsToDuration(t *testing.T) { 32 if d := floatSecondsToDuration(0.123); d != 123*time.Millisecond { 33 t.Error(d) 34 } 35 if d := floatSecondsToDuration(456.0); d != 456*time.Second { 36 t.Error(d) 37 } 38 } 39 40 func TestAbsTimeDiff(t *testing.T) { 41 diff := 5 * time.Second 42 before := time.Now() 43 after := before.Add(5 * time.Second) 44 45 if out := absTimeDiff(before, after); out != diff { 46 t.Error(out, diff) 47 } 48 if out := absTimeDiff(after, before); out != diff { 49 t.Error(out, diff) 50 } 51 if out := absTimeDiff(after, after); out != 0 { 52 t.Error(out) 53 } 54 } 55 56 func TestTimeToFloatMilliseconds(t *testing.T) { 57 tm := time.Unix(123, 456789000) 58 if ms := timeToFloatMilliseconds(tm); ms != 123456.789 { 59 t.Error(ms) 60 } 61 } 62 63 func TestCompactJSON(t *testing.T) { 64 in := ` 65 { "zip": 1}` 66 out := CompactJSONString(in) 67 if out != `{"zip":1}` { 68 t.Fatal(in, out) 69 } 70 } 71 72 func TestGetContentLengthFromHeader(t *testing.T) { 73 // Nil header. 74 if cl := GetContentLengthFromHeader(nil); cl != -1 { 75 t.Errorf("unexpected content length: expected -1; got %d", cl) 76 } 77 78 // Empty header. 79 header := make(http.Header) 80 if cl := GetContentLengthFromHeader(header); cl != -1 { 81 t.Errorf("unexpected content length: expected -1; got %d", cl) 82 } 83 84 // Invalid header. 85 header.Set("Content-Length", "foo") 86 if cl := GetContentLengthFromHeader(header); cl != -1 { 87 t.Errorf("unexpected content length: expected -1; got %d", cl) 88 } 89 90 // Zero header. 91 header.Set("Content-Length", "0") 92 if cl := GetContentLengthFromHeader(header); cl != 0 { 93 t.Errorf("unexpected content length: expected 0; got %d", cl) 94 } 95 96 // Valid, non-zero header. 97 header.Set("Content-Length", "1024") 98 if cl := GetContentLengthFromHeader(header); cl != 1024 { 99 t.Errorf("unexpected content length: expected 1024; got %d", cl) 100 } 101 } 102 103 func TestStringLengthByteLimit(t *testing.T) { 104 testcases := []struct { 105 input string 106 limit int 107 expect string 108 }{ 109 {"", 255, ""}, 110 {"awesome", -1, ""}, 111 {"awesome", 0, ""}, 112 {"awesome", 1, "a"}, 113 {"awesome", 7, "awesome"}, 114 {"awesome", 20, "awesome"}, 115 {"日本\x80語", 10, "日本\x80語"}, // bad unicode 116 {"日本", 1, ""}, 117 {"日本", 2, ""}, 118 {"日本", 3, "日"}, 119 {"日本", 4, "日"}, 120 {"日本", 5, "日"}, 121 {"日本", 6, "日本"}, 122 {"日本", 7, "日本"}, 123 } 124 125 for _, tc := range testcases { 126 out := StringLengthByteLimit(tc.input, tc.limit) 127 if out != tc.expect { 128 t.Error(tc.input, tc.limit, tc.expect, out) 129 } 130 } 131 }