s3api_object_handlers_list_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package s3api
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. "time"
  7. )
  8. func TestListObjectsHandler(t *testing.T) {
  9. // https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html
  10. expected := `<?xml version="1.0" encoding="UTF-8"?>
  11. <ListBucketResult><Name>test_container</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>1.zip</Key><ETag>&#34;4397da7a7649e8085de9916c240e8166&#34;</ETag><Size>1234567</Size><Owner><ID>65a011niqo39cdf8ec533ec3d1ccaafsa932</ID></Owner><StorageClass>STANDARD</StorageClass><LastModified>2011-04-09T12:34:49Z</LastModified></Contents><EncodingType></EncodingType></ListBucketResult>`
  12. response := ListBucketResult{
  13. Name: "test_container",
  14. Prefix: "",
  15. Marker: "",
  16. NextMarker: "",
  17. MaxKeys: 1000,
  18. IsTruncated: false,
  19. Contents: []ListEntry{{
  20. Key: "1.zip",
  21. LastModified: time.Date(2011, 4, 9, 12, 34, 49, 0, time.UTC),
  22. ETag: "\"4397da7a7649e8085de9916c240e8166\"",
  23. Size: 1234567,
  24. Owner: CanonicalUser{
  25. ID: "65a011niqo39cdf8ec533ec3d1ccaafsa932",
  26. },
  27. StorageClass: "STANDARD",
  28. }},
  29. }
  30. encoded := string(s3err.EncodeXMLResponse(response))
  31. if encoded != expected {
  32. t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
  33. }
  34. }
  35. func Test_normalizePrefixMarker(t *testing.T) {
  36. type args struct {
  37. prefix string
  38. marker string
  39. }
  40. tests := []struct {
  41. name string
  42. args args
  43. wantAlignedDir string
  44. wantAlignedPrefix string
  45. wantAlignedMarker string
  46. }{
  47. {"prefix is a directory",
  48. args{"/parentDir/data/",
  49. ""},
  50. "parentDir",
  51. "data",
  52. "",
  53. },
  54. {"normal case",
  55. args{"/parentDir/data/0",
  56. "parentDir/data/0e/0e149049a2137b0cc12e"},
  57. "parentDir/data",
  58. "0",
  59. "0e/0e149049a2137b0cc12e",
  60. },
  61. {"empty prefix",
  62. args{"",
  63. "parentDir/data/0e/0e149049a2137b0cc12e"},
  64. "",
  65. "",
  66. "parentDir/data/0e/0e149049a2137b0cc12e",
  67. },
  68. {"empty directory",
  69. args{"parent",
  70. "parentDir/data/0e/0e149049a2137b0cc12e"},
  71. "",
  72. "parent",
  73. "parentDir/data/0e/0e149049a2137b0cc12e",
  74. },
  75. }
  76. for _, tt := range tests {
  77. t.Run(tt.name, func(t *testing.T) {
  78. gotAlignedDir, gotAlignedPrefix, gotAlignedMarker := normalizePrefixMarker(tt.args.prefix, tt.args.marker)
  79. assert.Equalf(t, tt.wantAlignedDir, gotAlignedDir, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
  80. assert.Equalf(t, tt.wantAlignedPrefix, gotAlignedPrefix, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
  81. assert.Equalf(t, tt.wantAlignedMarker, gotAlignedMarker, "normalizePrefixMarker(%v, %v)", tt.args.prefix, tt.args.marker)
  82. })
  83. }
  84. }