1
0
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:
Yasuhiro Matsumoto 2011-07-13 14:39:33 -07:00 committed by Russ Cox
parent 350504559e
commit 2012290c7e
3 changed files with 18 additions and 1 deletions

View File

@ -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 {

View File

@ -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
View File

@ -0,0 +1 @@
body {}