Remove the eager check in the browse middleware (#1144)

* Remove the eager check in the browse middleware, whether the root directory exists.
Caddy will start and throw a 404-error until the directory will be created.

* Add the complimentary test.
 - Tests the startup of the browse middleware if the site root is inexistent and browse is pointing to the site root.

* Some minor stylistic tweaks.
This commit is contained in:
Peer Beckmann 2016-09-28 20:23:44 +02:00 committed by Matt Holt
parent 8620581f95
commit bb7787d2ee
2 changed files with 14 additions and 11 deletions

View File

@ -62,15 +62,6 @@ func browseParse(c *caddy.Controller) ([]Config, error) {
bc.PathScope = "/"
}
bc.Root = http.Dir(cfg.Root)
theRoot, err := bc.Root.Open("/") // catch a missing path early
if err != nil {
return configs, err
}
defer theRoot.Close()
_, err = theRoot.Readdir(-1)
if err != nil {
return configs, err
}
// Second argument would be the template file to use
var tplText string

View File

@ -18,7 +18,7 @@ func TestSetup(t *testing.T) {
if err != nil {
t.Fatalf("BeforeTest: Failed to find an existing directory for testing! Error was: %v", err)
}
nonExistantDirPath := filepath.Join(tempDirPath, strconv.Itoa(int(time.Now().UnixNano())))
nonExistentDirPath := filepath.Join(tempDirPath, strconv.Itoa(int(time.Now().UnixNano())))
tempTemplate, err := ioutil.TempFile(".", "tempTemplate")
if err != nil {
@ -43,7 +43,7 @@ func TestSetup(t *testing.T) {
{"browse . " + tempTemplatePath, []string{"."}, false},
// test case #3 tests detection of non-existent template
{"browse . " + nonExistantDirPath, nil, true},
{"browse . " + nonExistentDirPath, nil, true},
// test case #4 tests detection of duplicate pathscopes
{"browse " + tempDirPath + "\n browse " + tempDirPath, nil, true},
@ -66,4 +66,16 @@ func TestSetup(t *testing.T) {
}
}
}
// test case #6 tests startup with missing root directory in combination with default browse settings
controller := caddy.NewTestController("http", "browse")
cfg := httpserver.GetConfig(controller)
// Make sure non-existent root path doesn't return error
cfg.Root = nonExistentDirPath
err = setup(controller)
if err != nil {
t.Errorf("Test for non-existent browse path received an error, but shouldn't have: %v", err)
}
}