summaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorJesús Espino <jespinog@gmail.com>2018-06-14 09:00:40 +0200
committerCarlos Tadeu Panato Junior <ctadeu@gmail.com>2018-06-14 09:00:40 +0200
commit695c5d6bf82f5a5c58aa0a22b4911439f08a80fb (patch)
treed0ad8946e816cfa4e4b307828178f39cebae1c60 /web
parenta6d815e05a656b1f80a377ae713bc0e31e4a1ef1 (diff)
downloadchat-695c5d6bf82f5a5c58aa0a22b4911439f08a80fb.tar.gz
chat-695c5d6bf82f5a5c58aa0a22b4911439f08a80fb.tar.bz2
chat-695c5d6bf82f5a5c58aa0a22b4911439f08a80fb.zip
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
Diffstat (limited to 'web')
-rw-r--r--web/handlers.go8
-rw-r--r--web/handlers_test.go58
2 files changed, 64 insertions, 2 deletions
diff --git a/web/handlers.go b/web/handlers.go
index aac88aa3a..fe77241e3 100644
--- a/web/handlers.go
+++ b/web/handlers.go
@@ -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()
diff --git a/web/handlers_test.go b/web/handlers_test.go
new file mode 100644
index 000000000..b4c89e50f
--- /dev/null
+++ b/web/handlers_test.go
@@ -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=")
+ }
+ })
+ }
+}