Installable as an app (PWA): manifests, offline-only service worker, icons
This commit is contained in:
parent
b26360e8f3
commit
44708d5f3c
16 changed files with 246 additions and 0 deletions
46
internal/web/pwa_test.go
Normal file
46
internal/web/pwa_test.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPWA(t *testing.T) {
|
||||
h := (&Server{site: defaultSite()}).Handler()
|
||||
get := func(path string) *httptest.ResponseRecorder {
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("%s: status %d", path, rec.Code)
|
||||
}
|
||||
return rec
|
||||
}
|
||||
|
||||
for path, start := range map[string]string{"/manifest.webmanifest": "/", "/admin.webmanifest": "/admin"} {
|
||||
rec := get(path)
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/manifest+json" {
|
||||
t.Errorf("%s: content type %q", path, ct)
|
||||
}
|
||||
var m manifest
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &m); err != nil {
|
||||
t.Fatalf("%s: %v", path, err)
|
||||
}
|
||||
if m.StartURL != start || m.ID != start || m.Scope != "/" || m.Display != "standalone" {
|
||||
t.Errorf("%s: %+v", path, m)
|
||||
}
|
||||
// Every icon must exist, or Chrome won't offer to install.
|
||||
for _, icon := range m.Icons {
|
||||
if ct := get(icon.Src).Header().Get("Content-Type"); ct != icon.Type {
|
||||
t.Errorf("%s: %s is %q, manifest says %q", path, icon.Src, ct, icon.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ct := get("/sw.js").Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/javascript") {
|
||||
t.Errorf("/sw.js: content type %q", ct)
|
||||
}
|
||||
get("/static/offline.html")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue