check / check (push) Waiting to run
ratelimit.New takes ratelimit.Params (Limits, MaxClients) in place of two arguments by position. The process logger moves out of requestlog into internal/processlog, whose New takes Params (Output, InstanceName, Level); its two tests move with it unchanged. geojs.GeoJS keeps its params whole, as DNSBL, AbuseIPDB and routewatch.Client do. SPEC and README list the new package. Model: opus-5-5
135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
package requestlog_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
func TestWriteWritesOneJSONLineMarkedRequest(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
err := requestlog.Write(&out, &requestlog.Line{
|
|
Time: requestlog.FormatTime(
|
|
time.Date(2026, 10, 3, 12, 0, 0, 0, time.UTC),
|
|
),
|
|
ClientIP: "203.0.113.9",
|
|
Status: 200,
|
|
Action: requestlog.ActionForward,
|
|
DurationTotalMS: requestlog.Milliseconds(1500 * time.Microsecond),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("write: %v", err)
|
|
}
|
|
|
|
text := out.String()
|
|
if strings.Count(text, "\n") != 1 || !strings.HasSuffix(text, "\n") {
|
|
t.Fatalf("wrote %q, want one line", text)
|
|
}
|
|
|
|
var fields map[string]any
|
|
|
|
err = json.Unmarshal(out.Bytes(), &fields)
|
|
if err != nil {
|
|
t.Fatalf("decode %q: %v", text, err)
|
|
}
|
|
|
|
want := map[string]any{
|
|
"type": "request", "time": "2026-10-03T12:00:00.000Z",
|
|
"client_ip": "203.0.113.9", "status": 200.0, "action": "forward",
|
|
"duration_total_ms": 1.5,
|
|
}
|
|
for name, value := range want {
|
|
if fields[name] != value {
|
|
t.Errorf("%s is %v, want %v", name, fields[name], value)
|
|
}
|
|
}
|
|
|
|
unset := []string{
|
|
"forwarded_for", "content_type", "content_length", "request_headers",
|
|
"has_authorization", "has_cookie", "websocket", "response_content_type",
|
|
"upstream_status", "cache_control", "location", "aborted", "counts",
|
|
"limit_hit", "offence", "ban_expires", "duration_checks_ms",
|
|
"duration_upstream_connect", "duration_upstream_first_byte",
|
|
"duration_upstream_total_ms",
|
|
}
|
|
for _, name := range unset {
|
|
_, present := fields[name]
|
|
if present {
|
|
t.Errorf("%s is there with no value to give", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRequestLinesHoldTheQueryAndRefererAsSent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
err := requestlog.Write(&out, &requestlog.Line{
|
|
Query: "a=1&b=<2>",
|
|
Referer: "https://example.com/?x=1&y=<2>",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("write: %v", err)
|
|
}
|
|
|
|
err = requestlog.WriteRProxyLine(
|
|
&out,
|
|
&requestlog.RProxyLine{Query: "a=1&b=<2>"},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("write the rproxy line: %v", err)
|
|
}
|
|
|
|
lines := strings.Split(strings.TrimSuffix(out.String(), "\n"), "\n")
|
|
if len(lines) != 2 {
|
|
t.Fatalf("wrote %q, want two lines", out.String())
|
|
}
|
|
|
|
for _, line := range lines {
|
|
if !json.Valid([]byte(line)) {
|
|
t.Errorf("%s is not valid JSON", line)
|
|
}
|
|
|
|
if !strings.Contains(line, `"query":"a=1&b=<2>"`) {
|
|
t.Errorf("%s does not hold the query as sent", line)
|
|
}
|
|
}
|
|
|
|
if !strings.Contains(
|
|
lines[0],
|
|
`"referer":"https://example.com/?x=1&y=<2>"`,
|
|
) {
|
|
t.Errorf("%s does not hold the referer as sent", lines[0])
|
|
}
|
|
}
|
|
|
|
func TestWriteRProxyLineWritesOneJSONLineWithOnlyItsFields(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var out bytes.Buffer
|
|
|
|
err := requestlog.WriteRProxyLine(&out, &requestlog.RProxyLine{
|
|
RequestID: "an-id",
|
|
Disposition: "allow",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("write: %v", err)
|
|
}
|
|
|
|
const want = `{"type":"request","time":"","request_id":"an-id","client_ip":"",` +
|
|
`"host":"","method":"","path":"","query":"","status":0,"request_bytes":0,` +
|
|
`"response_bytes":0,"duration_request_ms":0,"duration_response_ms":0,` +
|
|
`"disposition":"allow"}` + "\n"
|
|
if out.String() != want {
|
|
t.Errorf("wrote %s, want %s", out.String(), want)
|
|
}
|
|
}
|