package tests import ( "bytes" "compress/gzip" "io" "net/http" "net/http/httptest" "testing" "git.coopcloud.tech/wiki-cafe/member-console/internal/middleware" ) func TestDecompressMiddleware(t *testing.T) { // Create a simple handler that reads the request body and returns it testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, err := io.ReadAll(r.Body) if err != nil { http.Error(w, "Error reading body", http.StatusInternalServerError) return } w.Write(body) }) // Apply decompression middleware handler := middleware.DecompressDefault()(testHandler) t.Run("No compression", func(t *testing.T) { // Create a request with no compression testData := []byte("test data with no compression") req := httptest.NewRequest("POST", "/", bytes.NewReader(testData)) rr := httptest.NewRecorder() // Call the handler handler.ServeHTTP(rr, req) // Check the response if rr.Code != http.StatusOK { t.Errorf("Expected status code %d, got %d", http.StatusOK, rr.Code) } if !bytes.Equal(testData, rr.Body.Bytes()) { t.Errorf("Response body does not match original data") } }) t.Run("Gzip compression", func(t *testing.T) { // Create gzip compressed data testData := []byte("test data with gzip compression") var buf bytes.Buffer gzWriter := gzip.NewWriter(&buf) _, err := gzWriter.Write(testData) if err != nil { t.Fatal(err) } if err := gzWriter.Close(); err != nil { t.Fatal(err) } // Create a request with gzip compression req := httptest.NewRequest("POST", "/", &buf) req.Header.Set("Content-Encoding", "gzip") rr := httptest.NewRecorder() // Call the handler handler.ServeHTTP(rr, req) // Check the response if rr.Code != http.StatusOK { t.Errorf("Expected status code %d, got %d", http.StatusOK, rr.Code) } if !bytes.Equal(testData, rr.Body.Bytes()) { t.Errorf("Response body does not match original data") } }) t.Run("Invalid gzip data", func(t *testing.T) { // Create invalid gzip data testData := []byte("this is not valid gzip data") req := httptest.NewRequest("POST", "/", bytes.NewReader(testData)) req.Header.Set("Content-Encoding", "gzip") rr := httptest.NewRecorder() // Call the handler handler.ServeHTTP(rr, req) // Check that we get a bad request if rr.Code != http.StatusBadRequest { t.Errorf("Expected status code %d, got %d", http.StatusBadRequest, rr.Code) } }) t.Run("Size limit", func(t *testing.T) { // Create oversized data testData := bytes.Repeat([]byte("a"), 11<<20) // 11MB var buf bytes.Buffer gzWriter := gzip.NewWriter(&buf) _, err := gzWriter.Write(testData) if err != nil { t.Fatal(err) } if err := gzWriter.Close(); err != nil { t.Fatal(err) } // Create a limited decompression middleware (10MB limit) limitedHandler := middleware.Decompress(nil)(testHandler) // Create a request with gzip compression req := httptest.NewRequest("POST", "/", &buf) req.Header.Set("Content-Encoding", "gzip") rr := httptest.NewRecorder() // Call the handler limitedHandler.ServeHTTP(rr, req) // The request should process but the body will be truncated if rr.Code != http.StatusOK { t.Errorf("Expected status code %d, got %d", http.StatusOK, rr.Code) } if len(rr.Body.Bytes()) >= 11<<20 { t.Errorf("Expected response to be truncated to less than 11MB, got %d bytes", len(rr.Body.Bytes())) } }) }