response_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package response
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/zeromicro/go-zero/rest/httpx"
  11. )
  12. // TC-0190: 触发404等
  13. func TestNewCodeError(t *testing.T) {
  14. err := NewCodeError(400, "bad request")
  15. assert.Equal(t, 400, err.Code())
  16. assert.Equal(t, "bad request", err.Error())
  17. }
  18. // TC-0190: 触发404等
  19. func TestCodeErrorHelpers(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. fn func(string) *CodeError
  23. code int
  24. msg string
  25. }{
  26. {"ErrBadRequest", ErrBadRequest, 400, "test bad"},
  27. {"ErrUnauthorized", ErrUnauthorized, 401, "test unauth"},
  28. {"ErrForbidden", ErrForbidden, 403, "test forbidden"},
  29. {"ErrNotFound", ErrNotFound, 404, "test not found"},
  30. {"ErrConflict", ErrConflict, 409, "test conflict"},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. err := tt.fn(tt.msg)
  35. assert.Equal(t, tt.code, err.Code())
  36. assert.Equal(t, tt.msg, err.Error())
  37. })
  38. }
  39. }
  40. // TC-0190: 触发404等
  41. func TestCodeError_AsInterface(t *testing.T) {
  42. var err error = NewCodeError(404, "not found")
  43. var ce *CodeError
  44. ok := errors.As(err, &ce)
  45. assert.True(t, ok)
  46. assert.Equal(t, 404, ce.Code())
  47. assert.Equal(t, "not found", ce.Error())
  48. }
  49. // TC-0190: 触发404等
  50. func TestCodeError_NonCodeError(t *testing.T) {
  51. err := errors.New("internal error")
  52. var ce *CodeError
  53. ok := errors.As(err, &ce)
  54. assert.False(t, ok)
  55. }
  56. // TC-0190: 触发404等
  57. func TestSetup_ErrorHandler_CodeError(t *testing.T) {
  58. Setup()
  59. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  60. httpx.ErrorCtx(r.Context(), w, ErrBadRequest("参数错误"))
  61. })
  62. req := httptest.NewRequest(http.MethodPost, "/test", nil)
  63. rr := httptest.NewRecorder()
  64. handler.ServeHTTP(rr, req)
  65. assert.Equal(t, http.StatusOK, rr.Code)
  66. var body Body
  67. err := json.Unmarshal(rr.Body.Bytes(), &body)
  68. require.NoError(t, err)
  69. assert.Equal(t, 400, body.Code)
  70. assert.Equal(t, "参数错误", body.Msg)
  71. assert.Nil(t, body.Data)
  72. }
  73. // TC-0191: DB异常
  74. func TestSetup_ErrorHandler_InternalError(t *testing.T) {
  75. Setup()
  76. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  77. httpx.ErrorCtx(r.Context(), w, errors.New("db connection failed"))
  78. })
  79. req := httptest.NewRequest(http.MethodPost, "/test", nil)
  80. rr := httptest.NewRecorder()
  81. handler.ServeHTTP(rr, req)
  82. assert.Equal(t, http.StatusOK, rr.Code)
  83. var body Body
  84. err := json.Unmarshal(rr.Body.Bytes(), &body)
  85. require.NoError(t, err)
  86. assert.Equal(t, 500, body.Code)
  87. assert.Equal(t, "服务器内部错误", body.Msg)
  88. assert.Nil(t, body.Data)
  89. }
  90. // TC-0192: 正常请求
  91. func TestSetup_OkHandler_WithData(t *testing.T) {
  92. Setup()
  93. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  94. httpx.OkJsonCtx(r.Context(), w, map[string]string{"key": "value"})
  95. })
  96. req := httptest.NewRequest(http.MethodPost, "/test", nil)
  97. rr := httptest.NewRecorder()
  98. handler.ServeHTTP(rr, req)
  99. assert.Equal(t, http.StatusOK, rr.Code)
  100. var body Body
  101. err := json.Unmarshal(rr.Body.Bytes(), &body)
  102. require.NoError(t, err)
  103. assert.Equal(t, 0, body.Code)
  104. assert.Equal(t, "ok", body.Msg)
  105. require.NotNil(t, body.Data)
  106. dataMap, ok := body.Data.(map[string]interface{})
  107. require.True(t, ok)
  108. assert.Equal(t, "value", dataMap["key"])
  109. }
  110. // TC-0193: 返回nil
  111. func TestSetup_OkHandler_NilData(t *testing.T) {
  112. Setup()
  113. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. httpx.OkJsonCtx(r.Context(), w, nil)
  115. })
  116. req := httptest.NewRequest(http.MethodPost, "/test", nil)
  117. rr := httptest.NewRecorder()
  118. handler.ServeHTTP(rr, req)
  119. assert.Equal(t, http.StatusOK, rr.Code)
  120. var body Body
  121. err := json.Unmarshal(rr.Body.Bytes(), &body)
  122. require.NoError(t, err)
  123. assert.Equal(t, 0, body.Code)
  124. assert.Equal(t, "ok", body.Msg)
  125. assert.Nil(t, body.Data)
  126. }