Skip to content

Commit

Permalink
fix json unmarshal error when list plguins (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmssczy committed Mar 2, 2022
1 parent e2475cf commit cc9b6be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions pkg/apisix/cluster.go
Expand Up @@ -750,10 +750,15 @@ func (c *cluster) getList(ctx context.Context, url, resource string) ([]string,
return nil, err
}

var listResponse []string
var listResponse map[string]interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&listResponse); err != nil {
return nil, err
}
return listResponse, nil
res := make([]string, 0, len(listResponse))

for name := range listResponse {
res = append(res, name)
}
return res, nil
}
10 changes: 9 additions & 1 deletion pkg/apisix/plugin_test.go
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -46,8 +47,13 @@ func (srv *fakeAPISIXPluginSrv) ServeHTTP(w http.ResponseWriter, r *http.Request
return
}

fakePluginsResp := make(map[string]interface{}, len(srv.plugins))
for _, fp := range srv.plugins {
fakePluginsResp[fp] = struct{}{}
}

if r.Method == http.MethodGet {
data, _ := json.Marshal(srv.plugins)
data, _ := json.Marshal(fakePluginsResp)
_, _ = w.Write(data)
w.WriteHeader(http.StatusOK)
return
Expand Down Expand Up @@ -101,6 +107,8 @@ func TestPluginClient(t *testing.T) {
objs, err := cli.List(context.Background())
assert.Nil(t, err)
assert.Len(t, objs, len(fakePluginNames))
sort.Strings(fakePluginNames)
sort.Strings(objs)
for i := range fakePluginNames {
assert.Equal(t, fakePluginNames[i], objs[i])
}
Expand Down

0 comments on commit cc9b6be

Please sign in to comment.