response_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package responses
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "strconv"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func makeHTTPResponse(statusCode int, content string) (res *http.Response) {
  11. header := make(http.Header)
  12. status := strconv.Itoa(statusCode)
  13. res = &http.Response{
  14. Proto: "HTTP/1.1",
  15. ProtoMajor: 1,
  16. Header: header,
  17. StatusCode: statusCode,
  18. Status: status + " " + http.StatusText(statusCode),
  19. }
  20. res.Header = make(http.Header)
  21. res.Body = ioutil.NopCloser(bytes.NewReader([]byte(content)))
  22. return
  23. }
  24. func Test_CommonResponse(t *testing.T) {
  25. r := NewCommonResponse()
  26. assert.NotNil(t, r)
  27. assert.Equal(t, 0, r.GetHttpStatus())
  28. // assert.Equal(t, nil, r.GetHttpHeaders())
  29. assert.Equal(t, "", r.GetHttpContentString())
  30. assert.Equal(t, 0, len(r.GetHttpContentBytes()))
  31. assert.Nil(t, r.GetOriginHttpResponse())
  32. assert.False(t, r.IsSuccess())
  33. }
  34. func Test_CommonResponse_parseFromHttpResponse(t *testing.T) {
  35. r := NewCommonResponse()
  36. res := makeHTTPResponse(200, "")
  37. res.Header.Add("Server", "GitHub.com")
  38. r.parseFromHttpResponse(res)
  39. expected := `HTTP/1.1 200 OK
  40. Server: GitHub.com
  41. `
  42. assert.True(t, r.IsSuccess())
  43. assert.Equal(t, "GitHub.com", r.GetHttpHeaders()["Server"][0])
  44. assert.Equal(t, expected, r.String())
  45. }
  46. func Test_CommonResponse_Unmarshal(t *testing.T) {
  47. r := NewCommonResponse()
  48. res := makeHTTPResponse(400, "")
  49. err := Unmarshal(r, res, "JSON")
  50. assert.NotNil(t, err)
  51. assert.Equal(t, "SDK.ServerError\nErrorCode: \nRecommend: \nRequestId: \nMessage: ", err.Error())
  52. }
  53. func Test_CommonResponse_Unmarshal_CommonResponse(t *testing.T) {
  54. r := NewCommonResponse()
  55. res := makeHTTPResponse(200, "")
  56. err := Unmarshal(r, res, "JSON")
  57. assert.Nil(t, err)
  58. }
  59. func Test_CommonResponse_Unmarshal_XML(t *testing.T) {
  60. r := &MyResponse{
  61. BaseResponse: &BaseResponse{},
  62. }
  63. res := makeHTTPResponse(200, `{"RequestId": "the request id"}`)
  64. err := Unmarshal(r, res, "XML")
  65. assert.NotNil(t, err)
  66. }
  67. type MyResponse struct {
  68. *BaseResponse
  69. RequestId string
  70. }
  71. func Test_CommonResponse_Unmarshal_EmptyContent(t *testing.T) {
  72. r := &MyResponse{
  73. BaseResponse: &BaseResponse{},
  74. }
  75. res := makeHTTPResponse(200, "")
  76. err := Unmarshal(r, res, "JSON")
  77. assert.Nil(t, err)
  78. assert.Equal(t, "", r.RequestId)
  79. }
  80. func Test_CommonResponse_Unmarshal_MyResponse(t *testing.T) {
  81. r := &MyResponse{
  82. BaseResponse: &BaseResponse{},
  83. }
  84. res := makeHTTPResponse(200, `{"RequestId": "the request id"}`)
  85. err := Unmarshal(r, res, "JSON")
  86. assert.Nil(t, err)
  87. assert.Equal(t, "the request id", r.RequestId)
  88. }
  89. func Test_CommonResponse_Unmarshal_Error(t *testing.T) {
  90. r := &MyResponse{
  91. BaseResponse: &BaseResponse{},
  92. }
  93. res := makeHTTPResponse(200, `{`)
  94. err := Unmarshal(r, res, "JSON")
  95. assert.NotNil(t, err)
  96. assert.Equal(t, "[SDK.JsonUnmarshalError] Failed to unmarshal response, but you can get the data via response.GetHttpStatusCode() and response.GetHttpContentString()\ncaused by:\nresponses.MyResponse.readFieldHash: expect \", but found \x00, error found in #1 byte of ...|{|..., bigger context ...|{|...", err.Error())
  97. }