You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.4 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gzip
  5. import (
  6. "archive/zip"
  7. "bytes"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptest"
  11. "testing"
  12. gzipp "github.com/klauspost/compress/gzip"
  13. "github.com/stretchr/testify/assert"
  14. macaron "gopkg.in/macaron.v1"
  15. )
  16. func setup(sampleResponse []byte) (*macaron.Macaron, *[]byte) {
  17. m := macaron.New()
  18. m.Use(Middleware())
  19. m.Get("/", func() *[]byte { return &sampleResponse })
  20. return m, &sampleResponse
  21. }
  22. func reqNoAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte) {
  23. // Request without accept gzip: Should not gzip
  24. resp := httptest.NewRecorder()
  25. req, err := http.NewRequest("GET", "/", nil)
  26. assert.NoError(t, err)
  27. m.ServeHTTP(resp, req)
  28. _, ok := resp.HeaderMap[contentEncodingHeader]
  29. assert.False(t, ok)
  30. contentEncoding := resp.Header().Get(contentEncodingHeader)
  31. assert.NotContains(t, contentEncoding, "gzip")
  32. result := resp.Body.Bytes()
  33. assert.Equal(t, *sampleResponse, result)
  34. }
  35. func reqAcceptGzip(t *testing.T, m *macaron.Macaron, sampleResponse *[]byte, expectGzip bool) {
  36. // Request without accept gzip: Should not gzip
  37. resp := httptest.NewRecorder()
  38. req, err := http.NewRequest("GET", "/", nil)
  39. assert.NoError(t, err)
  40. req.Header.Set(acceptEncodingHeader, "gzip")
  41. m.ServeHTTP(resp, req)
  42. _, ok := resp.HeaderMap[contentEncodingHeader]
  43. assert.Equal(t, ok, expectGzip)
  44. contentEncoding := resp.Header().Get(contentEncodingHeader)
  45. if expectGzip {
  46. assert.Contains(t, contentEncoding, "gzip")
  47. gzippReader, err := gzipp.NewReader(resp.Body)
  48. assert.NoError(t, err)
  49. result, err := ioutil.ReadAll(gzippReader)
  50. assert.NoError(t, err)
  51. assert.Equal(t, *sampleResponse, result)
  52. } else {
  53. assert.NotContains(t, contentEncoding, "gzip")
  54. result := resp.Body.Bytes()
  55. assert.Equal(t, *sampleResponse, result)
  56. }
  57. }
  58. func TestMiddlewareSmall(t *testing.T) {
  59. m, sampleResponse := setup([]byte("Small response"))
  60. reqNoAcceptGzip(t, m, sampleResponse)
  61. reqAcceptGzip(t, m, sampleResponse, false)
  62. }
  63. func TestMiddlewareLarge(t *testing.T) {
  64. b := make([]byte, MinSize+1)
  65. for i := range b {
  66. b[i] = byte(i % 256)
  67. }
  68. m, sampleResponse := setup(b)
  69. reqNoAcceptGzip(t, m, sampleResponse)
  70. // This should be gzipped as we accept gzip
  71. reqAcceptGzip(t, m, sampleResponse, true)
  72. }
  73. func TestMiddlewareGzip(t *testing.T) {
  74. b := make([]byte, MinSize*10)
  75. for i := range b {
  76. b[i] = byte(i % 256)
  77. }
  78. outputBuffer := bytes.NewBuffer([]byte{})
  79. gzippWriter := gzipp.NewWriter(outputBuffer)
  80. gzippWriter.Write(b)
  81. gzippWriter.Flush()
  82. gzippWriter.Close()
  83. output := outputBuffer.Bytes()
  84. m, sampleResponse := setup(output)
  85. reqNoAcceptGzip(t, m, sampleResponse)
  86. // This should not be gzipped even though we accept gzip
  87. reqAcceptGzip(t, m, sampleResponse, false)
  88. }
  89. func TestMiddlewareZip(t *testing.T) {
  90. b := make([]byte, MinSize*10)
  91. for i := range b {
  92. b[i] = byte(i % 256)
  93. }
  94. outputBuffer := bytes.NewBuffer([]byte{})
  95. zipWriter := zip.NewWriter(outputBuffer)
  96. fileWriter, err := zipWriter.Create("default")
  97. assert.NoError(t, err)
  98. fileWriter.Write(b)
  99. //fileWriter.Close()
  100. zipWriter.Close()
  101. output := outputBuffer.Bytes()
  102. m, sampleResponse := setup(output)
  103. reqNoAcceptGzip(t, m, sampleResponse)
  104. // This should not be gzipped even though we accept gzip
  105. reqAcceptGzip(t, m, sampleResponse, false)
  106. }