- 注册 GET /hello,使用统一响应结构并添加路由回归测试 - 使用 lyra.toml 配置 Gin 模式与监听地址,处理启动错误 - 明确 handler/service 业务分包约定并同步文档
29 lines
640 B
Go
29 lines
640 B
Go
package router_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"lyra/internal/router"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestHelloRoute(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
engine := gin.New()
|
|
router.RegisterRoutes(engine)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/hello", nil))
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("GET /hello status = %d, want %d", recorder.Code, http.StatusOK)
|
|
}
|
|
want := `{"success":true,"data":"Hello, World!"}`
|
|
if got := recorder.Body.String(); got != want {
|
|
t.Fatalf("GET /hello body = %q, want %q", got, want)
|
|
}
|
|
}
|