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