Skip to content

Commit 834e3fb

Browse files
cniackzcniackzbexsoftdvaldivia
authored
Add test to upload an object (#1473)
Co-authored-by: cniackz <[email protected]> Co-authored-by: Alex <[email protected]> Co-authored-by: Daniel Valdivia <[email protected]>
1 parent 0b7d4a2 commit 834e3fb

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

integration/buckets_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,41 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
359359
return response, err
360360
}
361361

362+
func UploadAnObject(bucketName string, fileName string) (*http.Response, error) {
363+
/*
364+
Helper function to upload a file to a bucket for testing.
365+
POST {{baseUrl}}/buckets/:bucket_name/objects/upload
366+
*/
367+
boundary := "WebKitFormBoundaryWtayBM7t9EUQb8q3"
368+
boundaryStart := "------" + boundary + "\r\n"
369+
contentDispositionOne := "Content-Disposition: form-data; name=\"2\"; "
370+
contentDispositionTwo := "filename=\"" + fileName + "\"\r\n"
371+
contenType := "Content-Type: text/plain\r\n\r\na\n\r\n"
372+
boundaryEnd := "------" + boundary + "--\r\n"
373+
file := boundaryStart + contentDispositionOne + contentDispositionTwo +
374+
contenType + boundaryEnd
375+
arrayOfBytes := []byte(file)
376+
requestDataBody := bytes.NewReader(arrayOfBytes)
377+
request, err := http.NewRequest(
378+
"POST",
379+
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/upload",
380+
requestDataBody,
381+
)
382+
if err != nil {
383+
log.Println(err)
384+
}
385+
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
386+
request.Header.Add(
387+
"Content-Type",
388+
"multipart/form-data; boundary=----"+boundary,
389+
)
390+
client := &http.Client{
391+
Timeout: 2 * time.Second,
392+
}
393+
response, err := client.Do(request)
394+
return response, err
395+
}
396+
362397
func TestAddBucket(t *testing.T) {
363398
assert := assert.New(t)
364399

@@ -941,3 +976,39 @@ func TestBucketRetention(t *testing.T) {
941976
expected := "Http Response: {\"mode\":\"compliance\",\"unit\":\"years\",\"validity\":3}\n"
942977
assert.Equal(expected, finalResponse, finalResponse)
943978
}
979+
980+
func TestUploadObjectToBucket(t *testing.T) {
981+
/*
982+
Function to test the upload of an object to a bucket.
983+
*/
984+
985+
// Test's variables
986+
assert := assert.New(t)
987+
bucketName := "testuploadobjecttobucket1"
988+
fileName := "sample.txt"
989+
990+
// 1. Create the bucket
991+
response, err := AddBucket(bucketName, false, false, nil, nil)
992+
assert.Nil(err)
993+
if err != nil {
994+
log.Println(err)
995+
return
996+
}
997+
if response != nil {
998+
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
999+
}
1000+
1001+
// 2. Upload the object to the bucket
1002+
uploadResponse, uploadError := UploadAnObject(bucketName, fileName)
1003+
assert.Nil(uploadError)
1004+
if uploadError != nil {
1005+
log.Println(uploadError)
1006+
return
1007+
}
1008+
1009+
// 3. Verify the object was uploaded
1010+
finalResponse := inspectHTTPResponse(uploadResponse)
1011+
if uploadResponse != nil {
1012+
assert.Equal(200, uploadResponse.StatusCode, finalResponse)
1013+
}
1014+
}

0 commit comments

Comments
 (0)