add a unit test

master v0.8
bel 2020-05-29 18:12:18 -06:00
parent c6109e23af
commit 3c5f23d3ac
2 changed files with 39 additions and 1 deletions

View File

@ -204,7 +204,11 @@ func toRealPath(p string) string {
}
func setContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
switch strings.ToLower(path.Ext(r.URL.Path)) {
ext := strings.ToLower(path.Ext(r.URL.Path))
if i := strings.LastIndex(ext, "."); i != -1 {
ext = ext[i:]
}
switch ext {
case ".mp4":
w.Header().Set("Content-Type", "video/mp4")
case ".mkv":

34
main_test.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func TestSetContentType(t *testing.T) {
t.Parallel()
cases := map[string]struct {
path string
want string
}{
"css with multi .": {
path: "/static/css/main.2145ce41.chunk.css",
want: "text/css",
},
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
r := &http.Request{URL: &url.URL{Path: c.path}}
w := httptest.NewRecorder()
setContentTypeIfMedia(w, r)
if ct := w.Header().Get("Content-Type"); ct != c.want {
t.Errorf("wrong content type: want %q, got %q", c.want, ct)
}
})
}
}