From 1305bf49a5b304b052b1da9b6f23f57dcd794f83 Mon Sep 17 00:00:00 2001 From: Harold Ozouf Date: Mon, 25 Jan 2021 11:08:04 +0100 Subject: [PATCH] Fix plugin type on middleware endpoint response --- pkg/api/handler.go | 8 +++ pkg/api/handler_test.go | 109 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/pkg/api/handler.go b/pkg/api/handler.go index dc2fa712e..d35483f82 100644 --- a/pkg/api/handler.go +++ b/pkg/api/handler.go @@ -8,6 +8,7 @@ import ( assetfs "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/mux" + "github.com/traefik/traefik/v2/pkg/config/dynamic" "github.com/traefik/traefik/v2/pkg/config/runtime" "github.com/traefik/traefik/v2/pkg/config/static" "github.com/traefik/traefik/v2/pkg/log" @@ -157,6 +158,13 @@ func extractType(element interface{}) string { v := reflect.ValueOf(element).Elem() for i := 0; i < v.NumField(); i++ { field := v.Field(i) + + if field.Kind() == reflect.Map && field.Type().Elem() == reflect.TypeOf(dynamic.PluginConf{}) { + if keys := field.MapKeys(); len(keys) == 1 { + return keys[0].String() + } + } + if field.Kind() == reflect.Ptr && field.Elem().Kind() == reflect.Struct { if !field.IsNil() { return v.Type().Field(i).Name diff --git a/pkg/api/handler_test.go b/pkg/api/handler_test.go index 6028348ba..065bb9035 100644 --- a/pkg/api/handler_test.go +++ b/pkg/api/handler_test.go @@ -171,3 +171,112 @@ func TestHandler_RawData(t *testing.T) { }) } } + +func TestHandler_GetMiddleware(t *testing.T) { + testCases := []struct { + desc string + middlewareName string + conf runtime.Configuration + expectedStatus int + expected interface{} + }{ + { + desc: "Middleware not found", + middlewareName: "auth@myprovider", + conf: runtime.Configuration{ + Middlewares: map[string]*runtime.MiddlewareInfo{}, + }, + expectedStatus: http.StatusNotFound, + }, + { + desc: "Get middleware", + middlewareName: "auth@myprovider", + conf: runtime.Configuration{ + Middlewares: map[string]*runtime.MiddlewareInfo{ + "auth@myprovider": { + Middleware: &dynamic.Middleware{ + BasicAuth: &dynamic.BasicAuth{ + Users: []string{"admin:admin"}, + }, + }, + }, + }, + }, + expectedStatus: http.StatusOK, + expected: middlewareRepresentation{ + MiddlewareInfo: &runtime.MiddlewareInfo{ + Middleware: &dynamic.Middleware{ + BasicAuth: &dynamic.BasicAuth{ + Users: []string{"admin:admin"}, + }, + }, + }, + Name: "auth@myprovider", + Provider: "myprovider", + Type: "basicauth", + }, + }, + { + desc: "Get plugin middleware", + middlewareName: "myplugin@myprovider", + conf: runtime.Configuration{ + Middlewares: map[string]*runtime.MiddlewareInfo{ + "myplugin@myprovider": { + Middleware: &dynamic.Middleware{ + Plugin: map[string]dynamic.PluginConf{ + "mysuperplugin": { + "foo": "bar", + }, + }, + }, + }, + }, + }, + expectedStatus: http.StatusOK, + expected: middlewareRepresentation{ + MiddlewareInfo: &runtime.MiddlewareInfo{ + Middleware: &dynamic.Middleware{ + Plugin: map[string]dynamic.PluginConf{ + "mysuperplugin": { + "foo": "bar", + }, + }, + }, + }, + Name: "myplugin@myprovider", + Provider: "myprovider", + Type: "mysuperplugin", + }, + }, + } + + for _, test := range testCases { + test := test + t.Run(test.desc, func(t *testing.T) { + t.Parallel() + + handler := New(static.Configuration{API: &static.API{}, Global: &static.Global{}}, &test.conf) + server := httptest.NewServer(handler.createRouter()) + + resp, err := http.DefaultClient.Get(server.URL + "/api/http/middlewares/" + test.middlewareName) + require.NoError(t, err) + + assert.Equal(t, test.expectedStatus, resp.StatusCode) + + if test.expected == nil { + return + } + + data, err := ioutil.ReadAll(resp.Body) + require.NoError(t, err) + + err = resp.Body.Close() + require.NoError(t, err) + + expected, err := json.Marshal(test.expected) + require.NoError(t, err) + + assert.JSONEq(t, string(expected), string(data)) + }) + } +}