浏览代码

修改bug

yjp 1 年之前
父节点
当前提交
380eeb6bf1
共有 2 个文件被更改,包括 62 次插入2 次删除
  1. 4 2
      http_binding/http_binding.go
  2. 58 0
      http_binding_test.go

+ 4 - 2
http_binding/http_binding.go

@@ -28,8 +28,9 @@ func NewBinding(apiVersion string, middlewares ...middleware.Func) *Binding {
 
 	ginMiddlewares := make([]gin.HandlerFunc, 0)
 	for _, m := range middlewares {
+		innerM := m
 		ginMiddlewares = append(ginMiddlewares, func(c *gin.Context) {
-			m(&binding_context.Context{Context: c})
+			innerM(&binding_context.Context{Context: c})
 		})
 	}
 
@@ -108,8 +109,9 @@ func (item *BindItem[I, O]) bind(routerGroup *gin.RouterGroup, middlewares ...mi
 
 	ginHandleFunctions := make([]gin.HandlerFunc, 0)
 	for _, m := range middlewares {
+		innerM := m
 		ginHandleFunctions = append(ginHandleFunctions, func(c *gin.Context) {
-			m(&binding_context.Context{Context: c})
+			innerM(&binding_context.Context{Context: c})
 		})
 	}
 

+ 58 - 0
http_binding_test.go

@@ -1,6 +1,7 @@
 package api_binding
 
 import (
+	"fmt"
 	"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"
@@ -61,3 +62,60 @@ func TestHttpBinding(t *testing.T) {
 		t.Fatal("响应错误")
 	}
 }
+
+func Auth(binding *binding_context.Context) {
+	fmt.Println("11111")
+}
+
+func TestHttpBinding2(t *testing.T) {
+	http_binding.Init("test", "10000")
+	defer http_binding.Destroy()
+
+	testBinding := http_binding.NewBinding("test", Auth)
+
+	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("响应错误")
+	}
+}