http_binding_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package api_binding
  2. import (
  3. "fmt"
  4. "git.sxidc.com/go-tools/api_binding/http_binding"
  5. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  6. "git.sxidc.com/go-tools/api_binding/http_binding/response"
  7. "github.com/goccy/go-json"
  8. "io"
  9. "net/http"
  10. "testing"
  11. )
  12. func TestHttpBinding(t *testing.T) {
  13. http_binding.Init("test", "10000")
  14. defer http_binding.Destroy()
  15. testBinding := http_binding.NewBinding("test")
  16. http_binding.GetBind(testBinding, &http_binding.SimpleBindItem[any, map[string]interface{}]{
  17. Path: "/ping",
  18. ResponseFunc: response.SendMapResponse,
  19. BusinessFunc: func(c *binding_context.Context, inputModel any) (map[string]interface{}, error) {
  20. return map[string]interface{}{
  21. "result": "pong",
  22. }, nil
  23. },
  24. })
  25. resp, err := http.Get("http://localhost:10000/test/api/test/ping")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if resp.StatusCode != http.StatusOK {
  30. t.Fatal("状态码错误")
  31. }
  32. body, err := io.ReadAll(resp.Body)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer func(Body io.ReadCloser) {
  37. err := Body.Close()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. }(resp.Body)
  42. respMap := make(map[string]interface{})
  43. err = json.Unmarshal(body, &respMap)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. pong, ok := respMap["result"]
  48. if !ok {
  49. t.Fatal("响应错误")
  50. }
  51. if pong != "pong" {
  52. t.Fatal("响应错误")
  53. }
  54. }
  55. func Auth(binding *binding_context.Context) {
  56. fmt.Println("11111")
  57. }
  58. func TestHttpBinding2(t *testing.T) {
  59. http_binding.Init("test", "10000")
  60. defer http_binding.Destroy()
  61. testBinding := http_binding.NewBinding("test", Auth)
  62. http_binding.GetBind(testBinding, &http_binding.SimpleBindItem[any, map[string]interface{}]{
  63. Path: "/ping",
  64. ResponseFunc: response.SendMapResponse,
  65. BusinessFunc: func(c *binding_context.Context, inputModel any) (map[string]interface{}, error) {
  66. return map[string]interface{}{
  67. "result": "pong",
  68. }, nil
  69. },
  70. })
  71. resp, err := http.Get("http://localhost:10000/test/api/test/ping")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if resp.StatusCode != http.StatusOK {
  76. t.Fatal("状态码错误")
  77. }
  78. body, err := io.ReadAll(resp.Body)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer func(Body io.ReadCloser) {
  83. err := Body.Close()
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. }(resp.Body)
  88. respMap := make(map[string]interface{})
  89. err = json.Unmarshal(body, &respMap)
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. pong, ok := respMap["result"]
  94. if !ok {
  95. t.Fatal("响应错误")
  96. }
  97. if pong != "pong" {
  98. t.Fatal("响应错误")
  99. }
  100. }