github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/cmd/api-resources_test.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package cmd 19 20 import ( 21 "net/url" 22 "testing" 23 ) 24 25 // Test list objects resources V2. 26 func TestListObjectsV2Resources(t *testing.T) { 27 testCases := []struct { 28 values url.Values 29 prefix, token, startAfter, delimiter string 30 fetchOwner bool 31 maxKeys int 32 encodingType string 33 errCode APIErrorCode 34 }{ 35 { 36 values: url.Values{ 37 "prefix": []string{"photos/"}, 38 "continuation-token": []string{"dG9rZW4="}, 39 "start-after": []string{"start-after"}, 40 "delimiter": []string{SlashSeparator}, 41 "fetch-owner": []string{"true"}, 42 "max-keys": []string{"100"}, 43 "encoding-type": []string{"gzip"}, 44 }, 45 prefix: "photos/", 46 token: "token", 47 startAfter: "start-after", 48 delimiter: SlashSeparator, 49 fetchOwner: true, 50 maxKeys: 100, 51 encodingType: "gzip", 52 errCode: ErrNone, 53 }, 54 { 55 values: url.Values{ 56 "prefix": []string{"photos/"}, 57 "continuation-token": []string{"dG9rZW4="}, 58 "start-after": []string{"start-after"}, 59 "delimiter": []string{SlashSeparator}, 60 "fetch-owner": []string{"true"}, 61 "encoding-type": []string{"gzip"}, 62 }, 63 prefix: "photos/", 64 token: "token", 65 startAfter: "start-after", 66 delimiter: SlashSeparator, 67 fetchOwner: true, 68 maxKeys: maxObjectList, 69 encodingType: "gzip", 70 errCode: ErrNone, 71 }, 72 { 73 values: url.Values{ 74 "prefix": []string{"photos/"}, 75 "continuation-token": []string{""}, 76 "start-after": []string{"start-after"}, 77 "delimiter": []string{SlashSeparator}, 78 "fetch-owner": []string{"true"}, 79 "encoding-type": []string{"gzip"}, 80 }, 81 prefix: "", 82 token: "", 83 startAfter: "", 84 delimiter: "", 85 fetchOwner: false, 86 maxKeys: 0, 87 encodingType: "", 88 errCode: ErrIncorrectContinuationToken, 89 }, 90 } 91 92 for i, testCase := range testCases { 93 prefix, token, startAfter, delimiter, fetchOwner, maxKeys, encodingType, errCode := getListObjectsV2Args(testCase.values) 94 95 if errCode != testCase.errCode { 96 t.Errorf("Test %d: Expected error code:%d, got %d", i+1, testCase.errCode, errCode) 97 } 98 if prefix != testCase.prefix { 99 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.prefix, prefix) 100 } 101 if token != testCase.token { 102 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.token, token) 103 } 104 if startAfter != testCase.startAfter { 105 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.startAfter, startAfter) 106 } 107 if delimiter != testCase.delimiter { 108 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.delimiter, delimiter) 109 } 110 if fetchOwner != testCase.fetchOwner { 111 t.Errorf("Test %d: Expected %t, got %t", i+1, testCase.fetchOwner, fetchOwner) 112 } 113 if maxKeys != testCase.maxKeys { 114 t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxKeys, maxKeys) 115 } 116 if encodingType != testCase.encodingType { 117 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType) 118 } 119 } 120 } 121 122 // Test list objects resources V1. 123 func TestListObjectsV1Resources(t *testing.T) { 124 testCases := []struct { 125 values url.Values 126 prefix, marker, delimiter string 127 maxKeys int 128 encodingType string 129 }{ 130 { 131 values: url.Values{ 132 "prefix": []string{"photos/"}, 133 "marker": []string{"test"}, 134 "delimiter": []string{SlashSeparator}, 135 "max-keys": []string{"100"}, 136 "encoding-type": []string{"gzip"}, 137 }, 138 prefix: "photos/", 139 marker: "test", 140 delimiter: SlashSeparator, 141 maxKeys: 100, 142 encodingType: "gzip", 143 }, 144 { 145 values: url.Values{ 146 "prefix": []string{"photos/"}, 147 "marker": []string{"test"}, 148 "delimiter": []string{SlashSeparator}, 149 "encoding-type": []string{"gzip"}, 150 }, 151 prefix: "photos/", 152 marker: "test", 153 delimiter: SlashSeparator, 154 maxKeys: maxObjectList, 155 encodingType: "gzip", 156 }, 157 } 158 159 for i, testCase := range testCases { 160 prefix, marker, delimiter, maxKeys, encodingType, argsErr := getListObjectsV1Args(testCase.values) 161 if argsErr != ErrNone { 162 t.Errorf("Test %d: argument parsing failed, got %v", i+1, argsErr) 163 } 164 if prefix != testCase.prefix { 165 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.prefix, prefix) 166 } 167 if marker != testCase.marker { 168 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.marker, marker) 169 } 170 if delimiter != testCase.delimiter { 171 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.delimiter, delimiter) 172 } 173 if maxKeys != testCase.maxKeys { 174 t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxKeys, maxKeys) 175 } 176 if encodingType != testCase.encodingType { 177 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType) 178 } 179 } 180 } 181 182 // Validates extracting information for object resources. 183 func TestGetObjectsResources(t *testing.T) { 184 testCases := []struct { 185 values url.Values 186 uploadID string 187 partNumberMarker, maxParts int 188 encodingType string 189 }{ 190 { 191 values: url.Values{ 192 "uploadId": []string{"11123-11312312311231-12313"}, 193 "part-number-marker": []string{"1"}, 194 "max-parts": []string{"1000"}, 195 "encoding-type": []string{"gzip"}, 196 }, 197 uploadID: "11123-11312312311231-12313", 198 partNumberMarker: 1, 199 maxParts: 1000, 200 encodingType: "gzip", 201 }, 202 } 203 204 for i, testCase := range testCases { 205 uploadID, partNumberMarker, maxParts, encodingType, argsErr := getObjectResources(testCase.values) 206 if argsErr != ErrNone { 207 t.Errorf("Test %d: argument parsing failed, got %v", i+1, argsErr) 208 } 209 if uploadID != testCase.uploadID { 210 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.uploadID, uploadID) 211 } 212 if partNumberMarker != testCase.partNumberMarker { 213 t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.partNumberMarker, partNumberMarker) 214 } 215 if maxParts != testCase.maxParts { 216 t.Errorf("Test %d: Expected %d, got %d", i+1, testCase.maxParts, maxParts) 217 } 218 if encodingType != testCase.encodingType { 219 t.Errorf("Test %d: Expected %s, got %s", i+1, testCase.encodingType, encodingType) 220 } 221 } 222 }