[MM-36563] Only read body from inter-plugin request if there is one (#17798)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e50cfca2ea
Коммит
2637a326fb
@@ -1414,23 +1414,27 @@ func TestInterpluginPluginHTTP(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *MyPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
func (p *MyPlugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.Path != "/api/v2/test" {
|
switch r.URL.Path {
|
||||||
return
|
case "/api/v2/test":
|
||||||
}
|
if r.URL.Query().Get("abc") != "xyz" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if r.URL.Query().Get("abc") != "xyz" {
|
if r.Header.Get("Mattermost-Plugin-ID") != "testplugininterclient" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.Header.Get("Mattermost-Plugin-ID") != "testplugininterclient" {
|
buf := bytes.Buffer{}
|
||||||
return
|
buf.ReadFrom(r.Body)
|
||||||
|
resp := "we got:" + buf.String()
|
||||||
|
w.WriteHeader(598)
|
||||||
|
w.Write([]byte(resp))
|
||||||
|
if r.URL.Path != "/api/v2/test" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "/nobody":
|
||||||
|
w.WriteHeader(599)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := bytes.Buffer{}
|
|
||||||
buf.ReadFrom(r.Body)
|
|
||||||
resp := "we got:" + buf.String()
|
|
||||||
w.WriteHeader(598)
|
|
||||||
w.Write([]byte(resp))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -1474,7 +1478,26 @@ func TestInterpluginPluginHTTP(t *testing.T) {
|
|||||||
if resp.StatusCode != 598 {
|
if resp.StatusCode != 598 {
|
||||||
return nil, "wrong status " + string(respbody)
|
return nil, "wrong status " + string(respbody)
|
||||||
}
|
}
|
||||||
return nil, string(respbody)
|
|
||||||
|
if string(respbody) != "we got:This is the request" {
|
||||||
|
return nil, "wrong response " + string(respbody)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err = http.NewRequest("GET", "/testplugininterserver/nobody", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err.Error()
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = p.API.PluginHTTP(req)
|
||||||
|
if resp == nil {
|
||||||
|
return nil, "Nil resp"
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != 599 {
|
||||||
|
return nil, "wrong status " + string(respbody)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, "ok"
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -1498,7 +1521,7 @@ func TestInterpluginPluginHTTP(t *testing.T) {
|
|||||||
hooks, err := th.App.GetPluginsEnvironment().HooksForPlugin("testplugininterclient")
|
hooks, err := th.App.GetPluginsEnvironment().HooksForPlugin("testplugininterclient")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
_, ret := hooks.MessageWillBePosted(nil, nil)
|
_, ret := hooks.MessageWillBePosted(nil, nil)
|
||||||
assert.Equal(t, "we got:This is the request", ret)
|
assert.Equal(t, "ok", ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestApiMetrics(t *testing.T) {
|
func TestApiMetrics(t *testing.T) {
|
||||||
|
|||||||
@@ -475,17 +475,20 @@ func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response {
|
|||||||
RequestURI: request.RequestURI,
|
RequestURI: request.RequestURI,
|
||||||
}
|
}
|
||||||
|
|
||||||
requestBody, err := ioutil.ReadAll(request.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
request.Body.Close()
|
|
||||||
request.Body = nil
|
|
||||||
|
|
||||||
_args := &Z_PluginHTTPArgs{
|
_args := &Z_PluginHTTPArgs{
|
||||||
Request: forwardedRequest,
|
Request: forwardedRequest,
|
||||||
RequestBody: requestBody,
|
}
|
||||||
|
|
||||||
|
if request.Body != nil {
|
||||||
|
requestBody, err := ioutil.ReadAll(request.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
request.Body.Close()
|
||||||
|
request.Body = nil
|
||||||
|
|
||||||
|
_args.RequestBody = requestBody
|
||||||
}
|
}
|
||||||
|
|
||||||
_returns := &Z_PluginHTTPReturns{}
|
_returns := &Z_PluginHTTPReturns{}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user