Skip to content

Commit

Permalink
Fix leaflet and hlsjs js files not being served
Browse files Browse the repository at this point in the history
  • Loading branch information
jlelse committed Dec 20, 2023
1 parent 1bca07e commit 2ef34e0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
3 changes: 2 additions & 1 deletion httpFs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 62 additions & 0 deletions httpFs_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 2ef34e0

Please sign in to comment.