Commit 9077cce1 authored by Matthew Holt's avatar Matthew Holt

Add tests for case insensitivity of keys and saving contexts

parent 76d9d695
...@@ -65,7 +65,7 @@ type httpContext struct { ...@@ -65,7 +65,7 @@ type httpContext struct {
func (h *httpContext) saveConfig(key string, cfg *SiteConfig) { func (h *httpContext) saveConfig(key string, cfg *SiteConfig) {
h.siteConfigs = append(h.siteConfigs, cfg) h.siteConfigs = append(h.siteConfigs, cfg)
h.keysToSiteConfigs[strings.ToLower(key)] = cfg h.keysToSiteConfigs[key] = cfg
} }
// InspectServerBlocks make sure that everything checks out before // InspectServerBlocks make sure that everything checks out before
...@@ -178,8 +178,9 @@ func GetConfig(c *caddy.Controller) *SiteConfig { ...@@ -178,8 +178,9 @@ func GetConfig(c *caddy.Controller) *SiteConfig {
// we should only get here during tests because directive // we should only get here during tests because directive
// actions typically skip the server blocks where we make // actions typically skip the server blocks where we make
// the configs // the configs
ctx.saveConfig(key, &SiteConfig{Root: Root, TLS: new(caddytls.Config)}) cfg := &SiteConfig{Root: Root, TLS: new(caddytls.Config)}
return GetConfig(c) ctx.saveConfig(key, cfg)
return cfg
} }
// shortCaddyfileLoader loads a Caddyfile if positional arguments are // shortCaddyfileLoader loads a Caddyfile if positional arguments are
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile" "github.com/mholt/caddy/caddyfile"
) )
...@@ -138,6 +139,39 @@ func TestInspectServerBlocksWithCustomDefaultPort(t *testing.T) { ...@@ -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) { func TestDirectivesList(t *testing.T) {
for i, dir1 := range directives { for i, dir1 := range directives {
if dir1 == "" { if dir1 == "" {
...@@ -157,3 +191,21 @@ func TestDirectivesList(t *testing.T) { ...@@ -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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment