mirror of
https://github.com/golang/go
synced 2024-11-19 11:14:47 -07:00
http: fix Content-Type of file extension.
ServeFile() pass empty string to serveFile(). serveFile() should get file extension via joining root and filename. R=bradfitz, rsc CC=golang-dev https://golang.org/cl/4654089
This commit is contained in:
parent
350504559e
commit
2012290c7e
@ -222,7 +222,8 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
|
||||
|
||||
// ServeFile replies to the request with the contents of the named file or directory.
|
||||
func ServeFile(w ResponseWriter, r *Request, name string) {
|
||||
serveFile(w, r, Dir(name), "", false)
|
||||
dir, file := filepath.Split(name)
|
||||
serveFile(w, r, Dir(dir), file, false)
|
||||
}
|
||||
|
||||
type fileHandler struct {
|
||||
|
@ -175,6 +175,21 @@ func TestServeFileContentType(t *testing.T) {
|
||||
get(ctype)
|
||||
}
|
||||
|
||||
func TestServeFileMimeType(t *testing.T) {
|
||||
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
ServeFile(w, r, "testdata/style.css")
|
||||
}))
|
||||
defer ts.Close()
|
||||
resp, err := Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "text/css"
|
||||
if h := resp.Header.Get("Content-Type"); h != want {
|
||||
t.Errorf("Content-Type mismatch: got %q, want %q", h, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeFileWithContentEncoding(t *testing.T) {
|
||||
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
w.Header().Set("Content-Encoding", "foo")
|
||||
|
1
src/pkg/http/testdata/style.css
vendored
Normal file
1
src/pkg/http/testdata/style.css
vendored
Normal file
@ -0,0 +1 @@
|
||||
body {}
|
Loading…
Reference in New Issue
Block a user