MM-10417 Improve HTTPService for use in image proxy (#9966)

* Replaced httpservice with proper http.Client

* Added HTTPService.MakeTransport

* Expose timeouts used by HTTPServiceImpl

* Add additional documentation to HTTPService

* Remove MockedHTTPService

* Fix missing license
Этот коммит содержится в:
Harrison Healey
2018-12-12 11:39:14 -05:00
коммит произвёл GitHub
родитель f42c00ee53
Коммит 749a3e7538
9 изменённых файлов: 60 добавлений и 151 удалений

Просмотреть файл

@@ -6,7 +6,6 @@ package app
import (
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
@@ -16,7 +15,6 @@ import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/testutils"
)
type TestHelper struct {
@@ -32,8 +30,6 @@ type TestHelper struct {
tempConfigPath string
tempWorkspace string
MockedHTTPService *testutils.MockedHTTPService
}
func setupTestHelper(enterprise bool) *TestHelper {
@@ -133,13 +129,6 @@ func (me *TestHelper) InitBasic() *TestHelper {
return me
}
func (me *TestHelper) MockHTTPService(handler http.Handler) *TestHelper {
me.MockedHTTPService = testutils.MakeMockedHTTPService(handler)
me.App.HTTPService = me.MockedHTTPService
return me
}
func (me *TestHelper) MakeEmail() string {
return "success_" + model.NewId() + "@simulator.amazonses.com"
}

Просмотреть файл

@@ -1,65 +0,0 @@
package app
import (
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMockHTTPService(t *testing.T) {
getCalled := false
putCalled := false
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/get" && r.Method == http.MethodGet {
getCalled = true
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
} else if r.URL.Path == "/put" && r.Method == http.MethodPut {
putCalled = true
w.WriteHeader(http.StatusCreated)
w.Write([]byte("CREATED"))
} else {
w.WriteHeader(http.StatusNotFound)
}
})
th := Setup().MockHTTPService(handler)
defer th.TearDown()
url := th.MockedHTTPService.Server.URL
t.Run("GET", func(t *testing.T) {
client := th.App.HTTPService.MakeClient(false)
resp, err := client.Get(url + "/get")
defer consumeAndClose(resp)
bodyContents, _ := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "OK", string(bodyContents))
assert.True(t, getCalled)
})
t.Run("PUT", func(t *testing.T) {
client := th.App.HTTPService.MakeClient(false)
request, _ := http.NewRequest(http.MethodPut, url+"/put", nil)
resp, err := client.Do(request)
defer consumeAndClose(resp)
bodyContents, _ := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
assert.Equal(t, http.StatusCreated, resp.StatusCode)
assert.Equal(t, "CREATED", string(bodyContents))
assert.True(t, putCalled)
})
}

Просмотреть файл

@@ -27,7 +27,6 @@ import (
"strings"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/httpservice"
"github.com/mattermost/mattermost-server/utils"
)
@@ -132,7 +131,7 @@ func (a *App) DoActionRequest(rawURL string, body []byte) (*http.Response, *mode
req.Header.Set("Accept", "application/json")
// Allow access to plugin routes for action buttons
var httpClient *httpservice.Client
var httpClient *http.Client
url, _ := url.Parse(rawURL)
siteURL, _ := url.Parse(*a.Config().ServiceSettings.SiteURL)
subpath, _ := utils.GetSubpathFromConfig(a.Config())

Просмотреть файл

@@ -425,9 +425,6 @@ func (s *Server) Shutdown() error {
s.DisableConfigWatch()
if s.HTTPService != nil {
s.HTTPService.Close()
}
mlog.Info("Server stopped")
return nil
}