Skip to content

Commit

Permalink
test: add static assets handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
rramiachraf committed May 4, 2024
1 parent 6d68567 commit 5014e10
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import (
type assets struct{}

func (assets) Open(p string) (fs.File, error) {
return os.Open(path.Join("./", p))
return os.Open(path.Join("../", p))
}
35 changes: 35 additions & 0 deletions handlers/static_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package handlers

import (
"mime"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/rramiachraf/dumb/utils"
)

func TestStaticAssets(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, "/static/style.css", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
l := utils.NewLogger(os.Stdout)
m := New(l, &assets{})

m.ServeHTTP(rr, r)

contentType := rr.Header().Get("content-type")
expectedContentType := mime.TypeByExtension(".css")

if contentType != expectedContentType {
t.Fatalf("expected %q, got %q", expectedContentType, contentType)
}

if rr.Code != 200 {
t.Fatalf("expected %d, got %d", 200, rr.Code)
}
}

0 comments on commit 5014e10

Please sign in to comment.