From bb7787d2ee99cd401c345fb47d75ce0449ea23ee Mon Sep 17 00:00:00 2001 From: Peer Beckmann Date: Wed, 28 Sep 2016 20:23:44 +0200 Subject: 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. --- caddyhttp/browse/setup.go | 9 --------- caddyhttp/browse/setup_test.go | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'caddyhttp') diff --git a/caddyhttp/browse/setup.go b/caddyhttp/browse/setup.go index b860d847c..290915421 100644 --- a/caddyhttp/browse/setup.go +++ b/caddyhttp/browse/setup.go @@ -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 diff --git a/caddyhttp/browse/setup_test.go b/caddyhttp/browse/setup_test.go index 0531cc224..534eb16f6 100644 --- a/caddyhttp/browse/setup_test.go +++ b/caddyhttp/browse/setup_test.go @@ -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) + } } -- cgit v1.2.3