57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package admin
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sundynix/pets-be/internal/config"
|
|
"github.com/sundynix/pets-be/internal/httpapi"
|
|
)
|
|
|
|
func TestAdminAtRootAndAPIRoutes(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
r := gin.New()
|
|
httpapi.New(nil, nil, &config.Config{}).Register(r)
|
|
Register(r)
|
|
assets, err := fs.ReadDir(distFS, "dist/assets")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var script string
|
|
for _, asset := range assets {
|
|
if strings.HasSuffix(asset.Name(), ".js") {
|
|
script = "/assets/" + asset.Name()
|
|
break
|
|
}
|
|
}
|
|
if script == "" {
|
|
t.Fatal("admin bundle has no JavaScript asset")
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
path string
|
|
status int
|
|
body string
|
|
}{
|
|
{path: "/", status: http.StatusOK, body: "<div id=\"root\"></div>"},
|
|
{path: "/admin/breeds", status: http.StatusOK, body: "<div id=\"root\"></div>"},
|
|
{path: "/admin/walks", status: http.StatusOK, body: "<div id=\"root\"></div>"},
|
|
{path: "/admin/files", status: http.StatusOK, body: "<div id=\"root\"></div>"},
|
|
{path: script, status: http.StatusOK},
|
|
{path: "/brand-logo.png", status: http.StatusOK},
|
|
{path: "/api/ping", status: http.StatusOK, body: "pong"},
|
|
{path: "/api/missing", status: http.StatusNotFound},
|
|
{path: "/assets/missing.js", status: http.StatusNotFound},
|
|
} {
|
|
recorder := httptest.NewRecorder()
|
|
r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, tc.path, nil))
|
|
if recorder.Code != tc.status || (tc.body != "" && !strings.Contains(recorder.Body.String(), tc.body)) {
|
|
t.Errorf("GET %s: got %d %q, want %d containing %q", tc.path, recorder.Code, recorder.Body.String(), tc.status, tc.body)
|
|
}
|
|
}
|
|
}
|