mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/http: make ServeContent errors return more specific HTTP status codes
Previously all errors were 404 errors, even if the real error had nothing to do with a file being non-existent. Fixes #10283 Change-Id: I5b08b471a9064c347510cfcf8557373704eef7c0 Reviewed-on: https://go-review.googlesource.com/9200 Reviewed-by: Daniel Morsing <daniel.morsing@gmail.com>
This commit is contained in:
parent
58c1c011a6
commit
5fa2d9915f
2 changed files with 49 additions and 4 deletions
|
|
@ -497,6 +497,7 @@ type fakeFileInfo struct {
|
|||
modtime time.Time
|
||||
ents []*fakeFileInfo
|
||||
contents string
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeFileInfo) Name() string { return f.basename }
|
||||
|
|
@ -549,6 +550,9 @@ func (fs fakeFS) Open(name string) (File, error) {
|
|||
if !ok {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
if f.err != nil {
|
||||
return nil, f.err
|
||||
}
|
||||
return &fakeFile{ReadSeeker: strings.NewReader(f.contents), fi: f, path: name}, nil
|
||||
}
|
||||
|
||||
|
|
@ -803,6 +807,31 @@ func TestServeContent(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestServeContentErrorMessages(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
fs := fakeFS{
|
||||
"/500": &fakeFileInfo{
|
||||
err: errors.New("random error"),
|
||||
},
|
||||
"/403": &fakeFileInfo{
|
||||
err: &os.PathError{Err: os.ErrPermission},
|
||||
},
|
||||
}
|
||||
ts := httptest.NewServer(FileServer(fs))
|
||||
defer ts.Close()
|
||||
for _, code := range []int{403, 404, 500} {
|
||||
res, err := DefaultClient.Get(fmt.Sprintf("%s/%d", ts.URL, code))
|
||||
if err != nil {
|
||||
t.Errorf("Error fetching /%d: %v", code, err)
|
||||
continue
|
||||
}
|
||||
if res.StatusCode != code {
|
||||
t.Errorf("For /%d, status code = %d; want %d", code, res.StatusCode, code)
|
||||
}
|
||||
res.Body.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// verifies that sendfile is being used on Linux
|
||||
func TestLinuxSendfile(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue