Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement schema API #601

Merged
merged 13 commits into from
Aug 5, 2021
Prev Previous commit
Next Next commit
add unit tests for plugin
  • Loading branch information
fgksgf committed Aug 4, 2021
commit c0c0be6d634018d124c50941cda794f89bab56cd
86 changes: 41 additions & 45 deletions pkg/apisix/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@
package apisix

import (
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"strings"
"testing"

"golang.org/x/net/nettest"
)

type fakeAPISIXPluginSrv struct {
plugins map[string]string
plugins []string
}

var fakePluginNames = []string{
"plugin-1",
"plugin-2",
"plugin-3",
}

func (srv *fakeAPISIXPluginSrv) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand All @@ -35,27 +45,16 @@ func (srv *fakeAPISIXPluginSrv) ServeHTTP(w http.ResponseWriter, r *http.Request
}

if r.Method == http.MethodGet {
list := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(list) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}

pluginName := list[len(list)-1]
if resp, ok := srv.plugins[pluginName]; ok {
_, _ = w.Write([]byte(resp))
} else {
_, _ = w.Write([]byte(errMsg))
}
data, _ := json.Marshal(srv.plugins)
_, _ = w.Write(data)
w.WriteHeader(http.StatusOK)
return
}

}

func runFakePluginSrv(t *testing.T) *http.Server {
srv := &fakeAPISIXPluginSrv{
plugins: testData,
plugins: fakePluginNames,
}

ln, _ := nettest.NewLocalListener("tcp")
Expand All @@ -75,34 +74,31 @@ func runFakePluginSrv(t *testing.T) *http.Server {
}

func TestPluginClient(t *testing.T) {
//srv := runFakePluginSrv(t)
//defer func() {
// assert.Nil(t, srv.Shutdown(context.Background()))
//}()
//
//u := url.URL{
// Scheme: "http",
// Host: srv.Addr,
// Path: "/apisix/admin",
//}
//
//closedCh := make(chan struct{})
//close(closedCh)
//cli := newPluginClient(&cluster{
// baseURL: u.String(),
// cli: http.DefaultClient,
// cache: &dummyCache{},
// cacheSynced: closedCh,
//})

//for k := range testData {
// obj, err := cli.Get(context.Background(), k)
// assert.Nil(t, err)
// assert.Equal(t, obj.Name, k)
// assert.Equal(t, obj.Content, testData[k])
//}
//
//obj, err := cli.Get(context.Background(), "not-a-plugin")
//assert.Nil(t, err)
//assert.Equal(t, obj.Content, errMsg)
srv := runFakePluginSrv(t)
defer func() {
assert.Nil(t, srv.Shutdown(context.Background()))
}()

u := url.URL{
Scheme: "http",
Host: srv.Addr,
Path: "/apisix/admin",
}

closedCh := make(chan struct{})
close(closedCh)
cli := newPluginClient(&cluster{
baseURL: u.String(),
cli: http.DefaultClient,
cache: &dummyCache{},
cacheSynced: closedCh,
})

// List
objs, err := cli.List(context.Background())
assert.Nil(t, err)
assert.Len(t, objs, len(fakePluginNames))
for i := range fakePluginNames {
assert.Equal(t, objs[i], fakePluginNames[i])
}
}