response_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package responses
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "net/http"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func Test_CommonResponse(t *testing.T) {
  11. r := NewCommonResponse()
  12. assert.NotNil(t, r)
  13. assert.Equal(t, 0, r.GetHttpStatus())
  14. // assert.Equal(t, nil, r.GetHttpHeaders())
  15. assert.Equal(t, "", r.GetHttpContentString())
  16. assert.Equal(t, 0, len(r.GetHttpContentBytes()))
  17. assert.Nil(t, r.GetOriginHttpResponse())
  18. assert.False(t, r.IsSuccess())
  19. }
  20. func Test_CommonResponse_parseFromHttpResponse(t *testing.T) {
  21. r := NewCommonResponse()
  22. header := make(http.Header)
  23. status := "200"
  24. statusCode := 200
  25. res := &http.Response{
  26. Proto: "HTTP/1.1",
  27. ProtoMajor: 1,
  28. Header: header,
  29. StatusCode: statusCode,
  30. Status: status + " " + http.StatusText(statusCode),
  31. }
  32. var noBody io.ReadCloser = ioutil.NopCloser(bytes.NewReader(nil))
  33. res.Header = make(http.Header)
  34. res.Header.Add("Server", "GitHub.com")
  35. res.Body = noBody
  36. r.parseFromHttpResponse(res)
  37. expected := `HTTP/1.1 200 OK
  38. Server: GitHub.com
  39. `
  40. assert.True(t, r.IsSuccess())
  41. assert.Equal(t, "GitHub.com", r.GetHttpHeaders()["Server"][0])
  42. assert.Equal(t, expected, r.String())
  43. }