Skip to content

Commit

Permalink
Remove the eager check in the browse middleware (caddyserver#1144)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pbeckmann authored and mholt committed Sep 28, 2016
1 parent 8620581 commit bb7787d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
9 changes: 0 additions & 9 deletions caddyhttp/browse/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions caddyhttp/browse/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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},
Expand All @@ -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)
}
}

0 comments on commit bb7787d

Please sign in to comment.