Bladeren bron

修改bug

yjp 1 jaar geleden
bovenliggende
commit
08e0150d82
3 gewijzigde bestanden met toevoegingen van 72 en 9 verwijderingen
  1. 8 8
      http_binding/http_binding.go
  2. 1 1
      http_binding/middleware/middleware.go
  3. 63 0
      http_binding_test.go

+ 8 - 8
http_binding/http_binding.go

@@ -17,7 +17,7 @@ type BusinessFunc[I any, O any] func(c *binding_context.Context, inputModel I) (
 type BindingFunc[O any] func(c *binding_context.Context, request any, sendFunc response.SendFunc[O]) bool
 
 type Binding struct {
-	RouterGroup *gin.RouterGroup
+	routerGroup *gin.RouterGroup
 }
 
 func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
@@ -33,31 +33,31 @@ func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
 		})
 	}
 
-	return &Binding{RouterGroup: routerInstance.Group(apiPrefix, ginMiddlewares...)}
+	return &Binding{routerGroup: routerInstance.Group(apiPrefix, ginMiddlewares...)}
 }
 
 func PostBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
-	item.bind(b.RouterGroup, http.MethodPost, middlewares...)
+	item.bind(b.routerGroup, http.MethodPost, middlewares...)
 }
 
 func DeleteBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
-	item.bind(b.RouterGroup, http.MethodDelete, middlewares...)
+	item.bind(b.routerGroup, http.MethodDelete, middlewares...)
 }
 
 func PutBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
-	item.bind(b.RouterGroup, http.MethodPut, middlewares...)
+	item.bind(b.routerGroup, http.MethodPut, middlewares...)
 }
 
 func GetBind[I any, O any](b *Binding, item *SimpleBindItem[I, O], middlewares ...middleware.Func) {
-	item.bind(b.RouterGroup, http.MethodGet, middlewares...)
+	item.bind(b.routerGroup, http.MethodGet, middlewares...)
 }
 
 func Bind[I any, O any](b *Binding, item *BindItem[I, O], middlewares ...middleware.Func) {
-	item.bind(b.RouterGroup, middlewares...)
+	item.bind(b.routerGroup, middlewares...)
 }
 
 func Static(b *Binding, item *StaticBindItem) {
-	item.bind(b.RouterGroup)
+	item.bind(b.routerGroup)
 }
 
 type SimpleBindItem[I any, O any] struct {

+ 1 - 1
http_binding/middleware/middleware.go

@@ -1,5 +1,5 @@
 package middleware
 
-import "lcp/api/binding/http_binding/binding_context"
+import "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
 
 type Func func(c *binding_context.Context)

+ 63 - 0
http_binding_test.go

@@ -0,0 +1,63 @@
+package api_binding
+
+import (
+	"git.sxidc.com/go-tools/api_binding/http_binding"
+	"git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
+	"git.sxidc.com/go-tools/api_binding/http_binding/response"
+	"github.com/goccy/go-json"
+	"io"
+	"net/http"
+	"testing"
+)
+
+func TestHttpBinding(t *testing.T) {
+	http_binding.Init("test", "10000")
+	defer http_binding.Destroy()
+
+	testBinding := http_binding.NewBinding("test")
+	http_binding.GetBind(testBinding, &http_binding.SimpleBindItem[any, map[string]interface{}]{
+		Path:         "/ping",
+		ResponseFunc: response.SendMapResponse,
+		BusinessFunc: func(c *binding_context.Context, inputModel any) (map[string]interface{}, error) {
+			return map[string]interface{}{
+				"result": "pong",
+			}, nil
+		},
+	})
+
+	resp, err := http.Get("http://localhost:10000/test/api/test/ping")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if resp.StatusCode != http.StatusOK {
+		t.Fatal("状态码错误")
+	}
+
+	body, err := io.ReadAll(resp.Body)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	defer func(Body io.ReadCloser) {
+		err := Body.Close()
+		if err != nil {
+			t.Fatal(err)
+		}
+	}(resp.Body)
+
+	respMap := make(map[string]interface{})
+	err = json.Unmarshal(body, &respMap)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	pong, ok := respMap["result"]
+	if !ok {
+		t.Fatal("响应错误")
+	}
+
+	if pong != "pong" {
+		t.Fatal("响应错误")
+	}
+}