SEBRAUC/ui/ui_test.go
Theta-Dev df78f37d86
Some checks failed
continuous-integration/drone/push Build is failing
add server_test
2021-12-25 18:03:43 +01:00

87 lines
1.5 KiB
Go

package ui
import (
"net/http"
"net/http/httptest"
"os"
"path"
"regexp"
"testing"
"code.thetadev.de/TSGRain/SEBRAUC/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: "SEBRAUC",
cached: false,
},
{
name: "index_html2",
path: "/index.html",
contains: "SEBRAUC",
cached: false,
},
{
name: "index_js",
path: path.Join("/assets", getIndexJS()),
contains: "SEBRAUC",
cached: true,
},
}
router := gin.New()
Register(router)
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")
}