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.

97 lines
2.6 KiB

  1. package cos
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "net/http"
  6. "reflect"
  7. "testing"
  8. )
  9. func Test_calSHA1Digest(t *testing.T) {
  10. want := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
  11. got := fmt.Sprintf("%x", calSHA1Digest([]byte("test")))
  12. if got != want {
  13. t.Errorf("calSHA1Digest request sha1: %+v, want %+v", got, want)
  14. }
  15. }
  16. func Test_calMD5Digest(t *testing.T) {
  17. want := "098f6bcd4621d373cade4e832627b4f6"
  18. got := fmt.Sprintf("%x", calMD5Digest([]byte("test")))
  19. if got != want {
  20. t.Errorf("calMD5Digest request md5: %+v, want %+v", got, want)
  21. }
  22. }
  23. func Test_cloneHeader(t *testing.T) {
  24. ori := http.Header{}
  25. opt := &ori
  26. opt.Add("TestHeader1", "h1")
  27. opt.Add("TestHeader1", "h2")
  28. res := cloneHeader(opt)
  29. if !reflect.DeepEqual(res, opt) {
  30. t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
  31. }
  32. if !reflect.DeepEqual(ori, *opt) {
  33. t.Errorf("cloneHeader, returned:%+v, want:%+v", *opt, ori)
  34. }
  35. res.Add("cloneHeader1", "c1")
  36. res.Add("cloneHeader2", "c2")
  37. if v := opt.Get("cloneHeader1"); v != "" {
  38. t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
  39. }
  40. if v := opt.Get("cloneHeader2"); v != "" {
  41. t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
  42. }
  43. opt = &http.Header{}
  44. res = cloneHeader(opt)
  45. if !reflect.DeepEqual(res, opt) {
  46. t.Errorf("cloneHeader, returned:%+v, want:%+v", res, opt)
  47. }
  48. }
  49. func Test_CloneCompleteMultipartUploadOptions(t *testing.T) {
  50. ori := CompleteMultipartUploadOptions{
  51. XMLName: xml.Name{Local: "CompleteMultipartUploadResult"},
  52. Parts: []Object{
  53. {
  54. Key: "Key1",
  55. ETag: "Etag1",
  56. },
  57. {
  58. Key: "Key2",
  59. ETag: "Etag2",
  60. },
  61. },
  62. XOptionHeader: &http.Header{},
  63. }
  64. ori.XOptionHeader.Add("Test", "value")
  65. opt := &ori
  66. res := CloneCompleteMultipartUploadOptions(opt)
  67. if !reflect.DeepEqual(res, opt) {
  68. t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
  69. }
  70. if !reflect.DeepEqual(ori, *opt) {
  71. t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", *opt, ori)
  72. }
  73. res.XOptionHeader.Add("TestClone", "value")
  74. if v := opt.XOptionHeader.Get("TestClone"); v != "" {
  75. t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
  76. }
  77. opt = &CompleteMultipartUploadOptions{}
  78. res = CloneCompleteMultipartUploadOptions(opt)
  79. if !reflect.DeepEqual(res, opt) {
  80. t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
  81. }
  82. res.Parts = append(res.Parts, Object{Key: "K", ETag: "T"})
  83. if len(opt.Parts) > 0 {
  84. t.Errorf("CloneCompleteMultipartUploadOptions Failed")
  85. }
  86. if reflect.DeepEqual(res, opt) {
  87. t.Errorf("CloneCompleteMultipartUploadOptions, returned:%+v,want:%+v", res, opt)
  88. }
  89. }