embedfs_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package util
  2. import (
  3. "embed"
  4. "github.com/stretchr/testify/require"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. "time"
  9. )
  10. var (
  11. modTime = time.Now()
  12. //go:embed embedfs
  13. testFs embed.FS
  14. testFsCached = &CachingEmbedFS{ModTime: modTime, FS: testFs}
  15. )
  16. func TestCachingEmbedFS(t *testing.T) {
  17. s := http.FileServer(http.FS(testFsCached))
  18. rr := httptest.NewRecorder()
  19. req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
  20. s.ServeHTTP(rr, req)
  21. require.Equal(t, 200, rr.Code)
  22. lastModified := rr.Header().Get("Last-Modified")
  23. rr = httptest.NewRecorder()
  24. req, _ = http.NewRequest("GET", "/embedfs/test.txt", nil)
  25. req.Header.Set("If-Modified-Since", lastModified)
  26. s.ServeHTTP(rr, req)
  27. require.Equal(t, 304, rr.Code) // Huzzah!
  28. }
  29. func TestCachingEmbedFS_Range(t *testing.T) {
  30. s := http.FileServer(http.FS(testFsCached))
  31. rr := httptest.NewRecorder()
  32. req, _ := http.NewRequest("GET", "/embedfs/test.txt", nil)
  33. req.Header.Set("Range", "bytes=1-20")
  34. s.ServeHTTP(rr, req)
  35. require.Equal(t, 206, rr.Code)
  36. require.Equal(t, "his is a test file f", rr.Body.String())
  37. }