feat(http): 支持业务适配器批次接入原生路由
Verify / verify (push) Canceled after 0s

This commit is contained in:
zhangdeman committed 2026-11-28 01:23:33 -08:00
1 parent 53bb8b7b4d
commit 98c28520ef
5 files changed
+129 -7

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
package http
import (
"net/http/httptest"
"reflect"
"testing"
"github.com/gin-gonic/gin"
)
func TestNativeBatchAtomicRegistrationAndMiddlewareFreeze(t *testing.T) {
server, _ := NewServer(Config{}, Options{NoRoute: func(ctx *gin.Context) { ctx.String(404, "application fallback") }})
ok := func(ctx *gin.Context) { ctx.Status(204) }
if err := server.HandleBatch(NativeRoute{Metadata: Metadata{Method: "GET", Path: "/first"}, Handlers: []gin.HandlerFunc{ok}}, NativeRoute{Metadata: Metadata{Method: "GET", Path: "/first"}, Handlers: []gin.HandlerFunc{ok}}); err == nil {
t.Fatal("duplicate batch accepted")
}
if len(server.Routes()) != 0 {
t.Fatal("failed batch partially registered")
}
if err := server.HandleBatch(NativeRoute{Metadata: Metadata{Method: "GET", Path: "/first"}, Handlers: []gin.HandlerFunc{ok}, InputType: reflect.TypeOf(flowForm{}), OutputType: reflect.TypeOf(exampleOutput{})}); err != nil {
t.Fatal(err)
}
if err := server.Use(func(ctx *gin.Context) { ctx.Header("X-Application", "ready"); ctx.Next() }); err != nil {
t.Fatal(err)
}
doc, err := server.OpenAPI(OpenAPIOptions{Title: "Native", Version: "1"})
if err != nil || doc.Paths.Value("/first").Get.Responses.Status(200) == nil {
t.Fatal("typed native contract lost", err)
}
handler := server.Handler()
for _, path := range []string{"/first", "/missing"} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest("GET", path, nil))
if recorder.Header().Get("X-Application") != "ready" {
t.Fatal("late middleware not applied")
}
if path == "/missing" && recorder.Body.String() != "application fallback" {
t.Fatal("application fallback lost")
}
}
if server.Use(ok) == nil || server.Handle("GET", "/late", ok) == nil {
t.Fatal("runtime registration was not frozen")
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.