parent
c6109e23af
commit
3c5f23d3ac
6
main.go
6
main.go
|
|
@ -204,7 +204,11 @@ func toRealPath(p string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
|
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":
|
case ".mp4":
|
||||||
w.Header().Set("Content-Type", "video/mp4")
|
w.Header().Set("Content-Type", "video/mp4")
|
||||||
case ".mkv":
|
case ".mkv":
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue