Skip to content

Commit 1ec74d5

Browse files
committed
Add tests
1 parent f3aae10 commit 1ec74d5

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

internal/funnel/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ func (r *CaptureRequestResponse) Type() string {
134134
if len(parts) > 0 {
135135
return parts[0]
136136
}
137-
return "unknown"
137+
return ""
138138
}
139139

140140
func (r *CaptureRequestResponse) RoundedDuration() string {
141-
if r.Duration.Seconds() > 1 {
141+
if r.Duration.Seconds() >= 1 {
142142
// we want one decimal place
143143
return fmt.Sprintf("%.1fs", r.Duration.Seconds())
144144
}

internal/funnel/types_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,67 @@ func TestRequestList_Add(t *testing.T) {
200200

201201
})
202202
}
203+
204+
func TestCaptureRequestResponse_RoundedDuration(t *testing.T) {
205+
testCases := []struct {
206+
name string
207+
duration time.Duration
208+
expected string
209+
}{
210+
{"Zero duration", 0, "0ms"},
211+
{"Milliseconds", 123 * time.Millisecond, "123ms"},
212+
{"Half second", 500 * time.Millisecond, "500ms"},
213+
{"Just under 1 second", 999 * time.Millisecond, "999ms"},
214+
{"Exactly 1 second", 1 * time.Second, "1.0s"},
215+
{"1.2 seconds", 1200 * time.Millisecond, "1.2s"},
216+
{"1.23 seconds (rounds)", 1234 * time.Millisecond, "1.2s"}, // Should round based on fmt.Sprintf
217+
{"Long duration", 5*time.Minute + 30*time.Second + 456*time.Millisecond, "330.5s"},
218+
}
219+
220+
for _, tc := range testCases {
221+
t.Run(tc.name, func(t *testing.T) {
222+
crr := CaptureRequestResponse{Duration: tc.duration}
223+
if got := crr.RoundedDuration(); got != tc.expected {
224+
t.Errorf("Expected RoundedDuration() to be %q, got %q", tc.expected, got)
225+
}
226+
})
227+
}
228+
}
229+
230+
func TestCaptureRequestResponse_Type(t *testing.T) {
231+
testCases := []struct {
232+
name string
233+
contentType string
234+
expected string
235+
}{
236+
{"No Content-Type", "", ""},
237+
{"JSON", "application/json", "json"},
238+
{"JSON with charset", "application/json; charset=utf-8", "json"},
239+
{"HTML", "text/html", "html"},
240+
{"XML", "text/xml", "xml"},
241+
{"CSS", "text/css", "css"},
242+
{"JavaScript", "text/javascript", "js"},
243+
{"Plain Text", "text/plain", "txt"},
244+
{"Image PNG", "image/png", "image"},
245+
{"Video MP4", "video/mp4", "video"},
246+
{"Application Octet Stream", "application/octet-stream", "application"},
247+
{"Weird format", "foo/bar", "foo"},
248+
{"Only main type", "audio", "audio"},
249+
{"Empty string after split", "/", ""}, // Invalid Content-Type
250+
}
251+
252+
for _, tc := range testCases {
253+
t.Run(tc.name, func(t *testing.T) {
254+
headers := make(map[string]string)
255+
if tc.contentType != "" {
256+
headers["Content-Type"] = tc.contentType
257+
}
258+
crr := CaptureRequestResponse{
259+
Response: CaptureResponse{Headers: headers},
260+
}
261+
if got := crr.Type(); got != tc.expected {
262+
t.Errorf("Expected Type() to be %q for Content-Type %q, got %q", tc.expected, tc.contentType, got)
263+
}
264+
})
265+
}
266+
}

0 commit comments

Comments
 (0)