2026-05-08 07:27:06 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestServeHTTP(t *testing.T) {
|
|
|
|
|
// 注册服务
|
|
|
|
|
handler := func(in struct{ Name string }) string {
|
|
|
|
|
return "Hello " + in.Name
|
|
|
|
|
}
|
2026-05-09 16:39:20 +08:00
|
|
|
Host("*").POST("/hello", handler).Auth(0).Memo("say hello")
|
2026-05-08 07:27:06 +08:00
|
|
|
|
2026-05-09 16:39:20 +08:00
|
|
|
rh := &RouteHandler{}
|
2026-05-08 07:27:06 +08:00
|
|
|
|
|
|
|
|
// 模拟请求
|
|
|
|
|
req := httptest.NewRequest("POST", "/hello", strings.NewReader(`{"name":"Star"}`))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
rh.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
if w.Code != http.StatusOK {
|
|
|
|
|
t.Errorf("Expected status 200, got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body := w.Body.String()
|
|
|
|
|
if body != "Hello Star" {
|
|
|
|
|
t.Errorf("Expected 'Hello Star', got '%s'", body)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestServeHTTP_404(t *testing.T) {
|
2026-05-09 16:39:20 +08:00
|
|
|
rh := &RouteHandler{}
|
2026-05-08 07:27:06 +08:00
|
|
|
req := httptest.NewRequest("GET", "/notfound", nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
rh.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
|
|
|
t.Errorf("Expected status 404, got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestServeHTTP_VerifyFailed(t *testing.T) {
|
|
|
|
|
type ValidIn struct {
|
|
|
|
|
Age int `verify:"between:18-100"`
|
|
|
|
|
}
|
|
|
|
|
handler := func(in ValidIn) string {
|
|
|
|
|
return "ok"
|
|
|
|
|
}
|
2026-05-09 16:39:20 +08:00
|
|
|
Host("*").POST("/verify", handler).Auth(0).Memo("test verify")
|
2026-05-08 07:27:06 +08:00
|
|
|
|
2026-05-09 16:39:20 +08:00
|
|
|
rh := &RouteHandler{}
|
2026-05-08 07:27:06 +08:00
|
|
|
req := httptest.NewRequest("POST", "/verify", strings.NewReader(`{"age":10}`))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
rh.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
|
|
|
t.Errorf("Expected status 400, got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-10 12:44:25 +08:00
|
|
|
|
|
|
|
|
func TestServeHTTP_Panic(t *testing.T) {
|
|
|
|
|
Host("*").GET("/panic", func() string {
|
|
|
|
|
panic("intentional panic")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
rh := &RouteHandler{}
|
|
|
|
|
req := httptest.NewRequest("GET", "/panic", nil)
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
rh.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
|
|
|
t.Errorf("Expected status 500, got %d", w.Code)
|
|
|
|
|
}
|
|
|
|
|
}
|