|
| 1 | +package gzip |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestStaticFileWithGzip(t *testing.T) { |
| 17 | + // Create a temporary directory and file for testing |
| 18 | + tmpDir, err := os.MkdirTemp("", "gzip_static_test") |
| 19 | + require.NoError(t, err) |
| 20 | + defer os.RemoveAll(tmpDir) |
| 21 | + |
| 22 | + // Create a test file |
| 23 | + testFile := filepath.Join(tmpDir, "test.txt") |
| 24 | + testContent := "This is a test file for static gzip compression testing. " + |
| 25 | + "It should be long enough to trigger gzip compression." |
| 26 | + err = os.WriteFile(testFile, []byte(testContent), 0o600) |
| 27 | + require.NoError(t, err) |
| 28 | + |
| 29 | + // Set up Gin router with gzip middleware and static file serving |
| 30 | + gin.SetMode(gin.TestMode) |
| 31 | + router := gin.New() |
| 32 | + router.Use(Gzip(DefaultCompression)) |
| 33 | + router.Static("/static", tmpDir) |
| 34 | + |
| 35 | + // Test static file request with gzip support |
| 36 | + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/static/test.txt", nil) |
| 37 | + req.Header.Add(headerAcceptEncoding, "gzip") |
| 38 | + |
| 39 | + w := httptest.NewRecorder() |
| 40 | + router.ServeHTTP(w, req) |
| 41 | + |
| 42 | + // The response should be successful and compressed |
| 43 | + assert.Equal(t, http.StatusOK, w.Code) |
| 44 | + |
| 45 | + // This is what should happen but currently fails due to the bug |
| 46 | + // The static handler initially sets status to 404, causing gzip headers to be removed |
| 47 | + assert.Equal(t, "gzip", w.Header().Get(headerContentEncoding), "Static file should be gzip compressed") |
| 48 | + assert.Equal(t, headerAcceptEncoding, w.Header().Get(headerVary), "Vary header should be set") |
| 49 | + |
| 50 | + // The compressed content should be smaller than original |
| 51 | + assert.Less(t, w.Body.Len(), len(testContent), "Compressed content should be smaller") |
| 52 | +} |
| 53 | + |
| 54 | +func TestStaticFileWithoutGzip(t *testing.T) { |
| 55 | + // Create a temporary directory and file for testing |
| 56 | + tmpDir, err := os.MkdirTemp("", "gzip_static_test") |
| 57 | + require.NoError(t, err) |
| 58 | + defer os.RemoveAll(tmpDir) |
| 59 | + |
| 60 | + // Create a test file |
| 61 | + testFile := filepath.Join(tmpDir, "test.txt") |
| 62 | + testContent := "This is a test file." |
| 63 | + err = os.WriteFile(testFile, []byte(testContent), 0o600) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + // Set up Gin router with gzip middleware and static file serving |
| 67 | + gin.SetMode(gin.TestMode) |
| 68 | + router := gin.New() |
| 69 | + router.Use(Gzip(DefaultCompression)) |
| 70 | + router.Static("/static", tmpDir) |
| 71 | + |
| 72 | + // Test static file request without gzip support |
| 73 | + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/static/test.txt", nil) |
| 74 | + // No Accept-Encoding header |
| 75 | + |
| 76 | + w := httptest.NewRecorder() |
| 77 | + router.ServeHTTP(w, req) |
| 78 | + |
| 79 | + // The response should be successful and not compressed |
| 80 | + assert.Equal(t, http.StatusOK, w.Code) |
| 81 | + assert.Equal(t, "", w.Header().Get(headerContentEncoding), "Content should not be compressed") |
| 82 | + assert.Equal(t, "", w.Header().Get(headerVary), "Vary header should not be set") |
| 83 | + assert.Equal(t, testContent, w.Body.String(), "Content should match original") |
| 84 | +} |
| 85 | + |
| 86 | +func TestStaticFileNotFound(t *testing.T) { |
| 87 | + // Create a temporary directory (but no files) |
| 88 | + tmpDir, err := os.MkdirTemp("", "gzip_static_test") |
| 89 | + require.NoError(t, err) |
| 90 | + defer os.RemoveAll(tmpDir) |
| 91 | + |
| 92 | + // Set up Gin router with gzip middleware and static file serving |
| 93 | + gin.SetMode(gin.TestMode) |
| 94 | + router := gin.New() |
| 95 | + router.Use(Gzip(DefaultCompression)) |
| 96 | + router.Static("/static", tmpDir) |
| 97 | + |
| 98 | + // Test request for non-existent file |
| 99 | + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/static/nonexistent.txt", nil) |
| 100 | + req.Header.Add(headerAcceptEncoding, "gzip") |
| 101 | + |
| 102 | + w := httptest.NewRecorder() |
| 103 | + router.ServeHTTP(w, req) |
| 104 | + |
| 105 | + // The response should be 404 and not compressed (this should work correctly) |
| 106 | + assert.Equal(t, http.StatusNotFound, w.Code) |
| 107 | + assert.Equal(t, "", w.Header().Get(headerContentEncoding), "404 response should not be compressed") |
| 108 | + assert.Equal(t, "", w.Header().Get(headerVary), "Vary header should be removed for error responses") |
| 109 | +} |
| 110 | + |
| 111 | +func TestStaticDirectoryListing(t *testing.T) { |
| 112 | + // Create a temporary directory with a file |
| 113 | + tmpDir, err := os.MkdirTemp("", "gzip_static_test") |
| 114 | + require.NoError(t, err) |
| 115 | + defer os.RemoveAll(tmpDir) |
| 116 | + |
| 117 | + // Create a test file |
| 118 | + testFile := filepath.Join(tmpDir, "test.txt") |
| 119 | + err = os.WriteFile(testFile, []byte("test content"), 0o600) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + // Set up Gin router with gzip middleware and static file serving |
| 123 | + gin.SetMode(gin.TestMode) |
| 124 | + router := gin.New() |
| 125 | + router.Use(Gzip(DefaultCompression)) |
| 126 | + router.Static("/static", tmpDir) |
| 127 | + |
| 128 | + // Test directory listing request |
| 129 | + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/static/", nil) |
| 130 | + req.Header.Add(headerAcceptEncoding, "gzip") |
| 131 | + |
| 132 | + w := httptest.NewRecorder() |
| 133 | + router.ServeHTTP(w, req) |
| 134 | + |
| 135 | + // Note: Gin's default static handler doesn't enable directory listing |
| 136 | + // so this will return 404, which should NOT be compressed |
| 137 | + assert.Equal(t, http.StatusNotFound, w.Code) |
| 138 | + assert.Equal(t, "", w.Header().Get(headerContentEncoding), "404 response should not be compressed") |
| 139 | + assert.Equal(t, "", w.Header().Get(headerVary), "Vary header should be removed for error responses") |
| 140 | +} |
| 141 | + |
| 142 | +// This test demonstrates the specific issue mentioned in #122 |
| 143 | +func TestStaticFileGzipHeadersBug(t *testing.T) { |
| 144 | + // Create a temporary directory and file for testing |
| 145 | + tmpDir, err := os.MkdirTemp("", "gzip_static_test") |
| 146 | + require.NoError(t, err) |
| 147 | + defer os.RemoveAll(tmpDir) |
| 148 | + |
| 149 | + // Create a test file |
| 150 | + testFile := filepath.Join(tmpDir, "test.js") |
| 151 | + testContent := "console.log('This is a JavaScript file that should be compressed when served as a static file');" |
| 152 | + err = os.WriteFile(testFile, []byte(testContent), 0o600) |
| 153 | + require.NoError(t, err) |
| 154 | + |
| 155 | + // Set up Gin router with gzip middleware and static file serving |
| 156 | + gin.SetMode(gin.TestMode) |
| 157 | + router := gin.New() |
| 158 | + router.Use(Gzip(DefaultCompression)) |
| 159 | + router.Static("/assets", tmpDir) |
| 160 | + |
| 161 | + // Test static file request with gzip support |
| 162 | + req, _ := http.NewRequestWithContext(context.Background(), "GET", "/assets/test.js", nil) |
| 163 | + req.Header.Add(headerAcceptEncoding, "gzip") |
| 164 | + |
| 165 | + w := httptest.NewRecorder() |
| 166 | + router.ServeHTTP(w, req) |
| 167 | + |
| 168 | + t.Logf("Response Status: %d", w.Code) |
| 169 | + t.Logf("Content-Encoding: %s", w.Header().Get(headerContentEncoding)) |
| 170 | + t.Logf("Vary: %s", w.Header().Get(headerVary)) |
| 171 | + t.Logf("Content-Length: %s", w.Header().Get("Content-Length")) |
| 172 | + t.Logf("Body Length: %d", w.Body.Len()) |
| 173 | + |
| 174 | + // This test will currently fail due to the bug described in issue #122 |
| 175 | + // The static handler sets status to 404 initially, causing gzip middleware to remove headers |
| 176 | + assert.Equal(t, http.StatusOK, w.Code) |
| 177 | + |
| 178 | + // These assertions will fail with the current bug: |
| 179 | + // - Content-Encoding header will be empty instead of "gzip" |
| 180 | + // - Vary header will be empty instead of "Accept-Encoding" |
| 181 | + // - Content will not be compressed |
| 182 | + if w.Header().Get(headerContentEncoding) != "gzip" { |
| 183 | + t.Errorf("BUG REPRODUCED: Static file is not being gzip compressed. Content-Encoding: %q, expected: %q", |
| 184 | + w.Header().Get(headerContentEncoding), "gzip") |
| 185 | + } |
| 186 | + |
| 187 | + if w.Header().Get(headerVary) != headerAcceptEncoding { |
| 188 | + t.Errorf("BUG REPRODUCED: Vary header missing. Vary: %q, expected: %q", |
| 189 | + w.Header().Get(headerVary), headerAcceptEncoding) |
| 190 | + } |
| 191 | +} |
0 commit comments