Files
nixevol fe24a88bac feat: 统计页支持一年时段和按日趋势
在线人数在新的有效访问后尽快刷新,历史查询保留一年并画出每日连接数。
2026-09-28 11:08:28 +08:00

184 lines
5.6 KiB
Go

package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
)
func TestParseRange(t *testing.T) {
now := time.Date(2026, 9, 23, 11, 30, 0, 0, cst)
earliest := now.Add(-retainDuration)
t.Run("bad time", func(t *testing.T) {
_, _, err := parseRange("nope", now.Format(time.RFC3339), now)
if err == nil || err.Code != "bad_time" || err.Message != "时间格式不正确" {
t.Fatalf("%+v", err)
}
})
t.Run("bad order", func(t *testing.T) {
_, _, err := parseRange(now.Format(time.RFC3339), now.Add(-time.Hour).Format(time.RFC3339), now)
if err == nil || err.Code != "bad_order" {
t.Fatalf("%+v", err)
}
_, _, err = parseRange(now.Add(-time.Hour).Format(time.RFC3339), now.Add(-time.Hour).Format(time.RFC3339), now)
if err == nil || err.Code != "bad_order" || err.Message != "起点必须早于终点,且不能晚于当前时间" {
t.Fatalf("%+v", err)
}
})
t.Run("future start", func(t *testing.T) {
_, _, err := parseRange(now.Add(time.Hour).Format(time.RFC3339), now.Add(2*time.Hour).Format(time.RFC3339), now)
if err == nil || err.Code != "bad_order" {
t.Fatalf("%+v", err)
}
})
t.Run("too long", func(t *testing.T) {
_, _, err := parseRange(now.Add(-retainDuration-time.Hour).Format(time.RFC3339), now.Format(time.RFC3339), now)
if err == nil || err.Code != "range_too_long" || err.Message != "时段不能超过 1 年" {
t.Fatalf("%+v", err)
}
})
t.Run("exactly 72h", func(t *testing.T) {
_, _, err := parseRange(earliest.Format(time.RFC3339), now.Format(time.RFC3339), now)
if err != nil {
t.Fatal(err)
}
})
t.Run("truncate future end", func(t *testing.T) {
_, end, err := parseRange(now.Add(-time.Hour).Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339), now)
if err != nil {
t.Fatal(err)
}
if !end.Equal(now) {
t.Fatalf("end %s", end)
}
})
t.Run("start 3 min early", func(t *testing.T) {
start, end, err := parseRange(earliest.Add(-3*time.Minute).Format(time.RFC3339), now.Add(-3*time.Minute).Format(time.RFC3339), now)
if err != nil {
t.Fatal(err)
}
if !start.Equal(earliest) {
t.Fatalf("start %s want %s", start, earliest)
}
if !end.Equal(now.Add(-3 * time.Minute)) {
t.Fatalf("end %s", end)
}
})
t.Run("start 10 min early", func(t *testing.T) {
_, _, err := parseRange(earliest.Add(-10*time.Minute).Format(time.RFC3339), now.Add(-10*time.Minute).Format(time.RFC3339), now)
if err == nil || err.Code != "range_too_old" {
t.Fatalf("%+v", err)
}
if err.Earliest != formatRFC3339(earliest) {
t.Fatalf("earliest %s", err.Earliest)
}
want := "最早只能查到 " + formatChinese(earliest)
if err.Message != want {
t.Fatalf("message %q", err.Message)
}
})
t.Run("space becomes plus", func(t *testing.T) {
start, end, err := parseRange("2026-09-23T10:00:00 08:00", "2026-09-23T11:00:00 08:00", now)
if err != nil {
t.Fatal(err)
}
if start.Hour() != 10 || end.Hour() != 11 {
t.Fatalf("start %s end %s", start, end)
}
})
}
func TestRangeHTTP(t *testing.T) {
now := time.Date(2026, 9, 23, 11, 30, 0, 0, cst)
app := newTestApp(t, func() time.Time { return now })
earliest := now.Add(-retainDuration)
call := func(raw string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodGet, "/api/range?"+raw, nil)
rr := httptest.NewRecorder()
app.Handler().ServeHTTP(rr, req)
return rr
}
decodeErr := func(rr *httptest.ResponseRecorder) errJSON {
t.Helper()
var body errJSON
if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
return body
}
rr := call("start=foo&end=bar")
if rr.Code != 400 || decodeErr(rr).Error != "bad_time" {
t.Fatalf("%d %s", rr.Code, rr.Body)
}
if ct := rr.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") {
t.Fatalf("content-type %s", ct)
}
if rr.Header().Get("Cache-Control") != "no-store" {
t.Fatal(rr.Header().Get("Cache-Control"))
}
q := url.Values{}
q.Set("start", now.Add(-retainDuration-time.Hour).Format(time.RFC3339))
q.Set("end", now.Format(time.RFC3339))
rr = call(q.Encode())
if rr.Code != 400 || decodeErr(rr).Error != "range_too_long" {
t.Fatalf("%d %s", rr.Code, rr.Body)
}
q = url.Values{}
q.Set("start", earliest.Add(-10*time.Minute).Format(time.RFC3339))
q.Set("end", now.Add(-10*time.Minute).Format(time.RFC3339))
rr = call(q.Encode())
body := decodeErr(rr)
if rr.Code != 400 || body.Error != "range_too_old" || body.Earliest != formatRFC3339(earliest) {
t.Fatalf("%d %+v", rr.Code, body)
}
q = url.Values{}
q.Set("start", earliest.Add(-3*time.Minute).Format(time.RFC3339))
q.Set("end", now.Add(-3*time.Minute).Format(time.RFC3339))
rr = call(q.Encode())
if rr.Code != 200 {
t.Fatalf("%d %s", rr.Code, rr.Body)
}
var ok rangeResp
if err := json.Unmarshal(rr.Body.Bytes(), &ok); err != nil {
t.Fatal(err)
}
if ok.Start != formatRFC3339(earliest) {
t.Fatalf("adjusted start %s", ok.Start)
}
rr = call("start=2026-09-23T10:00:00+08:00&end=2026-09-23T11:00:00+08:00")
if rr.Code != 200 {
t.Fatalf("plus in query: %d %s", rr.Code, rr.Body)
}
}
func TestOnlineSurvivesCanceledRequest(t *testing.T) {
app := newTestApp(t, func() time.Time { return time.Date(2026, 9, 23, 12, 0, 0, 0, cst) })
ctx, cancel := context.WithCancel(context.Background())
cancel()
req := httptest.NewRequest(http.MethodGet, "/api/online", nil).WithContext(ctx)
rr := httptest.NewRecorder()
app.Handler().ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("status %d %s", rr.Code, rr.Body)
}
}
type errJSON struct {
Error string `json:"error"`
Message string `json:"message"`
Earliest string `json:"earliest"`
}