From f8f80d80df1e27e2ed4dcc91518bf504ab7f3e34 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Thu, 26 Jul 2018 08:33:56 -0400 Subject: Fix plugin.ServeHTTP subpath (#9161) * test ServicePluginRequest with subpath * handle subpath when routing to plugin.ServeHTTP --- app/plugin_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'app/plugin_test.go') diff --git a/app/plugin_test.go b/app/plugin_test.go index 02b492d28..c051324a7 100644 --- a/app/plugin_test.go +++ b/app/plugin_test.go @@ -4,6 +4,8 @@ package app import ( + "bytes" + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -52,6 +54,52 @@ func TestServePluginRequest(t *testing.T) { assert.Equal(t, http.StatusNotImplemented, w.Result().StatusCode) } +func TestPrivateServePluginRequest(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + testCases := []struct { + Description string + ConfigFunc func(cfg *model.Config) + URL string + ExpectedURL string + }{ + { + "no subpath", + func(cfg *model.Config) {}, + "/plugins/id/endpoint", + "/endpoint", + }, + { + "subpath", + func(cfg *model.Config) { *cfg.ServiceSettings.SiteURL += "/subpath" }, + "/subpath/plugins/id/endpoint", + "/endpoint", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.Description, func(t *testing.T) { + th.App.UpdateConfig(testCase.ConfigFunc) + expectedBody := []byte("body") + request := httptest.NewRequest(http.MethodGet, testCase.URL, bytes.NewReader(expectedBody)) + recorder := httptest.NewRecorder() + + handler := func(context *plugin.Context, w http.ResponseWriter, r *http.Request) { + assert.Equal(t, testCase.ExpectedURL, r.URL.Path) + + body, _ := ioutil.ReadAll(r.Body) + assert.Equal(t, expectedBody, body) + } + + request = mux.SetURLVars(request, map[string]string{"plugin_id": "id"}) + + th.App.servePluginRequest(recorder, request, handler) + }) + } + +} + func TestHandlePluginRequest(t *testing.T) { th := Setup().InitBasic() defer th.TearDown() -- cgit v1.2.3-1-g7c22