mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Add tests for case insensitivity of keys and saving contexts
This commit is contained in:
		
							parent
							
								
									76d9d695be
								
							
						
					
					
						commit
						9077cce126
					
				@ -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 {
 | 
				
			|||||||
	// 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 (
 | 
				
			|||||||
	"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) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user