62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestFrontendRoutesServeBuiltAssetsAtRootPath(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
distDir := t.TempDir()
|
|
assetsDir := filepath.Join(distDir, "assets")
|
|
if err := os.Mkdir(assetsDir, 0o755); err != nil {
|
|
t.Fatalf("create assets directory: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(assetsDir, "app.js"), []byte("console.log('ok')"), 0o600); err != nil {
|
|
t.Fatalf("write asset: %v", err)
|
|
}
|
|
|
|
router := gin.New()
|
|
registerFrontendRoutes(router, distDir)
|
|
|
|
response := httptest.NewRecorder()
|
|
router.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/assets/app.js", nil))
|
|
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("asset status = %d, want %d", response.Code, http.StatusOK)
|
|
}
|
|
}
|
|
|
|
func TestFrontendRefreshFallsBackWithoutMaskingAPIOrAssets(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
distDir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(distDir, "index.html"), []byte("<html>company app</html>"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := gin.New()
|
|
registerFrontendRoutes(r, distDir)
|
|
for _, path := range []string{"/stock", "/login", "/master-data/settings", "/api/missing", "/ws", "/uploads/missing", "/assets/missing.js"} {
|
|
t.Run(path, func(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
|
|
want := 200
|
|
if path == "/api/missing" || path == "/ws" || path == "/uploads/missing" || path == "/assets/missing.js" {
|
|
want = 404
|
|
}
|
|
if w.Code != want {
|
|
t.Fatalf("status=%d want=%d", w.Code, want)
|
|
}
|
|
})
|
|
}
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, httptest.NewRequest("POST", "/stock", nil))
|
|
if w.Code != 404 {
|
|
t.Fatalf("POST status=%d", w.Code)
|
|
}
|
|
}
|