Collector implementation
This commit is contained in:
114
cmd/collector/parser_test.go
Normal file
114
cmd/collector/parser_test.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseLine(t *testing.T) {
|
||||
good := "www.example.com\t1.2.3.4\t1741954800.123\tGET\t/api/v1/search?q=foo&x=1\t200\t1452\t0.043"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
wantOK bool
|
||||
want LogRecord
|
||||
}{
|
||||
{
|
||||
name: "normal IPv4 line strips query string",
|
||||
line: good,
|
||||
wantOK: true,
|
||||
want: LogRecord{
|
||||
Website: "www.example.com",
|
||||
ClientPrefix: "1.2.3.0/24",
|
||||
URI: "/api/v1/search",
|
||||
Status: "200",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "URI with no query string",
|
||||
line: "host\t10.0.0.1\t0\tPOST\t/submit\t201\t0\t0.001",
|
||||
wantOK: true,
|
||||
want: LogRecord{
|
||||
Website: "host",
|
||||
ClientPrefix: "10.0.0.0/24",
|
||||
URI: "/submit",
|
||||
Status: "201",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IPv6 address truncated to /48",
|
||||
line: "host\t2001:db8:cafe::1\t0\tGET\t/\t200\t0\t0.001",
|
||||
wantOK: true,
|
||||
want: LogRecord{
|
||||
Website: "host",
|
||||
ClientPrefix: "2001:db8:cafe::/48", // /48 = 3 full 16-bit groups intact
|
||||
URI: "/",
|
||||
Status: "200",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "too few fields returns false",
|
||||
line: "host\t1.2.3.4\t0\tGET\t/",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "empty line returns false",
|
||||
line: "",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "invalid IP returns false",
|
||||
line: "host\tnot-an-ip\t0\tGET\t/\t200\t0\t0.001",
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "status 429",
|
||||
line: "api.example.com\t5.6.7.8\t0\tGET\t/rate-limited\t429\t0\t0.001",
|
||||
wantOK: true,
|
||||
want: LogRecord{
|
||||
Website: "api.example.com",
|
||||
ClientPrefix: "5.6.7.0/24",
|
||||
URI: "/rate-limited",
|
||||
Status: "429",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, ok := ParseLine(tc.line, 24, 48)
|
||||
if ok != tc.wantOK {
|
||||
t.Fatalf("ParseLine ok=%v, want %v", ok, tc.wantOK)
|
||||
}
|
||||
if !tc.wantOK {
|
||||
return
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("got %+v, want %+v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
addr string
|
||||
want string
|
||||
}{
|
||||
{"1.2.3.4", "1.2.3.0/24"},
|
||||
{"192.168.100.200", "192.168.100.0/24"},
|
||||
{"2001:db8:cafe:babe::1", "2001:db8:cafe::/48"}, // /48 = 3 full groups intact
|
||||
{"::1", "::/48"}, // loopback — first 48 bits are all zero
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
got, ok := truncateIP(tc.addr, 24, 48)
|
||||
if !ok {
|
||||
t.Errorf("truncateIP(%q) returned not-ok", tc.addr)
|
||||
continue
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("truncateIP(%q) = %q, want %q", tc.addr, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user