-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhighlight_test.go
More file actions
180 lines (150 loc) · 5.04 KB
/
highlight_test.go
File metadata and controls
180 lines (150 loc) · 5.04 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"flag"
"strings"
"testing"
"time"
)
// Tests for header highlighting
func TestHighlightHeadersRequest(t *testing.T) {
headers := []byte("POST /foo HTTP/1.1\r\nHost: example.com\r\n\r\n")
out := string(highlightHeaders(headers, true))
if !strings.Contains(out, colorMethod) || !strings.Contains(out, colorURL) {
t.Errorf("request line not highlighted: %q", out)
}
if !strings.Contains(out, colorHeader) {
t.Errorf("header key not highlighted: %q", out)
}
}
func TestHighlightHeadersResponse(t *testing.T) {
headers := []byte("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n")
out := string(highlightHeaders(headers, false))
if !strings.Contains(out, colorStatus4xx) {
t.Errorf("status not colorized: %q", out)
}
if !strings.Contains(out, colorHeader) {
t.Errorf("header key not highlighted: %q", out)
}
}
func TestHighlightHeadersWithColorsDisabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = true
headers := []byte("POST /foo HTTP/1.1\r\nHost: example.com\r\n\r\n")
out := string(highlightHeaders(headers, true))
if strings.Contains(out, colorMethod) || strings.Contains(out, colorURL) || strings.Contains(out, colorHeader) {
t.Errorf("headers with no-color should not contain color codes: %q", out)
}
// Should still contain the actual header content
if !strings.Contains(out, "POST /foo HTTP/1.1") || !strings.Contains(out, "Host: example.com") {
t.Errorf("headers should still contain the data: %q", out)
}
}
// Tests for status code coloring
func TestColorStatus(t *testing.T) {
if colorStatus(101) != colorTime {
t.Errorf("expected 1xx color")
}
if colorStatus(201) != colorStatus2xx {
t.Errorf("expected 2xx color")
}
if colorStatus(302) != colorStatus3xx {
t.Errorf("expected 3xx color")
}
if colorStatus(404) != colorStatus4xx {
t.Errorf("expected 4xx color")
}
if colorStatus(500) != colorStatus5xx {
t.Errorf("expected 5xx color")
}
}
// Tests for color wrapping utility
func TestWrapColorWithColorsEnabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
// Initialize flag if not already done
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
// Set no-color to false (colors enabled)
*noColor = false
result := wrapColor("test", colorString)
expected := colorString + "test" + colorReset
if result != expected {
t.Errorf("wrapColor with colors enabled: got %q, want %q", result, expected)
}
}
func TestWrapColorWithColorsDisabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
// Initialize flag if not already done
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
// Set no-color to true (colors disabled)
*noColor = true
result := wrapColor("test", colorString)
expected := "test"
if result != expected {
t.Errorf("wrapColor with colors disabled: got %q, want %q", result, expected)
}
}
// Tests for time formatting
func TestColoredTimeWithColorsEnabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = false
testTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
result := coloredTime(testTime, colorTime)
if !strings.Contains(result, colorTime) {
t.Errorf("coloredTime with colors enabled should contain color codes: %q", result)
}
if !strings.Contains(result, "[2023/01/01 12:00:00]") {
t.Errorf("coloredTime should contain formatted time: %q", result)
}
}
func TestColoredTimeWithColorsDisabled(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = true
testTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
result := coloredTime(testTime, colorTime)
expected := "[2023/01/01 12:00:00]"
if result != expected {
t.Errorf("coloredTime with colors disabled: got %q, want %q", result, expected)
}
if strings.Contains(result, colorTime) {
t.Errorf("coloredTime with colors disabled should not contain color codes: %q", result)
}
}
func TestColoredTimeWithCustomColor(t *testing.T) {
// Save original state
originalNoColor := noColor
if noColor == nil {
noColor = flag.Bool("no-color", false, "disable colored output")
}
defer func() { noColor = originalNoColor }()
*noColor = false
testTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
result := coloredTime(testTime, colorReqMarker)
if !strings.Contains(result, colorReqMarker) {
t.Errorf("coloredTime with custom color should contain color codes: %q", result)
}
if !strings.Contains(result, "[2023/01/01 12:00:00]") {
t.Errorf("coloredTime should contain formatted time: %q", result)
}
}