|
@@ -1,6 +1,7 @@
|
|
package api_binding
|
|
package api_binding
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "fmt"
|
|
"git.sxidc.com/go-tools/api_binding/http_binding"
|
|
"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/binding_context"
|
|
"git.sxidc.com/go-tools/api_binding/http_binding/response"
|
|
"git.sxidc.com/go-tools/api_binding/http_binding/response"
|
|
@@ -61,3 +62,60 @@ func TestHttpBinding(t *testing.T) {
|
|
t.Fatal("响应错误")
|
|
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("响应错误")
|
|
|
|
+ }
|
|
|
|
+}
|