github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/handlers_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/masterhung0112/hk_server/v5/model"
    14  )
    15  
    16  func handlerForGzip(c *Context, w http.ResponseWriter, r *http.Request) {
    17  	// gziphandler default requires body size greater than 1400 bytes
    18  	var body [1400]byte
    19  	w.Write(body[:])
    20  }
    21  
    22  func testAPIHandlerGzipMode(t *testing.T, name string, h http.Handler, token string) {
    23  	t.Run("Handler: "+name+" No Accept-Encoding", func(t *testing.T) {
    24  		resp := httptest.NewRecorder()
    25  		req := httptest.NewRequest("GET", "/api/v4/test", nil)
    26  		req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
    27  		h.ServeHTTP(resp, req)
    28  		assert.Equal(t, http.StatusOK, resp.Code)
    29  		assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
    30  	})
    31  
    32  	t.Run("Handler: "+name+" With Accept-Encoding", func(t *testing.T) {
    33  		resp := httptest.NewRecorder()
    34  		req := httptest.NewRequest("GET", "/api/v4/test", nil)
    35  		req.Header.Set("Accept-Encoding", "gzip")
    36  		req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
    37  
    38  		h.ServeHTTP(resp, req)
    39  		assert.Equal(t, http.StatusOK, resp.Code)
    40  		assert.Equal(t, "gzip", resp.Header().Get("Content-Encoding"))
    41  	})
    42  }
    43  
    44  func testAPIHandlerNoGzipMode(t *testing.T, name string, h http.Handler, token string) {
    45  	t.Run("Handler: "+name+" No Accept-Encoding", func(t *testing.T) {
    46  		resp := httptest.NewRecorder()
    47  		req := httptest.NewRequest("GET", "/api/v4/test", nil)
    48  		req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
    49  
    50  		h.ServeHTTP(resp, req)
    51  		assert.Equal(t, http.StatusOK, resp.Code)
    52  		assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
    53  	})
    54  
    55  	t.Run("Handler: "+name+" With Accept-Encoding", func(t *testing.T) {
    56  		resp := httptest.NewRecorder()
    57  		req := httptest.NewRequest("GET", "/api/v4/test", nil)
    58  		req.Header.Set("Accept-Encoding", "gzip")
    59  		req.Header.Set(model.HEADER_AUTH, "Bearer "+token)
    60  
    61  		h.ServeHTTP(resp, req)
    62  		assert.Equal(t, http.StatusOK, resp.Code)
    63  		assert.Equal(t, "", resp.Header().Get("Content-Encoding"))
    64  	})
    65  }
    66  
    67  func TestAPIHandlersWithGzip(t *testing.T) {
    68  	th := Setup(t)
    69  	defer th.TearDown()
    70  
    71  	api := Init(th.App, th.Server.Router)
    72  	session, _ := th.App.GetSession(th.Client.AuthToken)
    73  
    74  	t.Run("with WebserverMode == \"gzip\"", func(t *testing.T) {
    75  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.WebserverMode = "gzip" })
    76  
    77  		testAPIHandlerGzipMode(t, "ApiHandler", api.ApiHandler(handlerForGzip), "")
    78  		testAPIHandlerGzipMode(t, "ApiSessionRequired", api.ApiSessionRequired(handlerForGzip), session.Token)
    79  		testAPIHandlerGzipMode(t, "ApiSessionRequiredMfa", api.ApiSessionRequiredMfa(handlerForGzip), session.Token)
    80  		testAPIHandlerGzipMode(t, "ApiHandlerTrustRequester", api.ApiHandlerTrustRequester(handlerForGzip), "")
    81  		testAPIHandlerGzipMode(t, "ApiSessionRequiredTrustRequester", api.ApiSessionRequiredTrustRequester(handlerForGzip), session.Token)
    82  	})
    83  
    84  	t.Run("with WebserverMode == \"nogzip\"", func(t *testing.T) {
    85  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.WebserverMode = "nogzip" })
    86  
    87  		testAPIHandlerNoGzipMode(t, "ApiHandler", api.ApiHandler(handlerForGzip), "")
    88  		testAPIHandlerNoGzipMode(t, "ApiSessionRequired", api.ApiSessionRequired(handlerForGzip), session.Token)
    89  		testAPIHandlerNoGzipMode(t, "ApiSessionRequiredMfa", api.ApiSessionRequiredMfa(handlerForGzip), session.Token)
    90  		testAPIHandlerNoGzipMode(t, "ApiHandlerTrustRequester", api.ApiHandlerTrustRequester(handlerForGzip), "")
    91  		testAPIHandlerNoGzipMode(t, "ApiSessionRequiredTrustRequester", api.ApiSessionRequiredTrustRequester(handlerForGzip), session.Token)
    92  	})
    93  }