Add test to add tag to an object (#1481)
This commit is contained in:
committed by
GitHub
parent
63d3c7207d
commit
ce989e39ab
@@ -369,6 +369,37 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
|
||||
return response, err
|
||||
}
|
||||
|
||||
func PutObjectTags(bucketName string, prefix string, tags map[string]string, versionID string) (*http.Response, error) {
|
||||
/*
|
||||
Helper function to put object's tags.
|
||||
PUT: /buckets/{bucket_name}/objects/tags?prefix=prefix
|
||||
{
|
||||
"tags": {}
|
||||
}
|
||||
*/
|
||||
requestDataAdd := map[string]interface{}{
|
||||
"tags": tags,
|
||||
}
|
||||
requestDataJSON, _ := json.Marshal(requestDataAdd)
|
||||
requestDataBody := bytes.NewReader(requestDataJSON)
|
||||
request, err := http.NewRequest(
|
||||
"PUT",
|
||||
"http://localhost:9090/api/v1/buckets/"+
|
||||
bucketName+"/objects/tags?prefix="+prefix+"&version_id="+versionID,
|
||||
requestDataBody,
|
||||
)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
request.Header.Add("Cookie", fmt.Sprintf("token=%s", token))
|
||||
request.Header.Add("Content-Type", "application/json")
|
||||
client := &http.Client{
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
return response, err
|
||||
}
|
||||
|
||||
func DownloadObject(bucketName string, path string) (*http.Response, error) {
|
||||
/*
|
||||
Helper function to download an object from a bucket.
|
||||
@@ -453,13 +484,13 @@ func DeleteObject(bucketName string, path string, recursive bool, allVersions bo
|
||||
return response, err
|
||||
}
|
||||
|
||||
func ListObjects(bucketName string) (*http.Response, error) {
|
||||
func ListObjects(bucketName string, prefix string) (*http.Response, error) {
|
||||
/*
|
||||
Helper function to list objects in a bucket.
|
||||
GET: {{baseUrl}}/buckets/:bucket_name/objects
|
||||
Helper function to list objects in a bucket.
|
||||
GET: {{baseUrl}}/buckets/:bucket_name/objects
|
||||
*/
|
||||
request, err := http.NewRequest("GET",
|
||||
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects",
|
||||
"http://localhost:9090/api/v1/buckets/"+bucketName+"/objects?prefix="+prefix,
|
||||
nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -1056,6 +1087,77 @@ func TestBucketRetention(t *testing.T) {
|
||||
assert.Equal(expected, finalResponse, finalResponse)
|
||||
}
|
||||
|
||||
func TestPutObjectTag(t *testing.T) {
|
||||
/*
|
||||
Test to put a tag to an object
|
||||
*/
|
||||
|
||||
// Vars
|
||||
assert := assert.New(t)
|
||||
bucketName := "testputobjecttagbucketone"
|
||||
fileName := "testputobjecttagbucketone.txt"
|
||||
path := encodeBase64(fileName)
|
||||
tags := make(map[string]string)
|
||||
tags["tag"] = "testputobjecttagbucketonetagone"
|
||||
versionID := "null"
|
||||
|
||||
// 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
|
||||
}
|
||||
if uploadResponse != nil {
|
||||
assert.Equal(
|
||||
200,
|
||||
uploadResponse.StatusCode,
|
||||
inspectHTTPResponse(uploadResponse),
|
||||
)
|
||||
}
|
||||
|
||||
// 3. Put a tag to the object
|
||||
putTagResponse, putTagError := PutObjectTags(
|
||||
bucketName, path, tags, versionID)
|
||||
assert.Nil(putTagError)
|
||||
if putTagError != nil {
|
||||
log.Println(putTagError)
|
||||
return
|
||||
}
|
||||
putObjectTagresult := inspectHTTPResponse(putTagResponse)
|
||||
if putTagResponse != nil {
|
||||
assert.Equal(
|
||||
200, putTagResponse.StatusCode, putObjectTagresult)
|
||||
}
|
||||
|
||||
// 4. Verify the object's tag is set
|
||||
listResponse, listError := ListObjects(bucketName, path)
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
return
|
||||
}
|
||||
finalResponse := inspectHTTPResponse(listResponse)
|
||||
if listResponse != nil {
|
||||
assert.Equal(200, listResponse.StatusCode,
|
||||
finalResponse)
|
||||
}
|
||||
assert.True(
|
||||
strings.Contains(finalResponse, tags["tag"]),
|
||||
finalResponse)
|
||||
}
|
||||
|
||||
func TestDownloadObject(t *testing.T) {
|
||||
/*
|
||||
Test to download an object from a given bucket.
|
||||
@@ -1217,7 +1319,7 @@ func TestDeleteObject(t *testing.T) {
|
||||
}
|
||||
|
||||
// 4. List the objects in the bucket and make sure the object is gone
|
||||
listResponse, listError := ListObjects(bucketName)
|
||||
listResponse, listError := ListObjects(bucketName, "")
|
||||
assert.Nil(listError)
|
||||
if listError != nil {
|
||||
log.Println(listError)
|
||||
|
||||
Reference in New Issue
Block a user