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.

122 lines
3.7 KiB

  1. package cos
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. )
  7. // func Test_checkResponse_error(t *testing.T) {
  8. // setup()
  9. // defer teardown()
  10. // mux.HandleFunc("/test_409", func(w http.ResponseWriter, r *http.Request) {
  11. // w.WriteHeader(http.StatusConflict)
  12. // fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  13. // <Error>
  14. // <Code>BucketAlreadyExists</Code>
  15. // <Message>The requested bucket name is not available.</Message>
  16. // <Resource>testdelete-1253846586.cos.ap-guangzhou.myqcloud.com</Resource>
  17. // <RequestId>NTk0NTRjZjZfNTViMjM1XzlkMV9hZTZh</RequestId>
  18. // <TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NTk3NWFiNjAyMzdlM2RlMmVmOGNiNWIxYjYwNDFhYmQ=</TraceId>
  19. // </Error>`)
  20. // })
  21. // req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_409", nil)
  22. // resp, _ := client.client.Do(req)
  23. // err := checkResponse(resp)
  24. // if e, ok := err.(*ErrorResponse); ok {
  25. // if e.Error() == "" {
  26. // t.Errorf("Expected e.Error() not empty, got %+v", e.Error())
  27. // }
  28. // if e.Code != "BucketAlreadyExists" {
  29. // t.Errorf("Expected BucketAlreadyExists error, got %+v", e.Code)
  30. // }
  31. // } else {
  32. // t.Errorf("Expected ErrorResponse error, got %+v", err)
  33. // }
  34. // }
  35. func Test_checkResponse_no_error(t *testing.T) {
  36. setup()
  37. defer teardown()
  38. mux.HandleFunc("/test_200", func(w http.ResponseWriter, r *http.Request) {
  39. fmt.Fprint(w, `test`)
  40. })
  41. req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_200", nil)
  42. resp, _ := client.client.Do(req)
  43. err := checkResponse(resp)
  44. if err != nil {
  45. t.Errorf("Expected error == nil, got %+v", err)
  46. }
  47. }
  48. func Test_checkResponse_with_error(t *testing.T) {
  49. setup()
  50. defer teardown()
  51. mux.HandleFunc("/test_409", func(w http.ResponseWriter, r *http.Request) {
  52. w.WriteHeader(http.StatusConflict)
  53. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  54. <Error>
  55. <Code>BucketAlreadyExists</Code>
  56. <Message>The requested bucket name is not available.</Message>
  57. <Resource>testdelete-1253846586.cos.ap-guangzhou.myqcloud.com</Resource>
  58. <RequestId>NTk0NTRjZjZfNTViMjM1XzlkMV9hZTZh</RequestId>
  59. <TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NTk3NWFiNjAyMzdlM2RlMmVmOGNiNWIxYjYwNDFhYmQ=</TraceId>
  60. </Error>`)
  61. })
  62. req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_409", nil)
  63. resp, _ := client.client.Do(req)
  64. err := checkResponse(resp)
  65. if e, ok := err.(*ErrorResponse); ok {
  66. if e.Error() == "" {
  67. t.Errorf("Expected e.Error() not empty, got %+v", e.Error())
  68. }
  69. if e.Code != "BucketAlreadyExists" {
  70. t.Errorf("Expected BucketAlreadyExists error, got %+v", e.Code)
  71. }
  72. } else {
  73. t.Errorf("Expected ErrorResponse error, got %+v", err)
  74. }
  75. }
  76. func Test_IsNotFoundError(t *testing.T) {
  77. setup()
  78. defer teardown()
  79. mux.HandleFunc("/test_404", func(w http.ResponseWriter, r *http.Request) {
  80. w.WriteHeader(http.StatusNotFound)
  81. fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
  82. <Error>
  83. <Code>NoSuchKey</Code>
  84. <Message>The specified key does not exist.</Message>
  85. <Resource>examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/test_404</Resource>
  86. <RequestId>NjA3OGY4NGFfNjJkMmMwYl8***</RequestId>
  87. <TraceId>OGVmYzZiMmQzYjA2OWNh***</TraceId>
  88. </Error>`)
  89. })
  90. req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_404", nil)
  91. resp, _ := client.client.Do(req)
  92. err := checkResponse(resp)
  93. e, ok := IsCOSError(err)
  94. if !ok {
  95. t.Errorf("IsCOSError Return Failed")
  96. }
  97. ok = IsNotFoundError(e)
  98. if !ok {
  99. t.Errorf("IsNotFoundError Return Failed")
  100. }
  101. if e.Code != "NoSuchKey" {
  102. t.Errorf("Expected NoSuchKey error, got %+v", e.Code)
  103. }
  104. }