handlers_test.go 3.6 KB

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