You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.8 KiB

  1. package cos
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. var (
  14. // mux is the HTTP request multiplexer used with the test server.
  15. mux *http.ServeMux
  16. // client is the COS client being tested.
  17. client *Client
  18. // server is a test HTTP server used to provide mock API responses.
  19. server *httptest.Server
  20. )
  21. // setup sets up a test HTTP server along with a cos.Client that is
  22. // configured to talk to that test server. Tests should register handlers on
  23. // mux which provide mock responses for the API method being tested.
  24. func setup() {
  25. // test server
  26. mux = http.NewServeMux()
  27. server = httptest.NewServer(mux)
  28. u, _ := url.Parse(server.URL)
  29. client = NewClient(&BaseURL{u, u}, nil)
  30. }
  31. // teardown closes the test HTTP server.
  32. func teardown() {
  33. server.Close()
  34. }
  35. type values map[string]string
  36. func testFormValues(t *testing.T, r *http.Request, values values) {
  37. want := url.Values{}
  38. for k, v := range values {
  39. want.Set(k, v)
  40. }
  41. r.ParseForm()
  42. if got := r.Form; !reflect.DeepEqual(got, want) {
  43. t.Errorf("Request parameters: %v, want %v", got, want)
  44. }
  45. }
  46. func testMethod(t *testing.T, r *http.Request, want string) {
  47. if got := r.Method; got != want {
  48. t.Errorf("Request method: %v, want %v", got, want)
  49. }
  50. }
  51. func testHeader(t *testing.T, r *http.Request, header string, want string) {
  52. if got := r.Header.Get(header); got != want {
  53. t.Errorf("Header.Get(%q) returned %q, want %q", header, got, want)
  54. }
  55. }
  56. func testURLParseError(t *testing.T, err error) {
  57. if err == nil {
  58. t.Errorf("Expected error to be returned")
  59. }
  60. if err, ok := err.(*url.Error); !ok || err.Op != "parse" {
  61. t.Errorf("Expected URL parse error, got %+v", err)
  62. }
  63. }
  64. func testBody(t *testing.T, r *http.Request, want string) {
  65. b, err := ioutil.ReadAll(r.Body)
  66. if err != nil {
  67. t.Errorf("Error reading request body: %v", err)
  68. }
  69. if got := string(b); got != want {
  70. t.Errorf("request Body is %s, want %s", got, want)
  71. }
  72. }
  73. // Helper function to test that a value is marshalled to XML as expected.
  74. func testXMLMarshal(t *testing.T, v interface{}, want string) {
  75. j, err := xml.Marshal(v)
  76. if err != nil {
  77. t.Errorf("Unable to marshal JSON for %v", v)
  78. }
  79. w := new(bytes.Buffer)
  80. err = xml.NewEncoder(w).Encode([]byte(want))
  81. if err != nil {
  82. t.Errorf("String is not valid json: %s", want)
  83. }
  84. if w.String() != string(j) {
  85. t.Errorf("xml.Marshal(%q) returned %s, want %s", v, j, w)
  86. }
  87. // now go the other direction and make sure things unmarshal as expected
  88. u := reflect.ValueOf(v).Interface()
  89. if err := xml.Unmarshal([]byte(want), u); err != nil {
  90. t.Errorf("Unable to unmarshal XML for %v", want)
  91. }
  92. if !reflect.DeepEqual(v, u) {
  93. t.Errorf("xml.Unmarshal(%q) returned %s, want %s", want, u, v)
  94. }
  95. }
  96. func TestNewClient(t *testing.T) {
  97. c := NewClient(nil, nil)
  98. if got, want := c.BaseURL.ServiceURL.String(), defaultServiceBaseURL; got != want {
  99. t.Errorf("NewClient BaseURL is %v, want %v", got, want)
  100. }
  101. if got, want := c.UserAgent, userAgent; got != want {
  102. t.Errorf("NewClient UserAgent is %v, want %v", got, want)
  103. }
  104. }
  105. func TestNewBucketURL_secure_false(t *testing.T) {
  106. got := NewBucketURL("bname-idx", "ap-guangzhou", false).String()
  107. want := "http://bname-idx.cos.ap-guangzhou.myqcloud.com"
  108. if got != want {
  109. t.Errorf("NewBucketURL is %v, want %v", got, want)
  110. }
  111. }
  112. func TestNewBucketURL_secure_true(t *testing.T) {
  113. got := NewBucketURL("bname-idx", "ap-guangzhou", true).String()
  114. want := "https://bname-idx.cos.ap-guangzhou.myqcloud.com"
  115. if got != want {
  116. t.Errorf("NewBucketURL is %v, want %v", got, want)
  117. }
  118. }
  119. func TestClient_doAPI(t *testing.T) {
  120. setup()
  121. defer teardown()
  122. }
  123. func TestNewAuthTime(t *testing.T) {
  124. a := NewAuthTime(time.Hour)
  125. if a.SignStartTime != a.KeyStartTime ||
  126. a.SignEndTime != a.SignEndTime ||
  127. a.SignStartTime.Add(time.Hour) != a.SignEndTime {
  128. t.Errorf("NewAuthTime request got %+v is not valid", a)
  129. }
  130. }