mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 04:10:16 +08:00
a798e0c951
- Server types no longer need to store their own contexts; they are stored on the caddy.Instance, which means each context will be properly GC'ed when the instance is stopped. Server types should use type assertions to convert from caddy.Context to their concrete context type when they need to use it. - Pass the entire context into httpserver.GetConfig instead of only the Key field. - caddy.NewTestController now requires a server type string so it can create a controller with the proper concrete context associated with that server type. Tests still need more attention so that we can test the proper creation of startup functions, etc.
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package browse
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mholt/caddy"
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
)
|
|
|
|
func TestSetup(t *testing.T) {
|
|
tempDirPath := os.TempDir()
|
|
_, err := os.Stat(tempDirPath)
|
|
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())))
|
|
|
|
tempTemplate, err := ioutil.TempFile(".", "tempTemplate")
|
|
if err != nil {
|
|
t.Fatalf("BeforeTest: Failed to create a temporary file in the working directory! Error was: %v", err)
|
|
}
|
|
defer os.Remove(tempTemplate.Name())
|
|
|
|
tempTemplatePath := filepath.Join(".", tempTemplate.Name())
|
|
|
|
for i, test := range []struct {
|
|
input string
|
|
expectedPathScope []string
|
|
shouldErr bool
|
|
}{
|
|
// test case #0 tests handling of multiple pathscopes
|
|
{"browse " + tempDirPath + "\n browse .", []string{tempDirPath, "."}, false},
|
|
|
|
// test case #1 tests instantiation of Config with default values
|
|
{"browse /", []string{"/"}, false},
|
|
|
|
// test case #2 tests detectaction of custom template
|
|
{"browse . " + tempTemplatePath, []string{"."}, false},
|
|
|
|
// test case #3 tests detection of non-existent template
|
|
{"browse . " + nonExistantDirPath, nil, true},
|
|
|
|
// test case #4 tests detection of duplicate pathscopes
|
|
{"browse " + tempDirPath + "\n browse " + tempDirPath, nil, true},
|
|
} {
|
|
|
|
c := caddy.NewTestController("http", test.input)
|
|
err := setup(c)
|
|
if err != nil && !test.shouldErr {
|
|
t.Errorf("Test case #%d recieved an error of %v", i, err)
|
|
}
|
|
if test.expectedPathScope == nil {
|
|
continue
|
|
}
|
|
mids := httpserver.GetConfig(c).Middleware()
|
|
mid := mids[len(mids)-1]
|
|
recievedConfigs := mid(nil).(Browse).Configs
|
|
for j, config := range recievedConfigs {
|
|
if config.PathScope != test.expectedPathScope[j] {
|
|
t.Errorf("Test case #%d expected a pathscope of %v, but got %v", i, test.expectedPathScope, config.PathScope)
|
|
}
|
|
}
|
|
}
|
|
}
|