diff options
Diffstat (limited to 'internal/server/handlers')
| -rw-r--r-- | internal/server/handlers/handlers.go | 1 | ||||
| -rw-r--r-- | internal/server/handlers/handlers_test.go | 32 |
2 files changed, 33 insertions, 0 deletions
diff --git a/internal/server/handlers/handlers.go b/internal/server/handlers/handlers.go index 958ca30..f39c6d5 100644 --- a/internal/server/handlers/handlers.go +++ b/internal/server/handlers/handlers.go @@ -20,6 +20,7 @@ func Routes(conf *config.Config) *http.ServeMux { } mux := http.NewServeMux() mux.HandleFunc("GET /{$}", handler.home) + mux.Handle("GET /css/", http.FileServerFS(web.Files)) return mux } diff --git a/internal/server/handlers/handlers_test.go b/internal/server/handlers/handlers_test.go index b2ae43e..75d25a2 100644 --- a/internal/server/handlers/handlers_test.go +++ b/internal/server/handlers/handlers_test.go @@ -37,6 +37,9 @@ func TestRoutes(t *testing.T) { want int }{ {"home", http.MethodGet, "/", http.StatusOK}, + {"stylesheet", http.MethodGet, "/css/style.css", http.StatusOK}, + {"missing stylesheet", http.MethodGet, "/css/nope.css", http.StatusNotFound}, + {"templates not served", http.MethodGet, "/templates/index.html", http.StatusNotFound}, {"unknown path", http.MethodGet, "/nonsense", http.StatusNotFound}, {"wrong method on home", http.MethodPost, "/", http.StatusMethodNotAllowed}, } @@ -51,6 +54,35 @@ func TestRoutes(t *testing.T) { } } +func TestStylesheetIsServed(t *testing.T) { + mux := Routes(newTestConfig()) + + rec := get(t, mux, http.MethodGet, "/css/style.css") + if rec.Code != http.StatusOK { + t.Fatalf("got status %d, want 200", rec.Code) + } + + if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/css") { + t.Errorf("got Content-Type %q, want text/css", ct) + } + if rec.Body.Len() == 0 { + t.Error("stylesheet served empty") + } +} + +func TestHomeLinksServedStylesheet(t *testing.T) { + mux := Routes(newTestConfig()) + + body := get(t, mux, http.MethodGet, "/").Body.String() + if !strings.Contains(body, `href="/css/style.css"`) { + t.Fatalf("home does not link the stylesheet, got:\n%s", body) + } + + if code := get(t, mux, http.MethodGet, "/css/style.css").Code; code != http.StatusOK { + t.Errorf("linked stylesheet returned %d, want 200", code) + } +} + func TestHomeRendersInstanceName(t *testing.T) { cfg := newTestConfig() cfg.Instance.Name = "Prodavnica123" |
