test: add tests for handler, email, cmd/server, version and enforce 80% coverage
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
//go:build linux || freebsd
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestItoa(t *testing.T) {
|
||||
tests := []struct {
|
||||
n int
|
||||
want string
|
||||
}{
|
||||
{0, "0"},
|
||||
{1, "1"},
|
||||
{9, "9"},
|
||||
{10, "10"},
|
||||
{42, "42"},
|
||||
{100, "100"},
|
||||
{-1, "-1"},
|
||||
{-42, "-42"},
|
||||
{8080, "8080"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.want, func(t *testing.T) {
|
||||
if got := itoa(tt.n); got != tt.want {
|
||||
t.Errorf("itoa(%d) = %q, want %q", tt.n, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLast(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
c byte
|
||||
want int
|
||||
}{
|
||||
{"hello", 'l', 3},
|
||||
{"hello", 'o', 4},
|
||||
{"hello", 'h', 0},
|
||||
{"hello", 'x', -1},
|
||||
{"", 'a', -1},
|
||||
{"aaa", 'a', 2},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if got := last(tt.s, tt.c); got != tt.want {
|
||||
t.Errorf("last(%q, %q) = %d, want %d", tt.s, tt.c, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
remoteAddr string
|
||||
want string
|
||||
}{
|
||||
{"ipv4 with port", "192.168.1.1:12345", "192.168.1.1"},
|
||||
{"ipv6 with brackets", "[::1]:12345", "[::1]"},
|
||||
{"ipv4 without port", "192.168.1.1", "192.168.1.1"},
|
||||
{"empty", "", ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.RemoteAddr = tt.remoteAddr
|
||||
if got := clientIP(req); got != tt.want {
|
||||
t.Errorf("clientIP() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatusRecorder(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
sr := &statusRecorder{ResponseWriter: rec, status: http.StatusOK}
|
||||
|
||||
// Default status before WriteHeader.
|
||||
if sr.status != http.StatusOK {
|
||||
t.Errorf("initial status = %d, want %d", sr.status, http.StatusOK)
|
||||
}
|
||||
|
||||
sr.WriteHeader(http.StatusNotFound)
|
||||
if sr.status != http.StatusNotFound {
|
||||
t.Errorf("status after WriteHeader = %d, want %d", sr.status, http.StatusNotFound)
|
||||
}
|
||||
|
||||
// rec should also have the status set.
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("rec.Code = %d, want %d", rec.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRequestLog(t *testing.T) {
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
wrapped := withRequestLog(handler, logger)
|
||||
req := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||
req.RemoteAddr = "192.168.1.1:12345"
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
wrapped.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("status = %d, want 200", rec.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user