diff --git a/main.go b/main.go index 81caf4b..7ddda44 100755 --- a/main.go +++ b/main.go @@ -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": diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..8f1ae58 --- /dev/null +++ b/main_test.go @@ -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) + } + }) + } +}