github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/api4/handlers_test.go (about)

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