internal/httpd/fonts_test.go

332a13feaa444362bb4cc872ca95c62ccafcb64c
gitbay/internal/httpd/fonts_test.go history · blame · raw

45 lines · 1305 bytes

 1package httpd
 2
 3import (
 4	"net/http"
 5	"net/http/httptest"
 6	"regexp"
 7	"testing"
 8
 9	"gitbay.org/gitbay/internal/config"
10	"gitbay.org/gitbay/internal/web"
11)
12
13// TestStylesheetFontsAreServed: every font URL the embedded stylesheet asks
14// for has a route, and that route answers with the file. The route names
15// and the @font-face URLs were once maintained by hand and drifted, so
16// gitbay.org served no web font at all (#102).
17func TestStylesheetFontsAreServed(t *testing.T) {
18	s := New(config.Default(), nil)
19	byPattern := map[string]http.HandlerFunc{}
20	for _, r := range s.Routes() {
21		if r.Method == "GET" {
22			byPattern[r.Pattern] = r.Handler
23		}
24	}
25	urls := regexp.MustCompile(`url\((/static/fonts/[^)]+)\)`).FindAllSubmatch(web.StyleCSS, -1)
26	if len(urls) == 0 {
27		t.Fatal("stylesheet declares no font URLs")
28	}
29	for _, m := range urls {
30		u := string(m[1])
31		h, ok := byPattern[u]
32		if !ok {
33			t.Errorf("%s: stylesheet asks for it, route table has no route", u)
34			continue
35		}
36		rec := httptest.NewRecorder()
37		h(rec, httptest.NewRequest("GET", u, nil))
38		if rec.Code != http.StatusOK || rec.Body.Len() == 0 {
39			t.Errorf("%s: status %d, %d bytes", u, rec.Code, rec.Body.Len())
40		}
41		if ct := rec.Header().Get("Content-Type"); ct != "font/woff2" {
42			t.Errorf("%s: content-type %q", u, ct)
43		}
44	}
45}