1
mirror of https://github.com/jlelse/GoBlog synced 2024-06-15 10:27:09 +00:00

Fix leaflet and hlsjs js files not being served

This commit is contained in:
Jan-Lukas Else 2023-12-20 15:10:41 +01:00
parent 1bca07e0ce
commit 2ef34e0c89
2 changed files with 64 additions and 1 deletions

View File

@ -21,7 +21,8 @@ func (a *goBlog) serveFs(f fs.FS, basePath string) http.HandlerFunc {
switch path.Ext(fileName) {
case ".js":
w.Header().Set(contentType, contenttype.JSUTF8)
_ = a.min.Get().Minify(contenttype.JS, w, file)
// Can't minify, because minify throws errors for some JS files
_, _ = io.Copy(w, file)
case ".css":
w.Header().Set(contentType, contenttype.CSSUTF8)
_ = a.min.Get().Minify(contenttype.CSS, w, file)

62
httpFs_test.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"context"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"testing"
"github.com/carlmjohnson/requests"
"github.com/stretchr/testify/require"
)
func Test_httpFs(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = app.initConfig(false)
t.Run("Leaflet", func(t *testing.T) {
t.Parallel()
testFs(t, app, leafletFiles, "/-/", []string{
"/-/leaflet/leaflet.js",
"/-/leaflet/leaflet.css",
"/-/leaflet/markercluster.js",
"/-/leaflet/markercluster.css",
"/-/leaflet/markercluster.default.css",
})
})
t.Run("Hls.js", func(t *testing.T) {
t.Parallel()
testFs(t, app, hlsjsFiles, "/-/", []string{
"/-/hlsjs/hls.js",
})
})
}
func testFs(t *testing.T, app *goBlog, files fs.FS, prefix string, paths []string) {
handler := app.serveFs(files, prefix)
for _, fp := range paths {
t.Run(fp, func(t *testing.T) {
fp := fp
t.Parallel()
w := httptest.NewRecorder()
r, _ := requests.URL(fp).Method(http.MethodGet).Request(context.Background())
handler.ServeHTTP(w, r)
result := w.Result()
bodyContent, _ := io.ReadAll(result.Body)
result.Body.Close()
require.NotEmpty(t, bodyContent)
})
}
}