MM-11855 Add App.HTTPService to allow mocking of HTTP client (#9359)

* MM-11855 Add App.HTTPService to allow mocking of HTTP client

* Initialize HTTPService earlier
Этот коммит содержится в:
Harrison Healey
2018-09-07 09:24:18 -04:00
коммит произвёл GitHub
родитель 2910007033
Коммит 0027d99855
10 изменённых файлов: 179 добавлений и 49 удалений

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

@@ -7,12 +7,10 @@ import (
"crypto/ecdsa"
"fmt"
"html/template"
"net"
"net/http"
"path"
"reflect"
"strconv"
"strings"
"sync"
"sync/atomic"
@@ -100,6 +98,8 @@ type App struct {
diagnosticId string
phase2PermissionsMigrationComplete bool
HTTPService HTTPService
}
var appCount = 0
@@ -125,6 +125,9 @@ func New(options ...Option) (outApp *App, outErr error) {
clientConfig: make(map[string]string),
licenseListeners: map[string]func(){},
}
app.HTTPService = MakeHTTPService(app)
defer func() {
if outErr != nil {
app.Shutdown()
@@ -285,6 +288,8 @@ func (a *App) Shutdown() {
mlog.Info("Server stopped")
a.DisableConfigWatch()
a.HTTPService.Close()
}
var accountMigrationInterface func(*App) einterfaces.AccountMigrationInterface
@@ -505,43 +510,6 @@ func (a *App) HTMLTemplates() *template.Template {
return nil
}
func (a *App) HTTPClient(trustURLs bool) *http.Client {
insecure := a.Config().ServiceSettings.EnableInsecureOutgoingConnections != nil && *a.Config().ServiceSettings.EnableInsecureOutgoingConnections
if trustURLs {
return utils.NewHTTPClient(insecure, nil, nil)
}
allowHost := func(host string) bool {
if a.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*a.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if host == allowed {
return true
}
}
return false
}
allowIP := func(ip net.IP) bool {
if !utils.IsReservedIP(ip) {
return true
}
if a.Config().ServiceSettings.AllowedUntrustedInternalConnections == nil {
return false
}
for _, allowed := range strings.Fields(*a.Config().ServiceSettings.AllowedUntrustedInternalConnections) {
if _, ipRange, err := net.ParseCIDR(allowed); err == nil && ipRange.Contains(ip) {
return true
}
}
return false
}
return utils.NewHTTPClient(insecure, allowHost, allowIP)
}
func (a *App) Handle404(w http.ResponseWriter, r *http.Request) {
err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)