Add test to upload an object (#1473)

Co-authored-by: cniackz <cniackz@cniackzs-MacBook-Air.local>
Co-authored-by: Alex <33497058+bexsoft@users.noreply.github.com>
Co-authored-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
This commit is contained in:
Cesar Celis Hernandez
2022-01-28 00:51:13 -05:00
committed by GitHub
parent 0b7d4a2c35
commit 834e3fb996

View File

@@ -359,6 +359,41 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
return response, err
}
func UploadAnObject(bucketName string, fileName string) (*http.Response, error) {
/*
Helper function to upload a file to a bucket for testing.
POST {{baseUrl}}/buckets/:bucket_name/objects/upload
*/
boundary := "WebKitFormBoundaryWtayBM7t9EUQb8q3"
boundaryStart := "------" + boundary + "\r\n"
contentDispositionOne := "Content-Disposition: form-data; name=\"2\"; "
contentDispositionTwo := "filename=\"" + fileName + "\"\r\n"
contenType := "Content-Type: text/plain\r\n\r\na\n\r\n"
boundaryEnd := "------" + boundary + "--\r\n"
file := boundaryStart + contentDispositionOne + contentDispositionTwo +
contenType + boundaryEnd
arrayOfBytes := []byte(file)
requestDataBody := bytes.NewReader(arrayOfBytes)
request, err := http.NewRequest(
"POST",
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects/upload",
requestDataBody,
)
if err != nil {
log.Println(err)
}
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
request.Header.Add(
"Content-Type",
"multipart/form-data; boundary=----"+boundary,
)
client := &http.Client{
Timeout: 2 * time.Second,
}
response, err := client.Do(request)
return response, err
}
func TestAddBucket(t *testing.T) {
assert := assert.New(t)
@@ -941,3 +976,39 @@ func TestBucketRetention(t *testing.T) {
expected := "Http Response: {\"mode\":\"compliance\",\"unit\":\"years\",\"validity\":3}\n"
assert.Equal(expected, finalResponse, finalResponse)
}
func TestUploadObjectToBucket(t *testing.T) {
/*
Function to test the upload of an object to a bucket.
*/
// Test's variables
assert := assert.New(t)
bucketName := "testuploadobjecttobucket1"
fileName := "sample.txt"
// 1. Create the bucket
response, err := AddBucket(bucketName, false, false, nil, nil)
assert.Nil(err)
if err != nil {
log.Println(err)
return
}
if response != nil {
assert.Equal(201, response.StatusCode, "Status Code is incorrect")
}
// 2. Upload the object to the bucket
uploadResponse, uploadError := UploadAnObject(bucketName, fileName)
assert.Nil(uploadError)
if uploadError != nil {
log.Println(uploadError)
return
}
// 3. Verify the object was uploaded
finalResponse := inspectHTTPResponse(uploadResponse)
if uploadResponse != nil {
assert.Equal(200, uploadResponse.StatusCode, finalResponse)
}
}