tool_kit.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package test
  2. import (
  3. "baize-demo/project/mono/application"
  4. "bytes"
  5. "encoding/json"
  6. "git.sxidc.com/go-tools/utils/strutils"
  7. "github.com/stretchr/testify/assert"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "reflect"
  12. "testing"
  13. "time"
  14. )
  15. type ToolKit struct {
  16. t *testing.T
  17. body io.Reader
  18. header map[string]string
  19. queryParams map[string]string
  20. jsonResponse interface{}
  21. token string
  22. responseRecorder *httptest.ResponseRecorder
  23. }
  24. func Init() {
  25. application.NewApp()
  26. go func() {
  27. err := application.Start()
  28. if err != nil {
  29. panic(err)
  30. }
  31. }()
  32. time.Sleep(100 * time.Millisecond)
  33. }
  34. func Destroy() {
  35. err := application.Finish()
  36. if err != nil {
  37. panic(err)
  38. }
  39. application.DestroyApp()
  40. }
  41. func NewToolKit(t *testing.T) *ToolKit {
  42. return &ToolKit{
  43. t: t,
  44. header: make(map[string]string),
  45. queryParams: make(map[string]string),
  46. }
  47. }
  48. func (toolKit *ToolKit) SetHeader(key string, value string) *ToolKit {
  49. toolKit.header[key] = value
  50. return toolKit
  51. }
  52. func (toolKit *ToolKit) SetBody(body *bytes.Buffer) *ToolKit {
  53. toolKit.body = body
  54. return toolKit
  55. }
  56. func (toolKit *ToolKit) SetQueryParams(key string, value string) *ToolKit {
  57. toolKit.queryParams[key] = value
  58. return toolKit
  59. }
  60. func (toolKit *ToolKit) SetJsonBody(body interface{}) *ToolKit {
  61. jsonBody, err := json.Marshal(body)
  62. if err != nil {
  63. toolKit.t.Fatal("转换JSON失败")
  64. }
  65. toolKit.body = bytes.NewBuffer(jsonBody)
  66. return toolKit
  67. }
  68. func (toolKit *ToolKit) SetJsonResponse(response interface{}) *ToolKit {
  69. if response == nil {
  70. toolKit.jsonResponse = nil
  71. return toolKit
  72. }
  73. responseValue := reflect.ValueOf(response)
  74. if responseValue.Kind() != reflect.Ptr {
  75. toolKit.t.Fatal("JsonResponse应该传递指针类型")
  76. }
  77. toolKit.jsonResponse = response
  78. return toolKit
  79. }
  80. func (toolKit *ToolKit) SetToken(token string) *ToolKit {
  81. toolKit.token = token
  82. return toolKit
  83. }
  84. func (toolKit *ToolKit) Request(url string, method string) *ToolKit {
  85. if len(toolKit.queryParams) != 0 {
  86. url = url + "?"
  87. for key, value := range toolKit.queryParams {
  88. url = url + key + "=" + value + "&"
  89. }
  90. url = url[:len(url)-1]
  91. toolKit.queryParams = make(map[string]string)
  92. }
  93. request, err := http.NewRequest(method, url, toolKit.body)
  94. if err != nil {
  95. toolKit.t.Fatal("创建请求失败", err)
  96. }
  97. if len(toolKit.header) != 0 {
  98. for key, value := range toolKit.header {
  99. request.Header.Add(key, value)
  100. }
  101. toolKit.header = make(map[string]string)
  102. }
  103. if !strutils.IsStringEmpty(toolKit.token) {
  104. request.Header.Add("Authorization", toolKit.token)
  105. }
  106. toolKit.responseRecorder = httptest.NewRecorder()
  107. application.ServerHttpForTest(toolKit.responseRecorder, request)
  108. if toolKit.responseRecorder.Code == http.StatusOK && toolKit.jsonResponse != nil {
  109. responseBody, err := io.ReadAll(toolKit.responseRecorder.Body)
  110. if err != nil {
  111. toolKit.t.Fatal("读取响应Body失败")
  112. }
  113. err = json.Unmarshal(responseBody, toolKit.jsonResponse)
  114. if err != nil {
  115. toolKit.t.Fatal("转换Response失败")
  116. }
  117. }
  118. return toolKit
  119. }
  120. func (toolKit *ToolKit) AssertStatusCode(code int) *ToolKit {
  121. assert.Equal(toolKit.t, code, toolKit.responseRecorder.Code)
  122. return toolKit
  123. }
  124. func (toolKit *ToolKit) AssertBodyEqual(body string) *ToolKit {
  125. assert.Equal(toolKit.t, body, toolKit.responseRecorder.Body.String())
  126. return toolKit
  127. }
  128. func (toolKit *ToolKit) AssertEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  129. assert.Equal(toolKit.t, expected, actual, msgAndArgs)
  130. return toolKit
  131. }
  132. func (toolKit *ToolKit) AssertNotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) *ToolKit {
  133. assert.NotEqual(toolKit.t, expected, actual, msgAndArgs)
  134. return toolKit
  135. }
  136. func (toolKit *ToolKit) AssertNotEmpty(object interface{}, msgAndArgs ...interface{}) *ToolKit {
  137. assert.NotEmpty(toolKit.t, object, msgAndArgs)
  138. return toolKit
  139. }
  140. func (toolKit *ToolKit) AssertGreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) *ToolKit {
  141. assert.GreaterOrEqual(toolKit.t, e1, e2, msgAndArgs)
  142. return toolKit
  143. }
  144. func (toolKit *ToolKit) AssertZero(e1 interface{}, msgAndArgs ...interface{}) *ToolKit {
  145. assert.Zero(toolKit.t, e1, msgAndArgs)
  146. return toolKit
  147. }