- Created main application entry point in `main.go`. - Added configuration management in `config/config.go` and tests in `config/config_test.go`. - Implemented handlers for book and stationery uploads in `handlers/book.go` and `handlers/stationery.go`, including validation logic. - Established database connection and services in `services/database.go` and `services/book_service.go`. - Defined models for books and stationery in `models/book.go` and `models/stationery.go`. - Set up Firebase integration for image uploads in `services/firebase.go`. - Created migration scripts for database schema in `migrations/001_create_tables.sql` and subsequent updates. - Added CORS and error handling middleware. - Included comprehensive tests for handlers, services, and utilities. - Documented API endpoints and usage in `README.md` and migration instructions in `migrations/README.md`. - Introduced `.gitignore` to exclude unnecessary files and directories. - Added Go module support with `go.mod` and `go.sum` files. - Implemented utility functions for slug generation and validation in `utils/slug.go` and `utils/validation.go`.
43 lines
1003 B
Go
43 lines
1003 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"jd-book-uploader/config"
|
|
)
|
|
|
|
func TestSetupCORS(t *testing.T) {
|
|
cfg := &config.Config{
|
|
FrontendURL: "http://localhost:5173",
|
|
}
|
|
|
|
app := fiber.New()
|
|
app.Use(SetupCORS(cfg))
|
|
|
|
app.Get("/test", func(c *fiber.Ctx) error {
|
|
return c.SendString("OK")
|
|
})
|
|
|
|
// Test CORS preflight request
|
|
req := httptest.NewRequest("OPTIONS", "/test", nil)
|
|
req.Header.Set("Origin", "http://localhost:5173")
|
|
req.Header.Set("Access-Control-Request-Method", "GET")
|
|
|
|
resp, err := app.Test(req)
|
|
if err != nil {
|
|
t.Fatalf("Test request failed: %v", err)
|
|
}
|
|
|
|
if resp.StatusCode != fiber.StatusNoContent {
|
|
t.Errorf("Expected status %d, got %d", fiber.StatusNoContent, resp.StatusCode)
|
|
}
|
|
|
|
// Check CORS headers
|
|
allowOrigin := resp.Header.Get("Access-Control-Allow-Origin")
|
|
if allowOrigin != "http://localhost:5173" {
|
|
t.Errorf("Expected Access-Control-Allow-Origin %s, got %s", "http://localhost:5173", allowOrigin)
|
|
}
|
|
}
|