WebUI/ui/ui_test.go
Theta-Dev 834092a101
Some checks failed
continuous-integration/drone/push Build is failing
add ui base, api client
2022-02-03 21:40:18 +01:00

87 lines
1.5 KiB
Go

package ui
import (
"net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"testing"
"code.thetadev.de/TSGRain/WebUI/src/fixtures"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestUI(t *testing.T) {
tests := []struct {
name string
path string
contains string
cached bool
}{
{
name: "index_html",
path: "/",
contains: "TSGRain",
cached: false,
},
{
name: "index_html2",
path: "/index.html",
contains: "TSGRain",
cached: false,
},
{
name: "index_js",
path: path.Join("/assets", getIndexJS()),
contains: "app",
cached: true,
},
}
router := gin.New()
Register(router, 3)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", tt.path, nil)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), tt.contains)
ccHeader := w.Header().Get("Cache-Control")
if tt.cached {
assert.Equal(t, "public, max-age=604800, immutable", ccHeader)
} else {
assert.Equal(t, "", ccHeader)
}
})
}
}
func getIndexJS() string {
baseDir := "ui/dist/assets"
indexExp := regexp.MustCompile(`index\.[0-9a-f]{8}\.js`)
fixtures.CdProjectRoot()
distDir, err := os.Open(baseDir)
if err != nil {
panic(err)
}
list, err := distDir.Readdir(-1)
if err != nil {
panic(err)
}
for _, f := range list {
if indexExp.MatchString(f.Name()) {
return f.Name()
}
}
panic("no index.js found")
}