-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathprogress_test.go
More file actions
262 lines (214 loc) · 8.08 KB
/
progress_test.go
File metadata and controls
262 lines (214 loc) · 8.08 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package progress
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func executeProgressRequest(path string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, path, nil)
rr := httptest.NewRecorder()
Progress(rr, req)
return rr
}
func executeProgressRequestWithMethod(method string, path string) *httptest.ResponseRecorder {
req := httptest.NewRequest(method, path, nil)
rr := httptest.NewRecorder()
Progress(rr, req)
return rr
}
func TestPickColorThresholds(t *testing.T) {
if got := pickColor(10, "", "", ""); got != red {
t.Fatalf("expected red for low percentage, got %q", got)
}
if got := pickColor(50, "", "", ""); got != yellow {
t.Fatalf("expected yellow for medium percentage, got %q", got)
}
if got := pickColor(80, "", "", ""); got != green {
t.Fatalf("expected green for high percentage, got %q", got)
}
}
func TestPickColorWithCustomColors(t *testing.T) {
if got := pickColor(10, "#00aa00", "#ffcc00", "#aa0000"); got != "#aa0000" {
t.Fatalf("expected custom danger color, got %q", got)
}
if got := pickColor(50, "#00aa00", "#ffcc00", "#aa0000"); got != "#ffcc00" {
t.Fatalf("expected custom warning color, got %q", got)
}
if got := pickColor(80, "#00aa00", "#ffcc00", "#aa0000"); got != "#00aa00" {
t.Fatalf("expected custom success color, got %q", got)
}
}
func TestProgressReturnsSVGForValidInput(t *testing.T) {
rr := executeProgressRequest("/progress/76")
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rr.Code)
}
if got := rr.Header().Get("Content-Type"); got != "image/svg+xml" {
t.Fatalf("expected image/svg+xml content type, got %q", got)
}
if got := rr.Header().Get("Cache-Control"); got != cacheControlValue {
t.Fatalf("expected cache control %q, got %q", cacheControlValue, got)
}
body := rr.Body.String()
if !strings.Contains(body, "76%") {
t.Fatalf("expected percentage text in SVG body, got %q", body)
}
if strings.Contains(body, "%%") {
t.Fatalf("expected single percent symbol in SVG body, got %q", body)
}
if !strings.Contains(body, `width="68" height="20" fill="#5cb85c"`) {
t.Fatalf("expected progress width/color in SVG body, got %q", body)
}
}
func TestProgressSupportsFloatPercentages(t *testing.T) {
rr := executeProgressRequest("/progress/76.5")
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rr.Code)
}
body := rr.Body.String()
if !strings.Contains(body, "76.5%") {
t.Fatalf("expected decimal percentage in body, got %q", body)
}
if !strings.Contains(body, `width="68" height="20" fill="#5cb85c"`) {
t.Fatalf("expected decimal percentage width/color in SVG body, got %q", body)
}
}
func TestProgressSupportsDataBarMode(t *testing.T) {
rr := executeProgressRequest("/progress/186?label=186&min=0&max=241&barColor=4472C4")
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rr.Code)
}
body := rr.Body.String()
if !strings.Contains(body, "186") {
t.Fatalf("expected custom label in body, got %q", body)
}
if !strings.Contains(body, `width="69" height="20" fill="#4472c4"`) {
t.Fatalf("expected scaled width/custom color in body, got %q", body)
}
}
func TestProgressDataBarDefaultsLabelToRawValue(t *testing.T) {
rr := executeProgressRequest("/progress/50?min=0&max=200")
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rr.Code)
}
body := rr.Body.String()
if !strings.Contains(body, "50\n") || strings.Contains(body, "50%") {
t.Fatalf("expected raw value label in body, got %q", body)
}
if !strings.Contains(body, `width="22" height="20" fill="#d9534f"`) {
t.Fatalf("expected scaled width and threshold color in body, got %q", body)
}
}
func TestProgressSupportsHEAD(t *testing.T) {
rr := executeProgressRequestWithMethod(http.MethodHead, "/progress/76")
if rr.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rr.Code)
}
if got := rr.Header().Get("Content-Type"); got != "image/svg+xml" {
t.Fatalf("expected image/svg+xml content type, got %q", got)
}
if got := rr.Header().Get("Cache-Control"); got != cacheControlValue {
t.Fatalf("expected cache control %q, got %q", cacheControlValue, got)
}
if rr.Body.Len() != 0 {
t.Fatalf("expected empty body for HEAD, got %q", rr.Body.String())
}
}
func TestProgressRejectsUnsupportedMethod(t *testing.T) {
rr := executeProgressRequestWithMethod(http.MethodPost, "/progress/50")
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("expected status 405, got %d", rr.Code)
}
if got := rr.Header().Get("Allow"); got != "GET, HEAD" {
t.Fatalf("expected Allow header for GET and HEAD, got %q", got)
}
}
func TestProgressReturnsBadRequestForInvalidPercentage(t *testing.T) {
rr := executeProgressRequest("/progress/not-a-number")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "percentage must be a number") {
t.Fatalf("expected invalid percentage message, got %q", rr.Body.String())
}
}
func TestProgressReturnsBadRequestForInvalidColor(t *testing.T) {
rr := executeProgressRequest("/progress/50?successColor=nothex")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "successColor must be a 6-character hex value") {
t.Fatalf("expected invalid color message, got %q", rr.Body.String())
}
}
func TestProgressReturnsBadRequestForInvalidBarColor(t *testing.T) {
rr := executeProgressRequest("/progress/50?barColor=nothex")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "barColor must be a 6-character hex value") {
t.Fatalf("expected invalid bar color message, got %q", rr.Body.String())
}
}
func TestProgressRequiresMinAndMaxTogether(t *testing.T) {
rr := executeProgressRequest("/progress/50?min=0")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "min and max must be provided together") {
t.Fatalf("expected min/max pair message, got %q", rr.Body.String())
}
}
func TestProgressRejectsInvalidMinMaxValues(t *testing.T) {
rr := executeProgressRequest("/progress/50?min=abc&max=100")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "min and max must be numeric values") {
t.Fatalf("expected min/max numeric message, got %q", rr.Body.String())
}
}
func TestProgressRejectsInvalidRangeOrder(t *testing.T) {
rr := executeProgressRequest("/progress/50?min=10&max=10")
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "max must be greater than min") {
t.Fatalf("expected max greater than min message, got %q", rr.Body.String())
}
}
func TestProgressRejectsTooLongLabel(t *testing.T) {
tooLongLabel := strings.Repeat("x", 65)
rr := executeProgressRequest("/progress/50?label=" + tooLongLabel)
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected status 400, got %d", rr.Code)
}
if !strings.Contains(rr.Body.String(), "label is too long") {
t.Fatalf("expected label length message, got %q", rr.Body.String())
}
}
func TestProgressClampsPercentageToRange(t *testing.T) {
high := executeProgressRequest("/progress/150")
if high.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", high.Code)
}
highBody := high.Body.String()
if !strings.Contains(highBody, "100%") {
t.Fatalf("expected clamped high percentage in body, got %q", highBody)
}
if !strings.Contains(highBody, `width="90" height="20" fill="#5cb85c"`) {
t.Fatalf("expected full width for clamped high percentage, got %q", highBody)
}
low := executeProgressRequest("/progress/-10")
if low.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", low.Code)
}
lowBody := low.Body.String()
if !strings.Contains(lowBody, "0%") {
t.Fatalf("expected clamped low percentage in body, got %q", lowBody)
}
if !strings.Contains(lowBody, `width="0" height="20" fill="#d9534f"`) {
t.Fatalf("expected empty width for clamped low percentage, got %q", lowBody)
}
}