|
17 | 17 | package integration |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "bytes" |
20 | 21 | "context" |
21 | 22 | "encoding/base64" |
| 23 | + "encoding/json" |
22 | 24 | "fmt" |
23 | 25 | "log" |
24 | 26 | "math/rand" |
@@ -194,3 +196,89 @@ func TestObjectGet(t *testing.T) { |
194 | 196 | }) |
195 | 197 | } |
196 | 198 | } |
| 199 | + |
| 200 | +func downloadMultipleFiles(bucketName string, objects []string) (*http.Response, error) { |
| 201 | + requestURL := fmt.Sprintf("http://localhost:9090/api/v1/buckets/%s/objects/download-multiple", bucketName) |
| 202 | + |
| 203 | + postReqParams, _ := json.Marshal(objects) |
| 204 | + reqBody := bytes.NewReader(postReqParams) |
| 205 | + |
| 206 | + request, err := http.NewRequest( |
| 207 | + "POST", requestURL, reqBody) |
| 208 | + if err != nil { |
| 209 | + log.Println(err) |
| 210 | + return nil, nil |
| 211 | + } |
| 212 | + |
| 213 | + request.Header.Add("Cookie", fmt.Sprintf("token=%s", token)) |
| 214 | + request.Header.Add("Content-Type", "application/json") |
| 215 | + client := &http.Client{ |
| 216 | + Timeout: 2 * time.Second, |
| 217 | + } |
| 218 | + response, err := client.Do(request) |
| 219 | + return response, err |
| 220 | +} |
| 221 | + |
| 222 | +func TestDownloadMultipleFiles(t *testing.T) { |
| 223 | + assert := assert.New(t) |
| 224 | + type args struct { |
| 225 | + bucketName string |
| 226 | + objectLis []string |
| 227 | + } |
| 228 | + tests := []struct { |
| 229 | + name string |
| 230 | + args args |
| 231 | + expectedStatus int |
| 232 | + expectedError bool |
| 233 | + }{ |
| 234 | + { |
| 235 | + name: "Test empty Bucket", |
| 236 | + args: args{ |
| 237 | + bucketName: "", |
| 238 | + }, |
| 239 | + expectedStatus: 400, |
| 240 | + expectedError: true, |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "Test empty object list", |
| 244 | + args: args{ |
| 245 | + bucketName: "test-bucket", |
| 246 | + }, |
| 247 | + expectedStatus: 400, |
| 248 | + expectedError: true, |
| 249 | + }, |
| 250 | + { |
| 251 | + name: "Test with bucket and object list", |
| 252 | + args: args{ |
| 253 | + bucketName: "test-bucket", |
| 254 | + objectLis: []string{ |
| 255 | + "my-object.txt", |
| 256 | + "test-prefix/", |
| 257 | + "test-prefix/nested-prefix/", |
| 258 | + "test-prefix/nested-prefix/deep-nested/", |
| 259 | + }, |
| 260 | + }, |
| 261 | + expectedStatus: 200, |
| 262 | + expectedError: false, |
| 263 | + }, |
| 264 | + } |
| 265 | + |
| 266 | + for _, tt := range tests { |
| 267 | + t.Run(tt.name, func(t *testing.T) { |
| 268 | + resp, err := downloadMultipleFiles(tt.args.bucketName, tt.args.objectLis) |
| 269 | + if tt.expectedError { |
| 270 | + assert.Nil(err) |
| 271 | + if err != nil { |
| 272 | + log.Println(err) |
| 273 | + return |
| 274 | + } |
| 275 | + } |
| 276 | + if resp != nil { |
| 277 | + assert.Equal( |
| 278 | + tt.expectedStatus, |
| 279 | + resp.StatusCode, |
| 280 | + ) |
| 281 | + } |
| 282 | + }) |
| 283 | + } |
| 284 | +} |
0 commit comments