resp_test.go 767 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package admin
  2. import (
  3. "encoding/json"
  4. "io"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRespSucess(t *testing.T) {
  10. assert := assert.New(t)
  11. w := httptest.NewRecorder()
  12. RespSucess(w, "data")
  13. // fmt.Println(w)
  14. assert.Equal(w.Code, 200)
  15. body, _ := io.ReadAll(w.Body)
  16. res := Resp{}
  17. err := json.Unmarshal(body, &res)
  18. assert.Nil(err)
  19. assert.Equal(res.Code, 0)
  20. assert.Equal(res.Data, "data")
  21. }
  22. func TestRespError(t *testing.T) {
  23. assert := assert.New(t)
  24. w := httptest.NewRecorder()
  25. RespError(w, 10, "err-msg")
  26. // fmt.Println(w)
  27. assert.Equal(w.Code, 200)
  28. body, _ := io.ReadAll(w.Body)
  29. res := Resp{}
  30. err := json.Unmarshal(body, &res)
  31. assert.Nil(err)
  32. assert.Equal(res.Code, 10)
  33. assert.Equal(res.Msg, "err-msg")
  34. }