|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "io/fs" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/carlmjohnson/requests" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_httpFs(t *testing.T) { |
| 16 | + app := &goBlog{ |
| 17 | + cfg: createDefaultTestConfig(t), |
| 18 | + } |
| 19 | + _ = app.initConfig(false) |
| 20 | + |
| 21 | + t.Run("Leaflet", func(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + testFs(t, app, leafletFiles, "/-/", []string{ |
| 24 | + "/-/leaflet/leaflet.js", |
| 25 | + "/-/leaflet/leaflet.css", |
| 26 | + "/-/leaflet/markercluster.js", |
| 27 | + "/-/leaflet/markercluster.css", |
| 28 | + "/-/leaflet/markercluster.default.css", |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("Hls.js", func(t *testing.T) { |
| 33 | + t.Parallel() |
| 34 | + testFs(t, app, hlsjsFiles, "/-/", []string{ |
| 35 | + "/-/hlsjs/hls.js", |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +func testFs(t *testing.T, app *goBlog, files fs.FS, prefix string, paths []string) { |
| 42 | + handler := app.serveFs(files, prefix) |
| 43 | + |
| 44 | + for _, fp := range paths { |
| 45 | + t.Run(fp, func(t *testing.T) { |
| 46 | + fp := fp |
| 47 | + |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + w := httptest.NewRecorder() |
| 51 | + r, _ := requests.URL(fp).Method(http.MethodGet).Request(context.Background()) |
| 52 | + |
| 53 | + handler.ServeHTTP(w, r) |
| 54 | + |
| 55 | + result := w.Result() |
| 56 | + bodyContent, _ := io.ReadAll(result.Body) |
| 57 | + result.Body.Close() |
| 58 | + |
| 59 | + require.NotEmpty(t, bodyContent) |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments