Files
mattermost/api4/handlers_test.go
Agniva De Sarker 4a2715974d MM-27149: optimize initBasic (#15063)
* MM-27149: optimize initBasic

Mostly, all tests just needed the user initialization part and not
the channel and group creation. So we move the user initialization inside
the Setup call. This avoids unnecessary DB calls which take around 250-300ms
on average.

And we make the login requests concurrently to shave off a few more ms.
According to my tests, the 2 login calls take 140 ms on average, which
shaves off 70ms.

So approximately, we shave off 350ms per test. And there are 114 occurences
of these. So around 39 seconds.

* make initlogin only for Setup/SetupEnterprise

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
2020-07-22 13:50:33 +05:30

94 lines
3.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/v5/model"
)
func handlerForGzip(c *Context, w http.ResponseWriter, r *http.Request) {
// gziphandler default requires body size greater than 1400 bytes
var body [1400]byte
w.Write(body[:])
}
func testAPIHandlerGzipMode(t *testing.T, name string, h http.Handler, token string) {
t.Run("Handler: "+name+" No Accept-Encoding", func(t *testing.T) {
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v4/test", nil)
req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
h.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
})
t.Run("Handler: "+name+" With Accept-Encoding", func(t *testing.T) {
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v4/test", nil)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
h.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "gzip", resp.Header().Get("Content-Encoding"))
})
}
func testAPIHandlerNoGzipMode(t *testing.T, name string, h http.Handler, token string) {
t.Run("Handler: "+name+" No Accept-Encoding", func(t *testing.T) {
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v4/test", nil)
req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
h.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
})
t.Run("Handler: "+name+" With Accept-Encoding", func(t *testing.T) {
resp := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v4/test", nil)
req.Header.Set("Accept-Encoding", "gzip")
req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
h.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
})
}
func TestAPIHandlersWithGzip(t *testing.T) {
th := Setup(t)
defer th.TearDown()
api := Init(th.Server, th.Server.AppOptions, th.Server.Router)
session, _ := th.App.GetSession(th.Client.AuthToken)
t.Run("with WebserverMode == \"gzip\"", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.WebserverMode = "gzip" })
testAPIHandlerGzipMode(t, "ApiHandler", api.ApiHandler(handlerForGzip), "")
testAPIHandlerGzipMode(t, "ApiSessionRequired", api.ApiSessionRequired(handlerForGzip), session.Token)
testAPIHandlerGzipMode(t, "ApiSessionRequiredMfa", api.ApiSessionRequiredMfa(handlerForGzip), session.Token)
testAPIHandlerGzipMode(t, "ApiHandlerTrustRequester", api.ApiHandlerTrustRequester(handlerForGzip), "")
testAPIHandlerGzipMode(t, "ApiSessionRequiredTrustRequester", api.ApiSessionRequiredTrustRequester(handlerForGzip), session.Token)
})
t.Run("with WebserverMode == \"nogzip\"", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.WebserverMode = "nogzip" })
testAPIHandlerNoGzipMode(t, "ApiHandler", api.ApiHandler(handlerForGzip), "")
testAPIHandlerNoGzipMode(t, "ApiSessionRequired", api.ApiSessionRequired(handlerForGzip), session.Token)
testAPIHandlerNoGzipMode(t, "ApiSessionRequiredMfa", api.ApiSessionRequiredMfa(handlerForGzip), session.Token)
testAPIHandlerNoGzipMode(t, "ApiHandlerTrustRequester", api.ApiHandlerTrustRequester(handlerForGzip), "")
testAPIHandlerNoGzipMode(t, "ApiSessionRequiredTrustRequester", api.ApiSessionRequiredTrustRequester(handlerForGzip), session.Token)
})
}