MM-10863: Handle non-API errors with redirect to webapp (#8943)

* MM-10863: Handle non-API errors with redirect to webapp

* Properly shutdown the app in the new test
This commit is contained in:
Jesús Espino
2018-06-14 09:00:40 +02:00
committed by Carlos Tadeu Panato Junior
parent a6d815e05a
commit 695c5d6bf8
2 changed files with 64 additions and 2 deletions

View File

@@ -157,8 +157,12 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.Err.IsOAuth = false
}
w.WriteHeader(c.Err.StatusCode)
w.Write([]byte(c.Err.ToJson()))
if IsApiCall(r) || len(r.Header.Get("X-Mobile-App")) > 0 {
w.WriteHeader(c.Err.StatusCode)
w.Write([]byte(c.Err.ToJson()))
} else {
utils.RenderWebAppError(w, r, c.Err, c.App.AsymmetricSigningKey())
}
if c.App.Metrics != nil {
c.App.Metrics.IncrementHttpError()

58
web/handlers_test.go Normal file
View File

@@ -0,0 +1,58 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package web
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/assert"
)
func handlerForTest(c *Context, w http.ResponseWriter, r *http.Request) {
c.Err = model.NewAppError("loginWithSaml", "api.user.saml.not_available.app_error", nil, "", http.StatusFound)
}
func TestHandlerServeHTTPErrors(t *testing.T) {
a, err := app.New(app.StoreOverride(testStore), app.DisableConfigWatch)
defer a.Shutdown()
web := NewWeb(a, a.Srv.Router)
if err != nil {
panic(err)
}
handler := web.NewHandler(handlerForTest)
var flagtests = []struct {
name string
url string
mobile bool
redirect bool
}{
{"redirect on destkop non-api endpoint", "/login/sso/saml", false, true},
{"not redirect on destkop api endpoint", "/api/v4/test", false, false},
{"not redirect on mobile non-api endpoint", "/login/sso/saml", true, false},
{"not redirect on mobile api endpoint", "/api/v4/test", true, false},
}
for _, tt := range flagtests {
t.Run(tt.name, func(t *testing.T) {
request := httptest.NewRequest("GET", tt.url, nil)
if tt.mobile {
request.Header.Add("X-Mobile-App", "mattermost")
}
response := httptest.NewRecorder()
handler.ServeHTTP(response, request)
if tt.redirect {
assert.Contains(t, response.Body.String(), "/error?message=")
} else {
assert.NotContains(t, response.Body.String(), "/error?message=")
}
})
}
}