diff --git a/httpFs.go b/httpFs.go index 9bc5a73..1bb2cf9 100644 --- a/httpFs.go +++ b/httpFs.go @@ -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) diff --git a/httpFs_test.go b/httpFs_test.go new file mode 100644 index 0000000..82a368d --- /dev/null +++ b/httpFs_test.go @@ -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) + }) + } +}