Skip to content

Commit

Permalink
fix: handle invalid request method in RemoveHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 6, 2024
1 parent c2b911a commit 5452c58
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions web/rm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func RemoveHandler(w http.ResponseWriter, r *http.Request) {
slog.Info("RemoveHandler is called.")
if r.Method != "POST" {
slog.Error("Invalid request method", "method", r.Method)
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
Expand Down
108 changes: 108 additions & 0 deletions web/rm_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package web

// import (
// "net/http"
// "net/http/httptest"
// "os"
// "path/filepath"
// "strings"
// "testing"
// )

// func TestRemoveHandler(t *testing.T) {
// // TEST_UPLOAD_DIR := "/test"

// tempDir, err := os.MkdirTemp("", "test-upload-dir")
// if err != nil {
// t.Fatal(err)
// }
// defer os.RemoveAll(tempDir)

// // Override TEST_UPLOAD_DIR to use the temporary directory
// // uploadDir := TEST_UPLOAD_DIR
// // defer func() { TEST_UPLOAD_DIR = uploadDir }()
// // TEST_UPLOAD_DIR = tempDir

// // Create a test file to be removed
// testFilePath := filepath.Join(UPLOAD_DIR, "testfile.txt")
// dst, err := os.Create(testFilePath)
// if err != nil {
// t.Fatal(err)
// }
// defer dst.Close()

// err = os.WriteFile(testFilePath, []byte("test content"), 0644)
// if err != nil {
// t.Fatal(err)
// }

// // Prepare the request
// formData := strings.NewReader("path=/s3/testfile.txt")
// req, err := http.NewRequest("POST", "/s3/remove", formData)
// if err != nil {
// t.Fatal(err)
// }
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

// // Record the response
// responseRecorder := httptest.NewRecorder()
// handler := http.HandlerFunc(RemoveHandler)

// // Call the handler
// handler.ServeHTTP(responseRecorder, req)

// // Check the status code
// if status := responseRecorder.Code; status != http.StatusFound {
// t.Errorf("handler returned wrong status code: got %v want %v",
// status, http.StatusFound)
// }

// // Check the file is removed
// if _, err := os.Stat(testFilePath); !os.IsNotExist(err) {
// t.Errorf("expected file to be removed, but it still exists: %v", err)
// }
// }

// func TestRemoveHandler_InvalidMethod(t *testing.T) {
// // Prepare the request with invalid method
// req, err := http.NewRequest("GET", "/s3/remove", nil)
// if err != nil {
// t.Fatal(err)
// }

// // Record the response
// rr := httptest.NewRecorder()
// handler := http.HandlerFunc(RemoveHandler)

// // Call the handler
// handler.ServeHTTP(rr, req)

// // Check the status code
// if status := rr.Code; status != http.StatusMethodNotAllowed {
// t.Errorf("handler returned wrong status code: got %v want %v",
// status, http.StatusMethodNotAllowed)
// }
// }

// func TestRemoveHandler_FileNotFound(t *testing.T) {
// // Prepare the request
// formData := strings.NewReader("path=/s3/nonexistentfile.txt")
// req, err := http.NewRequest("POST", "/s3/remove", formData)
// if err != nil {
// t.Fatal(err)
// }
// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

// // Record the response
// rr := httptest.NewRecorder()
// handler := http.HandlerFunc(RemoveHandler)

// // Call the handler
// handler.ServeHTTP(rr, req)

// // Check the status code
// if status := rr.Code; status != http.StatusInternalServerError {
// t.Errorf("handler returned wrong status code: got %v want %v",
// status, http.StatusInternalServerError)
// }
// }

0 comments on commit 5452c58

Please sign in to comment.