error_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package oss
  2. import (
  3. "math"
  4. "net/http"
  5. "strings"
  6. . "gopkg.in/check.v1"
  7. )
  8. type OssErrorSuite struct{}
  9. var _ = Suite(&OssErrorSuite{})
  10. func (s *OssErrorSuite) TestCheckCRCHasCRCInResp(c *C) {
  11. headers := http.Header{
  12. "Expires": {"-1"},
  13. "Content-Length": {"0"},
  14. "Content-Encoding": {"gzip"},
  15. "X-Oss-Hash-Crc64ecma": {"0"},
  16. }
  17. resp := &Response{
  18. StatusCode: 200,
  19. Headers: headers,
  20. Body: nil,
  21. ClientCRC: math.MaxUint64,
  22. ServerCRC: math.MaxUint64,
  23. }
  24. err := checkCRC(resp, "test")
  25. c.Assert(err, IsNil)
  26. }
  27. func (s *OssErrorSuite) TestCheckCRCNotHasCRCInResp(c *C) {
  28. headers := http.Header{
  29. "Expires": {"-1"},
  30. "Content-Length": {"0"},
  31. "Content-Encoding": {"gzip"},
  32. }
  33. resp := &Response{
  34. StatusCode: 200,
  35. Headers: headers,
  36. Body: nil,
  37. ClientCRC: math.MaxUint64,
  38. ServerCRC: math.MaxUint32,
  39. }
  40. err := checkCRC(resp, "test")
  41. c.Assert(err, IsNil)
  42. }
  43. func (s *OssErrorSuite) TestCheckCRCCNegative(c *C) {
  44. headers := http.Header{
  45. "Expires": {"-1"},
  46. "Content-Length": {"0"},
  47. "Content-Encoding": {"gzip"},
  48. "X-Oss-Hash-Crc64ecma": {"0"},
  49. }
  50. resp := &Response{
  51. StatusCode: 200,
  52. Headers: headers,
  53. Body: nil,
  54. ClientCRC: 0,
  55. ServerCRC: math.MaxUint64,
  56. }
  57. err := checkCRC(resp, "test")
  58. c.Assert(err, NotNil)
  59. testLogger.Println("error:", err)
  60. }
  61. func (s *OssErrorSuite) TestCheckDownloadCRC(c *C) {
  62. err := checkDownloadCRC(0xFBF9D9603A6FA020, 0xFBF9D9603A6FA020)
  63. c.Assert(err, IsNil)
  64. err = checkDownloadCRC(0, 0)
  65. c.Assert(err, IsNil)
  66. err = checkDownloadCRC(0xDB6EFFF26AA94946, 0)
  67. c.Assert(err, NotNil)
  68. testLogger.Println("error:", err)
  69. }
  70. func (s *OssErrorSuite) TestServiceErrorEndPoint(c *C) {
  71. xmlBody := `<?xml version="1.0" encoding="UTF-8"?>
  72. <Error>
  73. <Code>AccessDenied</Code>
  74. <Message>The bucket you visit is not belong to you.</Message>
  75. <RequestId>5C1B5E9BD79A6B9B6466166E</RequestId>
  76. <HostId>oss-c-sdk-test-verify-b.oss-cn-shenzhen.aliyuncs.com</HostId>
  77. </Error>`
  78. serverError, _ := serviceErrFromXML([]byte(xmlBody), 403, "5C1B5E9BD79A6B9B6466166E")
  79. errMsg := serverError.Error()
  80. c.Assert(strings.Contains(errMsg, "Endpoint="), Equals, false)
  81. xmlBodyWithEndPoint := `<?xml version="1.0" encoding="UTF-8"?>
  82. <Error>
  83. <Code>AccessDenied</Code>
  84. <Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message>
  85. <RequestId>5C1B595ED51820B569C6A12F</RequestId>
  86. <HostId>hello-hangzws.oss-cn-qingdao.aliyuncs.com</HostId>
  87. <Bucket>hello-hangzws</Bucket>
  88. <Endpoint>oss-cn-shenzhen.aliyuncs.com</Endpoint>
  89. </Error>`
  90. serverError, _ = serviceErrFromXML([]byte(xmlBodyWithEndPoint), 406, "5C1B595ED51820B569C6A12F")
  91. errMsg = serverError.Error()
  92. c.Assert(strings.Contains(errMsg, "Endpoint=oss-cn-shenzhen.aliyuncs.com"), Equals, true)
  93. }