git » gofer » commit 26144fa

config: Add tests

author Alberto Bertogli
2017-08-07 15:17:46 UTC
committer Alberto Bertogli
2017-08-07 15:17:46 UTC
parent 51b94482f01805a559661107e03bd85de9a34adf

config: Add tests

config/config.go +8 -0
config/config_test.go +79 -0

diff --git a/config/config.go b/config/config.go
index a8a2f2f..6fee49e 100644
--- a/config/config.go
+++ b/config/config.go
@@ -42,6 +42,14 @@ func mergeRoutes(src, dst RouteTable) {
 	}
 }
 
+func (c Config) String() string {
+	s, err := c.ToString()
+	if err != nil {
+		return fmt.Sprintf("<error: %v>", err)
+	}
+	return s
+}
+
 func (c Config) ToString() (string, error) {
 	buf := new(bytes.Buffer)
 	if err := toml.NewEncoder(buf).Encode(c); err != nil {
diff --git a/config/config_test.go b/config/config_test.go
new file mode 100644
index 0000000..d414e11
--- /dev/null
+++ b/config/config_test.go
@@ -0,0 +1,79 @@
+package config
+
+import (
+	"log"
+	"reflect"
+	"testing"
+)
+
+func TestSimple(t *testing.T) {
+	const contents = `
+control_addr = "127.0.0.1:9081"
+
+[[http]]
+addr = ":http"
+base_routes = "default"
+
+  [http.routes]
+    "/srv" = "http://srv/"
+
+[[https]]
+addr = ":https"
+certs = "/etc/letsencrypt/live/"
+base_routes = "default"
+
+  [https.routes]
+    "/" = "http://tlsoverrides/"
+    "/srv" = "http://srv2/"
+
+[routes.default]
+"/" = "http://def/"
+"/common" = "http://common/"
+`
+
+	expected := Config{
+		ControlAddr: "127.0.0.1:9081",
+		HTTP: []*HTTP{
+			&HTTP{
+				Addr:       ":http",
+				BaseRoutes: "default",
+				RouteTable: RouteTable{
+					"/":       "http://def/",
+					"/common": "http://common/",
+					"/srv":    "http://srv/",
+				},
+			},
+		},
+		HTTPS: []*HTTPS{
+			&HTTPS{
+				HTTP: HTTP{
+					Addr:       ":https",
+					BaseRoutes: "default",
+					RouteTable: RouteTable{
+						"/":       "http://tlsoverrides/",
+						"/common": "http://common/",
+						"/srv":    "http://srv2/",
+					},
+				},
+				Certs: "/etc/letsencrypt/live/",
+			},
+		},
+		Routes: map[string]RouteTable{
+			"default": RouteTable{
+				"/":       "http://def/",
+				"/common": "http://common/",
+			},
+		},
+	}
+
+	conf, err := LoadString(contents)
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	if !reflect.DeepEqual(*conf, expected) {
+		t.Errorf("configuration is not as expected")
+		t.Errorf("  expected: %v", expected.String())
+		t.Errorf("  got:      %v", conf.String())
+	}
+}