diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go index 7a55a180..68aaaba5 100644 --- a/caddyhttp/httpserver/plugin.go +++ b/caddyhttp/httpserver/plugin.go @@ -65,7 +65,7 @@ type httpContext struct { func (h *httpContext) saveConfig(key string, cfg *SiteConfig) { h.siteConfigs = append(h.siteConfigs, cfg) - h.keysToSiteConfigs[strings.ToLower(key)] = cfg + h.keysToSiteConfigs[key] = cfg } // InspectServerBlocks make sure that everything checks out before @@ -178,8 +178,9 @@ func GetConfig(c *caddy.Controller) *SiteConfig { // we should only get here during tests because directive // actions typically skip the server blocks where we make // the configs - ctx.saveConfig(key, &SiteConfig{Root: Root, TLS: new(caddytls.Config)}) - return GetConfig(c) + cfg := &SiteConfig{Root: Root, TLS: new(caddytls.Config)} + ctx.saveConfig(key, cfg) + return cfg } // shortCaddyfileLoader loads a Caddyfile if positional arguments are diff --git a/caddyhttp/httpserver/plugin_test.go b/caddyhttp/httpserver/plugin_test.go index 25179f06..49bb54e6 100644 --- a/caddyhttp/httpserver/plugin_test.go +++ b/caddyhttp/httpserver/plugin_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyfile" ) @@ -138,6 +139,39 @@ func TestInspectServerBlocksWithCustomDefaultPort(t *testing.T) { } } +func TestInspectServerBlocksCaseInsensitiveKey(t *testing.T) { + filename := "Testfile" + ctx := newContext().(*httpContext) + input := strings.NewReader("localhost {\n}\nLOCALHOST {\n}") + sblocks, err := caddyfile.Parse(filename, input, nil) + if err != nil { + t.Fatalf("Expected no error setting up test, got: %v", err) + } + _, err = ctx.InspectServerBlocks(filename, sblocks) + if err == nil { + t.Error("Expected an error because keys on this server type are case-insensitive (so these are duplicated), but didn't get an error") + } +} + +func TestGetConfig(t *testing.T) { + // case insensitivity for key + con := caddy.NewTestController("http", "") + con.Key = "foo" + cfg := GetConfig(con) + con.Key = "FOO" + cfg2 := GetConfig(con) + if cfg != cfg2 { + t.Errorf("Expected same config using same key with different case; got %p and %p", cfg, cfg2) + } + + // make sure different key returns different config + con.Key = "foobar" + cfg3 := GetConfig(con) + if cfg == cfg3 { + t.Errorf("Expected different configs using when key is different; got %p and %p", cfg, cfg3) + } +} + func TestDirectivesList(t *testing.T) { for i, dir1 := range directives { if dir1 == "" { @@ -157,3 +191,21 @@ func TestDirectivesList(t *testing.T) { } } } + +func TestContextSaveConfig(t *testing.T) { + ctx := newContext().(*httpContext) + ctx.saveConfig("foo", new(SiteConfig)) + if _, ok := ctx.keysToSiteConfigs["foo"]; !ok { + t.Error("Expected config to be saved, but it wasn't") + } + if got, want := len(ctx.siteConfigs), 1; got != want { + t.Errorf("Expected len(siteConfigs) == %d, but was %d", want, got) + } + ctx.saveConfig("Foobar", new(SiteConfig)) + if _, ok := ctx.keysToSiteConfigs["foobar"]; ok { + t.Error("Did not expect to get config with case-insensitive key, but did") + } + if got, want := len(ctx.siteConfigs), 2; got != want { + t.Errorf("Expected len(siteConfigs) == %d, but was %d", want, got) + } +}