test: add static assets handler test

This commit is contained in:
rramiachraf 2024-05-04 13:34:19 +01:00
parent 6d6856700b
commit 5014e10c7f
2 changed files with 36 additions and 1 deletions

View File

@ -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
handlers/static_test.go Normal file
View File

@ -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)
}
}