Skip to content

Commit

Permalink
fix: listBuckets properly returns wrapped response (#23879) (#23884)
Browse files Browse the repository at this point in the history
closes #23861

(cherry picked from commit e6e0fd2)

closes #23883

Co-authored-by: Vlasta Hajek <vlastimil.hajek@bonitoo.io>
  • Loading branch information
davidby-influx and vlastahajek committed Nov 9, 2022
1 parent cc26b76 commit b0e44a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 6 additions & 1 deletion services/httpd/handler.go
Expand Up @@ -1082,6 +1082,10 @@ type Bucket struct {
UpdatedAt time.Time `json:"updatedAt"`
}

type Buckets struct {
Buckets []Bucket `json:"buckets"`
}

func (h *Handler) servePostCreateBucketV2(w http.ResponseWriter, r *http.Request, user meta.User) {
var bs []byte
if r.ContentLength > 0 {
Expand Down Expand Up @@ -1457,7 +1461,8 @@ func findBucketIndex(dbs []meta.DatabaseInfo, db string, rp string) (dbIndex int
}

func (h *Handler) sendBuckets(w http.ResponseWriter, buckets []Bucket) {
b, err := json.Marshal(buckets)
bucketsObj := Buckets{buckets}
b, err := json.Marshal(bucketsObj)
if err != nil {
h.httpError(w, fmt.Sprintf("list buckets marshaling error: %s", err.Error()), http.StatusInternalServerError)
return
Expand Down
12 changes: 6 additions & 6 deletions services/httpd/handler_test.go
Expand Up @@ -2212,18 +2212,18 @@ func TestHandler_ListBuckets(t *testing.T) {
t.Fatalf("incorrect error message, expected: %q, got: %q", ct.errMsg, errMsg)
}
} else {
var got []httpd.Bucket
var got httpd.Buckets
exp := getBuckets(ct.skip, ct.limit)

if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatalf("unmarshaling buckets: %s", err.Error())
}
if len(exp) != len(got) {
t.Fatalf("expected %d buckets returned, got %d", len(exp), len(got))
if len(exp) != len(got.Buckets) {
t.Fatalf("expected %d buckets returned, got %d", len(exp), len(got.Buckets))
}
for i := 0; i < len(got); i++ {
if exp[i] != got[i].Name {
t.Fatalf("expected %q, got %q", exp[i], got[i].Name)
for i := 0; i < len(got.Buckets); i++ {
if exp[i] != got.Buckets[i].Name {
t.Fatalf("expected %q, got %q", exp[i], got.Buckets[i].Name)
}
}
}
Expand Down

0 comments on commit b0e44a4

Please sign in to comment.