MM-13606 Remove consumeAndClose and clean up integration response handling (#10066)
* MM-13606 Remove consumeAndClose * Allow overriding HTTPService's request timeout * MM-13606 Clean up integration response handling * Properly close httptest servers * Address feedback * Only call buf.Bytes when necessary * Properly check for errors in doOutgoingWebhookRequest * Add comment explaining ignored ioutil.ReadAll errors
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e67fe4c89d
Коммит
1a3ccaf305
@@ -4,11 +4,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/services/httpservice"
|
||||
)
|
||||
|
||||
func TestMoveCommand(t *testing.T) {
|
||||
@@ -257,3 +265,99 @@ func TestHandleCommandResponse(t *testing.T) {
|
||||
_, err = th.App.HandleCommandResponse(command, args, resp, builtIn)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestDoCommandRequest(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
cfg.ServiceSettings.AllowedUntrustedInternalConnections = model.NewString("127.0.0.1")
|
||||
cfg.ServiceSettings.EnableCommands = model.NewBool(true)
|
||||
})
|
||||
|
||||
t.Run("with a valid text response", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, strings.NewReader("Hello, World!"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.NotNil(t, resp)
|
||||
assert.Equal(t, "Hello, World!", resp.Text)
|
||||
})
|
||||
|
||||
t.Run("with a valid json response", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
io.Copy(w, strings.NewReader(`{"text": "Hello, World!"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.NotNil(t, resp)
|
||||
assert.Equal(t, "Hello, World!", resp.Text)
|
||||
})
|
||||
|
||||
t.Run("with a large text response", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(w, InfiniteReader{})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Since we limit the length of the response, no error will be returned and resp.Text will be a finite string
|
||||
|
||||
_, resp, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, resp)
|
||||
})
|
||||
|
||||
t.Run("with a large, valid json response", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
io.Copy(w, io.MultiReader(strings.NewReader(`{"text": "`), InfiniteReader{}, strings.NewReader(`"}`)))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
|
||||
})
|
||||
|
||||
t.Run("with a large, invalid json response", func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
io.Copy(w, InfiniteReader{})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
|
||||
})
|
||||
|
||||
t.Run("with a slow response", func(t *testing.T) {
|
||||
timeout := 100 * time.Millisecond
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(timeout + time.Millisecond)
|
||||
io.Copy(w, strings.NewReader(`{"text": "Hello, World!"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
th.App.HTTPService.(*httpservice.HTTPServiceImpl).RequestTimeout = timeout
|
||||
defer func() {
|
||||
th.App.HTTPService.(*httpservice.HTTPServiceImpl).RequestTimeout = httpservice.RequestTimeout
|
||||
}()
|
||||
|
||||
_, _, err := th.App.doCommandRequest(&model.Command{URL: server.URL}, url.Values{})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "api.command.execute_command.failed.app_error", err.Id)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user