diff options
| -rw-r--r-- | internal/server/handlers/handlers.go | 1 | ||||
| -rw-r--r-- | internal/server/handlers/handlers_test.go | 32 | ||||
| -rw-r--r-- | internal/web/css/style.css | 4 | ||||
| -rw-r--r-- | internal/web/web.go | 2 |
4 files changed, 38 insertions, 1 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" diff --git a/internal/web/css/style.css b/internal/web/css/style.css new file mode 100644 index 0000000..c2805e2 --- /dev/null +++ b/internal/web/css/style.css @@ -0,0 +1,4 @@ +body { + font-family: sans-serif; + line-height: 1.6; +} diff --git a/internal/web/web.go b/internal/web/web.go index 391aa2a..5db188e 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -2,5 +2,5 @@ package web import "embed" -//go:embed templates +//go:embed templates css var Files embed.FS |
