mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/http: handle absolute paths in mapDirOpenError
The current implementation does not account for Dir being initialized with an absolute path on systems that start paths with filepath.Separator. In this scenario, the original error is returned, and not checked for file segments. This change adds a test for this case, and corrects the behavior by ignoring blank path segments in the loop. Refs #18984 Change-Id: I9b79fd0a73a46976c8e2feda0283ef0bb2b62ea1 Reviewed-on: https://go-review.googlesource.com/36804 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
ef30a1c8aa
commit
a610957f2e
2 changed files with 27 additions and 13 deletions
|
|
@ -1176,22 +1176,33 @@ func TestFileServerNotDirError(t *testing.T) {
|
|||
t.Errorf("StatusCode = %v; want 404", res.StatusCode)
|
||||
}
|
||||
|
||||
dir := Dir("testdata")
|
||||
_, err = dir.Open("/index.html/not-a-file")
|
||||
if err == nil {
|
||||
t.Fatal("err == nil; want != nil")
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
|
||||
test := func(name string, dir Dir) {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
_, err = dir.Open("/index.html/not-a-file")
|
||||
if err == nil {
|
||||
t.Fatal("err == nil; want != nil")
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
_, err = dir.Open("/index.html/not-a-dir/not-a-file")
|
||||
if err == nil {
|
||||
t.Fatal("err == nil; want != nil")
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_, err = dir.Open("/index.html/not-a-dir/not-a-file")
|
||||
if err == nil {
|
||||
t.Fatal("err == nil; want != nil")
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("err = %v; os.IsNotExist(err) = %v; want true", err, os.IsNotExist(err))
|
||||
absPath, err := filepath.Abs("testdata")
|
||||
if err != nil {
|
||||
t.Fatal("get abs path:", err)
|
||||
}
|
||||
|
||||
test("RelativePath", Dir("testdata"))
|
||||
test("AbsolutePath", Dir(absPath))
|
||||
}
|
||||
|
||||
func TestFileServerCleanPath(t *testing.T) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue