feat: implement WYSIWYG markdown editor with Go backend and React frontend
- Backend: Go HTTP server with Cobra CLI (--data-dir, --port, --host flags) - CRUD REST API for markdown files with JSON error responses - File storage in flat directory structure (flat structure, .md files only) - Comprehensive logrus logging for all operations - Static file serving for frontend build (./frontend/dist) - Frontend: React + TypeScript + Tailwind CSS - Markdown editor with live GFM preview - File management: list, create, open, save, delete - Theme system (Dark, Light, System) with persistence - Responsive design for desktop and mobile - Backend tests (storage, API handlers) and frontend tests
This commit is contained in:
253
internal/api/handlers.go
Normal file
253
internal/api/handlers.go
Normal file
@@ -0,0 +1,253 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"eval/internal/config"
|
||||
"eval/internal/storage"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Handlers holds the API handlers
|
||||
type Handlers struct {
|
||||
store *storage.Storage
|
||||
config *config.Config
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
// NewHandlers creates a new Handlers instance
|
||||
func NewHandlers(store *storage.Storage, config *config.Config, logger *logrus.Logger) *Handlers {
|
||||
return &Handlers{
|
||||
store: store,
|
||||
config: config,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ErrorResponse represents an error response
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
// writeJSON writes a JSON response
|
||||
func (h *Handlers) writeJSON(w http.ResponseWriter, status int, data interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||
h.logger.Errorf("failed to encode response: %v", err)
|
||||
http.Error(w, `{"error": "internal server error"}`, http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// writeError writes an error response
|
||||
func (h *Handlers) writeError(w http.ResponseWriter, status int, message string) {
|
||||
h.writeJSON(w, status, ErrorResponse{Error: message})
|
||||
}
|
||||
|
||||
// listFilesHandler handles GET /api/files - list all markdown files
|
||||
func (h *Handlers) listFilesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
h.logger.Info("listing files")
|
||||
files, err := h.store.ListFiles()
|
||||
if err != nil {
|
||||
h.logger.Errorf("failed to list files: %v", err)
|
||||
h.writeError(w, http.StatusInternalServerError, "failed to list files")
|
||||
return
|
||||
}
|
||||
h.writeJSON(w, http.StatusOK, files)
|
||||
}
|
||||
|
||||
// getFileHandler handles GET /api/files/{filename} - get a specific file
|
||||
func (h *Handlers) getFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/files/")
|
||||
filename := path
|
||||
if filename == "" {
|
||||
h.writeError(w, http.StatusBadRequest, "filename is required")
|
||||
return
|
||||
}
|
||||
h.logger.WithField("filename", filename).Info("getting file")
|
||||
file, err := h.store.GetFile(filename)
|
||||
if err != nil {
|
||||
if err.Error() == "file not found" {
|
||||
h.writeError(w, http.StatusNotFound, "file not found")
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("failed to get file: %v", err)
|
||||
h.writeError(w, http.StatusInternalServerError, "failed to get file")
|
||||
return
|
||||
}
|
||||
h.writeJSON(w, http.StatusOK, file)
|
||||
}
|
||||
|
||||
// createFileHandler handles POST /api/files - create a new file
|
||||
func (h *Handlers) createFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var file storage.FileContent
|
||||
if err := json.NewDecoder(r.Body).Decode(&file); err != nil {
|
||||
h.writeError(w, http.StatusBadRequest, "invalid JSON")
|
||||
return
|
||||
}
|
||||
if file.Filename == "" {
|
||||
h.writeError(w, http.StatusBadRequest, "filename is required")
|
||||
return
|
||||
}
|
||||
h.logger.WithField("filename", file.Filename).Info("creating file")
|
||||
result, err := h.store.CreateFile(file.Filename, file.Content)
|
||||
if err != nil {
|
||||
if err.Error() == "file already exists" {
|
||||
h.writeError(w, http.StatusConflict, "file already exists")
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("failed to create file: %v", err)
|
||||
h.writeError(w, http.StatusInternalServerError, "failed to create file")
|
||||
return
|
||||
}
|
||||
h.writeJSON(w, http.StatusCreated, result)
|
||||
}
|
||||
|
||||
// updateFileHandler handles PUT /api/files/{filename} - update a file
|
||||
func (h *Handlers) updateFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/files/")
|
||||
filename := path
|
||||
if filename == "" {
|
||||
h.writeError(w, http.StatusBadRequest, "filename is required")
|
||||
return
|
||||
}
|
||||
var file storage.FileContent
|
||||
if err := json.NewDecoder(r.Body).Decode(&file); err != nil {
|
||||
h.writeError(w, http.StatusBadRequest, "invalid JSON")
|
||||
return
|
||||
}
|
||||
h.logger.WithField("filename", filename).Info("updating file")
|
||||
result, err := h.store.UpdateFile(filename, file.Content)
|
||||
if err != nil {
|
||||
if err.Error() == "file not found" {
|
||||
h.writeError(w, http.StatusNotFound, "file not found")
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("failed to update file: %v", err)
|
||||
h.writeError(w, http.StatusInternalServerError, "failed to update file")
|
||||
return
|
||||
}
|
||||
h.writeJSON(w, http.StatusOK, result)
|
||||
}
|
||||
|
||||
// deleteFileHandler handles DELETE /api/files/{filename} - delete a file
|
||||
func (h *Handlers) deleteFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/files/")
|
||||
filename := path
|
||||
if filename == "" {
|
||||
h.writeError(w, http.StatusBadRequest, "filename is required")
|
||||
return
|
||||
}
|
||||
h.logger.WithField("filename", filename).Info("deleting file")
|
||||
if err := h.store.DeleteFile(filename); err != nil {
|
||||
if err.Error() == "file not found" {
|
||||
h.writeError(w, http.StatusNotFound, "file not found")
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("failed to delete file: %v", err)
|
||||
h.writeError(w, http.StatusInternalServerError, "failed to delete file")
|
||||
return
|
||||
}
|
||||
h.writeJSON(w, http.StatusOK, map[string]string{"message": "file deleted"})
|
||||
}
|
||||
|
||||
// registerAPIRoutes registers all API routes
|
||||
func (h *Handlers) registerAPIRoutes(router *http.ServeMux) {
|
||||
router.HandleFunc("GET /api/files", h.listFilesHandler)
|
||||
router.HandleFunc("GET /api/files/", h.getFileHandler)
|
||||
router.HandleFunc("POST /api/files", h.createFileHandler)
|
||||
router.HandleFunc("PUT /api/files/", h.updateFileHandler)
|
||||
router.HandleFunc("DELETE /api/files/", h.deleteFileHandler)
|
||||
}
|
||||
|
||||
// ServeStaticHandler serves static files
|
||||
type ServeStaticHandler struct {
|
||||
fs http.FileSystem
|
||||
}
|
||||
|
||||
// NewServeStaticHandler creates a new static file handler
|
||||
func NewServeStaticHandler(dir string) (*ServeStaticHandler, error) {
|
||||
fs := http.Dir(dir)
|
||||
_, err := fs.Open(".")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("static directory not found: %w", err)
|
||||
}
|
||||
return &ServeStaticHandler{fs: fs}, nil
|
||||
}
|
||||
|
||||
// ServeHTTP serves files from the static directory
|
||||
func (h *ServeStaticHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
file, err := h.fs.Open(r.URL.Path)
|
||||
if err != nil {
|
||||
// If file not found, serve index.html for SPA routing
|
||||
file, err = h.fs.Open("/index.html")
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Get file info to determine content type
|
||||
info, err := file.Stat()
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeContent(w, r, info.Name(), info.ModTime(), file)
|
||||
}
|
||||
|
||||
// SetupStaticHandler sets up the static file handler
|
||||
func (h *Handlers) SetupStaticHandler(buildDir string) (http.HandlerFunc, error) {
|
||||
handler, err := NewServeStaticHandler(buildDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.ServeHTTP, nil
|
||||
}
|
||||
|
||||
// SetupRoutes sets up all routes and returns the router
|
||||
func (h *Handlers) SetupRoutes(buildDir string) (*http.ServeMux, error) {
|
||||
router := http.NewServeMux()
|
||||
h.registerAPIRoutes(router)
|
||||
|
||||
// Setup static handler
|
||||
if _, err := os.Stat(buildDir); err == nil {
|
||||
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" || r.URL.Path == "/index.html" {
|
||||
http.ServeFile(w, r, filepath.Join(buildDir, "index.html"))
|
||||
return
|
||||
}
|
||||
// Try to serve the file, if not found, serve index.html
|
||||
file, err := http.Dir(buildDir).Open(r.URL.Path)
|
||||
if err != nil {
|
||||
http.ServeFile(w, r, filepath.Join(buildDir, "index.html"))
|
||||
return
|
||||
}
|
||||
file.Close()
|
||||
http.ServeFile(w, r, filepath.Join(buildDir, r.URL.Path))
|
||||
})
|
||||
} else {
|
||||
h.logger.Warnf("build directory not found: %s, static file serving disabled", buildDir)
|
||||
}
|
||||
|
||||
return router, nil
|
||||
}
|
||||
|
||||
// StartServer starts the HTTP server
|
||||
func (h *Handlers) StartServer(router *http.ServeMux) error {
|
||||
addr := fmt.Sprintf("%s:%d", h.config.Host, h.config.Port)
|
||||
h.logger.Infof("starting server on %s", addr)
|
||||
return http.ListenAndServe(addr, router)
|
||||
}
|
||||
380
internal/api/handlers_test.go
Normal file
380
internal/api/handlers_test.go
Normal file
@@ -0,0 +1,380 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"eval/internal/config"
|
||||
"eval/internal/storage"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func setupTestEnvironment(t *testing.T) (*Handlers, string) {
|
||||
t.Helper()
|
||||
tempDir := t.TempDir()
|
||||
cfg := config.NewConfig()
|
||||
cfg.DataDir = tempDir
|
||||
|
||||
logger := logrus.New()
|
||||
logger.SetLevel(logrus.FatalLevel)
|
||||
|
||||
store := storage.NewStorage(tempDir)
|
||||
handlers := NewHandlers(store, cfg, logger)
|
||||
return handlers, tempDir
|
||||
}
|
||||
|
||||
func TestHandlers_ListFiles_Empty(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/files", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.listFilesHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
var files []storage.FileMetadata
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &files); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if len(files) != 0 {
|
||||
t.Errorf("expected 0 files, got %d", len(files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_ListFiles_Multiple(t *testing.T) {
|
||||
handlers, tempDir := setupTestEnvironment(t)
|
||||
|
||||
// Create test files
|
||||
testFiles := []string{"file1.md", "file2.md", "file3.md"}
|
||||
for _, name := range testFiles {
|
||||
content := "# Test " + name
|
||||
if err := os.WriteFile(filepath.Join(tempDir, name), []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/files", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.listFilesHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
var files []storage.FileMetadata
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &files); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if len(files) != 3 {
|
||||
t.Errorf("expected 3 files, got %d", len(files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_GetFile(t *testing.T) {
|
||||
handlers, tempDir := setupTestEnvironment(t)
|
||||
|
||||
// Create test file
|
||||
filename := "test.md"
|
||||
content := "# Hello World\n\nThis is a test file."
|
||||
if err := os.WriteFile(filepath.Join(tempDir, filename), []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/files/"+filename, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.getFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
var file storage.FileContent
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &file); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if file.Content != content {
|
||||
t.Errorf("expected content %s, got %s", content, file.Content)
|
||||
}
|
||||
if file.Filename != filename {
|
||||
t.Errorf("expected filename %s, got %s", filename, file.Filename)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_GetFile_NotFound(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/files/nonexistent.md", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.getFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected status 404, got %d", w.Code)
|
||||
}
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &errResp); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_CreateFile(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
filename := "newfile.md"
|
||||
content := "# New File\n\nCreated via API."
|
||||
body := &storage.FileContent{
|
||||
Filename: filename,
|
||||
Content: content,
|
||||
Title: "New File",
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/files", bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.createFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("expected status 201, got %d", w.Code)
|
||||
}
|
||||
|
||||
var file storage.FileContent
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &file); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if file.Content != content {
|
||||
t.Errorf("expected content %s, got %s", content, file.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_CreateFile_Validation(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
filename string
|
||||
expected int
|
||||
}{
|
||||
{"empty filename", "", http.StatusBadRequest},
|
||||
{"missing extension", "noext", http.StatusInternalServerError},
|
||||
{"invalid extension", "file.txt", http.StatusInternalServerError},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
body := &storage.FileContent{
|
||||
Filename: tt.filename,
|
||||
Content: "content",
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/files", bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.createFileHandler(w, req)
|
||||
|
||||
if w.Code != tt.expected {
|
||||
t.Errorf("expected status %d, got %d", tt.expected, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_UpdateFile(t *testing.T) {
|
||||
handlers, tempDir := setupTestEnvironment(t)
|
||||
|
||||
// Create initial file
|
||||
filename := "update.md"
|
||||
content := "# Original"
|
||||
if err := os.WriteFile(filepath.Join(tempDir, filename), []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// Update file
|
||||
newContent := "# Updated\n\nContent changed."
|
||||
body := &storage.FileContent{
|
||||
Filename: filename,
|
||||
Content: newContent,
|
||||
Title: "Updated",
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
|
||||
req := httptest.NewRequest("PUT", "/api/files/"+filename, bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.updateFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
var file storage.FileContent
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &file); err != nil {
|
||||
t.Fatalf("failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if file.Content != newContent {
|
||||
t.Errorf("expected content %s, got %s", newContent, file.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_UpdateFile_NotFound(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
body := &storage.FileContent{
|
||||
Filename: "nonexistent.md",
|
||||
Content: "content",
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
|
||||
req := httptest.NewRequest("PUT", "/api/files/nonexistent.md", bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.updateFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected status 404, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_DeleteFile(t *testing.T) {
|
||||
handlers, tempDir := setupTestEnvironment(t)
|
||||
|
||||
// Create file
|
||||
filename := "delete.md"
|
||||
content := "# To be deleted"
|
||||
if err := os.WriteFile(filepath.Join(tempDir, filename), []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to create test file: %v", err)
|
||||
}
|
||||
|
||||
// Delete file
|
||||
req := httptest.NewRequest("DELETE", "/api/files/"+filename, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.deleteFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Verify file is deleted
|
||||
if _, err := os.Stat(filepath.Join(tempDir, filename)); !os.IsNotExist(err) {
|
||||
t.Errorf("file should be deleted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_DeleteFile_NotFound(t *testing.T) {
|
||||
handlers, _ := setupTestEnvironment(t)
|
||||
|
||||
req := httptest.NewRequest("DELETE", "/api/files/nonexistent.md", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.deleteFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected status 404, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlers_CRUDRoundTrip(t *testing.T) {
|
||||
handlers, tempDir := setupTestEnvironment(t)
|
||||
|
||||
// Create
|
||||
filename := "roundtrip.md"
|
||||
content := "# Round Trip Test"
|
||||
body := &storage.FileContent{
|
||||
Filename: filename,
|
||||
Content: content,
|
||||
Title: "Round Trip Test",
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
|
||||
req := httptest.NewRequest("POST", "/api/files", bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handlers.createFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("Create failed with status %d", w.Code)
|
||||
}
|
||||
|
||||
var created storage.FileContent
|
||||
json.Unmarshal(w.Body.Bytes(), &created)
|
||||
|
||||
// Read
|
||||
req = httptest.NewRequest("GET", "/api/files/"+filename, nil)
|
||||
w = httptest.NewRecorder()
|
||||
handlers.getFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("Read failed with status %d", w.Code)
|
||||
}
|
||||
|
||||
var retrieved storage.FileContent
|
||||
json.Unmarshal(w.Body.Bytes(), &retrieved)
|
||||
|
||||
if retrieved.Content != content {
|
||||
t.Errorf("Content mismatch after read: expected %s, got %s", content, retrieved.Content)
|
||||
}
|
||||
|
||||
// Update
|
||||
newContent := "# Round Trip Updated"
|
||||
body = &storage.FileContent{
|
||||
Filename: filename,
|
||||
Content: newContent,
|
||||
Title: "Round Trip Updated",
|
||||
}
|
||||
jsonBody, _ = json.Marshal(body)
|
||||
|
||||
req = httptest.NewRequest("PUT", "/api/files/"+filename, bytes.NewBuffer(jsonBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w = httptest.NewRecorder()
|
||||
handlers.updateFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("Update failed with status %d", w.Code)
|
||||
}
|
||||
|
||||
// Delete
|
||||
req = httptest.NewRequest("DELETE", "/api/files/"+filename, nil)
|
||||
w = httptest.NewRecorder()
|
||||
handlers.deleteFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("Delete failed with status %d", w.Code)
|
||||
}
|
||||
|
||||
// Verify deletion
|
||||
req = httptest.NewRequest("GET", "/api/files/"+filename, nil)
|
||||
w = httptest.NewRecorder()
|
||||
handlers.getFileHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("Expected 404 after delete, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Verify file is deleted on disk
|
||||
if _, err := os.Stat(filepath.Join(tempDir, filename)); !os.IsNotExist(err) {
|
||||
t.Errorf("file should be deleted on disk")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user