diff --git a/api/errors/errors.go b/api/errors/errors.go index 27ac5669..ca514ee1 100644 --- a/api/errors/errors.go +++ b/api/errors/errors.go @@ -193,6 +193,7 @@ const ( ErrInvalidRangeLength ErrRangeOutOfBounds ErrMissingContentRange + ErrNoSuchTagSet ErrMalformedJSON ErrInsecureClientRequest @@ -1774,6 +1775,12 @@ var errorCodes = errorCodeMap{ Description: "You have reached the quota limit.", HTTPStatusCode: http.StatusConflict, }, + ErrNoSuchTagSet: { + ErrCode: ErrNoSuchTagSet, + Code: "NoSuchTagSet", + Description: "The TagSet does not exist", + HTTPStatusCode: http.StatusNotFound, + }, // Add your error structure here. } diff --git a/api/handler/tagging.go b/api/handler/tagging.go index 9dd7c369..76008d56 100644 --- a/api/handler/tagging.go +++ b/api/handler/tagging.go @@ -160,7 +160,12 @@ func (h *handler) GetBucketTaggingHandler(w http.ResponseWriter, r *http.Request tagSet, err := h.obj.GetBucketTagging(ctx, bktInfo) if err != nil { - h.logAndSendError(ctx, w, "could not get object tagging", reqInfo, err) + h.logAndSendError(ctx, w, "could not get bucket tagging", reqInfo, err) + return + } + + if len(tagSet) == 0 { + h.logAndSendError(ctx, w, "empty bucket tag set", reqInfo, errors.GetAPIError(errors.ErrNoSuchTagSet)) return } diff --git a/api/handler/tagging_test.go b/api/handler/tagging_test.go index ddfa45f5..0df81b41 100644 --- a/api/handler/tagging_test.go +++ b/api/handler/tagging_test.go @@ -2,6 +2,7 @@ package handler import ( "net/http" + "net/http/httptest" "strings" "testing" @@ -112,3 +113,63 @@ func TestPutObjectTaggingCheckUniqueness(t *testing.T) { }) } } + +func TestGetBucketTagging(t *testing.T) { + hc := prepareHandlerContext(t) + bktName, tagKey, tagValue := "get-bucket-tagging", "key", "value" + createBucket(hc, bktName) + + getBucketTaggingErr(hc, bktName, apierr.GetAPIError(apierr.ErrNoSuchTagSet)) + putBucketTagging(hc, bktName, map[string]string{tagKey: tagValue}) + + tagSet := getBucketTagging(hc, bktName) + require.Len(t, tagSet.TagSet, 1) + require.Equal(t, tagKey, tagSet.TagSet[0].Key) + require.Equal(t, tagValue, tagSet.TagSet[0].Value) + + deleteBucketTagging(hc, bktName) + getBucketTaggingErr(hc, bktName, apierr.GetAPIError(apierr.ErrNoSuchTagSet)) +} + +func putBucketTagging(hc *handlerContext, bktName string, tags map[string]string) { + body := &data.Tagging{ + TagSet: make([]data.Tag, 0, len(tags)), + } + + for key, val := range tags { + body.TagSet = append(body.TagSet, data.Tag{ + Key: key, + Value: val, + }) + } + + w, r := prepareTestRequest(hc, bktName, "", body) + middleware.GetReqInfo(r.Context()).Tagging = body + hc.Handler().PutBucketTaggingHandler(w, r) + assertStatus(hc.t, w, http.StatusOK) +} + +func getBucketTagging(hc *handlerContext, bktName string) *data.Tagging { + w := getBucketTaggingBase(hc, bktName) + assertStatus(hc.t, w, http.StatusOK) + res := &data.Tagging{} + parseTestResponse(hc.t, w, res) + return res +} + +func getBucketTaggingErr(hc *handlerContext, bktName string, err apierr.Error) { + w := getBucketTaggingBase(hc, bktName) + assertS3Error(hc.t, w, err) +} + +func getBucketTaggingBase(hc *handlerContext, bktName string) *httptest.ResponseRecorder { + w, r := prepareTestRequest(hc, bktName, "", nil) + hc.Handler().GetBucketTaggingHandler(w, r) + return w +} + +func deleteBucketTagging(hc *handlerContext, bktName string) { + w, r := prepareTestRequest(hc, bktName, "", nil) + hc.Handler().DeleteBucketTaggingHandler(w, r) + assertStatus(hc.t, w, http.StatusNoContent) +}