2015-06-23 23:00:55 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2015-10-19 20:19:12 -05:00
|
|
|
"reflect"
|
|
|
|
"sync"
|
2015-06-23 23:00:55 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/server"
|
|
|
|
)
|
|
|
|
|
2015-10-20 06:13:00 -05:00
|
|
|
func TestNewDefault(t *testing.T) {
|
|
|
|
config := NewDefault()
|
|
|
|
|
|
|
|
if actual, expected := config.Root, DefaultRoot; actual != expected {
|
|
|
|
t.Errorf("Root was %s but expected %s", actual, expected)
|
|
|
|
}
|
|
|
|
if actual, expected := config.Host, DefaultHost; actual != expected {
|
|
|
|
t.Errorf("Host was %s but expected %s", actual, expected)
|
|
|
|
}
|
|
|
|
if actual, expected := config.Port, DefaultPort; actual != expected {
|
2015-10-20 11:31:21 -05:00
|
|
|
t.Errorf("Port was %s but expected %s", actual, expected)
|
2015-10-20 06:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
func TestResolveAddr(t *testing.T) {
|
2015-06-23 23:00:55 -05:00
|
|
|
// NOTE: If tests fail due to comparing to string "127.0.0.1",
|
|
|
|
// it's possible that system env resolves with IPv6, or ::1.
|
|
|
|
// If that happens, maybe we should use actualAddr.IP.IsLoopback()
|
|
|
|
// for the assertion, rather than a direct string comparison.
|
|
|
|
|
2015-06-23 23:13:29 -05:00
|
|
|
// NOTE: Tests with {Host: "", Port: ""} and {Host: "localhost", Port: ""}
|
2015-06-25 09:54:49 -05:00
|
|
|
// will not behave the same cross-platform, so they have been omitted.
|
2015-06-23 23:13:29 -05:00
|
|
|
|
2015-06-23 23:00:55 -05:00
|
|
|
for i, test := range []struct {
|
|
|
|
config server.Config
|
|
|
|
shouldWarnErr bool
|
|
|
|
shouldFatalErr bool
|
|
|
|
expectedIP string
|
|
|
|
expectedPort int
|
|
|
|
}{
|
2015-08-04 19:29:54 -05:00
|
|
|
{server.Config{Host: "127.0.0.1", Port: "1234"}, false, false, "<nil>", 1234},
|
|
|
|
{server.Config{Host: "localhost", Port: "80"}, false, false, "<nil>", 80},
|
|
|
|
{server.Config{BindHost: "localhost", Port: "1234"}, false, false, "127.0.0.1", 1234},
|
|
|
|
{server.Config{BindHost: "127.0.0.1", Port: "1234"}, false, false, "127.0.0.1", 1234},
|
|
|
|
{server.Config{BindHost: "should-not-resolve", Port: "1234"}, true, false, "0.0.0.0", 1234},
|
|
|
|
{server.Config{BindHost: "localhost", Port: "http"}, false, false, "127.0.0.1", 80},
|
|
|
|
{server.Config{BindHost: "localhost", Port: "https"}, false, false, "127.0.0.1", 443},
|
|
|
|
{server.Config{BindHost: "", Port: "1234"}, false, false, "<nil>", 1234},
|
|
|
|
{server.Config{BindHost: "localhost", Port: "abcd"}, false, true, "", 0},
|
2015-06-23 23:00:55 -05:00
|
|
|
{server.Config{BindHost: "127.0.0.1", Host: "should-not-be-used", Port: "1234"}, false, false, "127.0.0.1", 1234},
|
|
|
|
{server.Config{BindHost: "localhost", Host: "should-not-be-used", Port: "1234"}, false, false, "127.0.0.1", 1234},
|
|
|
|
{server.Config{BindHost: "should-not-resolve", Host: "localhost", Port: "1234"}, true, false, "0.0.0.0", 1234},
|
|
|
|
} {
|
|
|
|
actualAddr, warnErr, fatalErr := resolveAddr(test.config)
|
|
|
|
|
|
|
|
if test.shouldFatalErr && fatalErr == nil {
|
|
|
|
t.Errorf("Test %d: Expected error, but there wasn't any", i)
|
|
|
|
}
|
|
|
|
if !test.shouldFatalErr && fatalErr != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error, but there was one: %v", i, fatalErr)
|
|
|
|
}
|
|
|
|
if fatalErr != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if test.shouldWarnErr && warnErr == nil {
|
|
|
|
t.Errorf("Test %d: Expected warning, but there wasn't any", i)
|
|
|
|
}
|
|
|
|
if !test.shouldWarnErr && warnErr != nil {
|
|
|
|
t.Errorf("Test %d: Expected no warning, but there was one: %v", i, warnErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual, expected := actualAddr.IP.String(), test.expectedIP; actual != expected {
|
|
|
|
t.Errorf("Test %d: IP was %s but expected %s", i, actual, expected)
|
|
|
|
}
|
|
|
|
if actual, expected := actualAddr.Port, test.expectedPort; actual != expected {
|
|
|
|
t.Errorf("Test %d: Port was %d but expected %d", i, actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 20:19:12 -05:00
|
|
|
|
|
|
|
func TestMakeOnces(t *testing.T) {
|
|
|
|
directives := []directive{
|
|
|
|
{"dummy", nil},
|
|
|
|
{"dummy2", nil},
|
|
|
|
}
|
|
|
|
directiveOrder = directives
|
|
|
|
onces := makeOnces()
|
|
|
|
if len(onces) != len(directives) {
|
|
|
|
t.Errorf("onces had len %d , expected %d", len(onces), len(directives))
|
|
|
|
}
|
|
|
|
expected := map[string]*sync.Once{
|
|
|
|
"dummy": new(sync.Once),
|
|
|
|
"dummy2": new(sync.Once),
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(onces, expected) {
|
|
|
|
t.Errorf("onces was %v, expected %v", onces, expected)
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 20:23:18 -05:00
|
|
|
|
|
|
|
func TestMakeStorages(t *testing.T) {
|
|
|
|
directives := []directive{
|
|
|
|
{"dummy", nil},
|
|
|
|
{"dummy2", nil},
|
|
|
|
}
|
|
|
|
directiveOrder = directives
|
|
|
|
storages := makeStorages()
|
|
|
|
if len(storages) != len(directives) {
|
|
|
|
t.Errorf("storages had len %d , expected %d", len(storages), len(directives))
|
|
|
|
}
|
|
|
|
expected := map[string]interface{}{
|
|
|
|
"dummy": nil,
|
|
|
|
"dummy2": nil,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(storages, expected) {
|
|
|
|
t.Errorf("storages was %v, expected %v", storages, expected)
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 20:36:34 -05:00
|
|
|
|
|
|
|
func TestValidDirective(t *testing.T) {
|
|
|
|
directives := []directive{
|
|
|
|
{"dummy", nil},
|
|
|
|
{"dummy2", nil},
|
|
|
|
}
|
|
|
|
directiveOrder = directives
|
|
|
|
for i, test := range []struct {
|
|
|
|
directive string
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
{"dummy", true},
|
|
|
|
{"dummy2", true},
|
|
|
|
{"dummy3", false},
|
|
|
|
} {
|
|
|
|
if actual, expected := validDirective(test.directive), test.valid; actual != expected {
|
|
|
|
t.Errorf("Test %d: valid was %t, expected %t", i, actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|