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("响应错误") } }