Miki Tebeka avatar Miki Tebeka committed 525221a

More tests

Comments (0)

Files changed (1)

 	getTest("get", t)
 }
 
-func TestAdd(t *testing.T) {
+func TestHTTPAdd(t *testing.T) {
 	backends = []string{"localhost:8888"}
 	backend := "localhost:8887"
 
 		t.Fatalf("bad reply - %s\n", reply)
 	}
 }
+
+func TestHTTPRemove(t *testing.T) {
+	backend1, backend2 := "localhost:8888", "localhost:8887"
+	backends = []string{backend1, backend2}
+	reply, err := callAPI(fmt.Sprintf("remove?backend=%s", backend1))
+	if err != nil {
+		t.Fatalf("%s", err)
+	}
+
+	if len(backends) != 1 {
+		t.Fatalf("bad number of backends (%d)\nreply: %s", len(backends), reply)
+	}
+
+	if backends[0] != backend2 {
+		t.Fatalf("bad backend left - %s", backends[0])
+	}
+}
+
+func Test_isValidBackend(t *testing.T) {
+	names := map[bool]string{
+		true:  "valid",
+		false: "invalid",
+	}
+
+	cases := []struct {
+		value string
+		valid bool
+	}{
+		{"localhost:7", true},
+		{"foo.com:8080", true},
+		{"", false},
+		{"foo.com", false},
+		{"localhost", false},
+		{"foo.com:", false},
+	}
+
+	for _, c := range cases {
+		if isValidBackend(c.value) != c.valid {
+			t.Fatalf("`%s` should be %s", c.value, names[c.valid])
+		}
+	}
+}
+
+func Test_nextBackend(t *testing.T) {
+	backend1, backend2 := "localhost:8888", "localhost:8887"
+	backends = []string{backend1, backend2}
+	currentBackend = 0
+
+	for i, expected := range []string{backend1, backend2, backend1} {
+		next, _ := nextBackend()
+		if next != expected {
+			t.Fatalf("backend should be %s at %d (was %s)", expected, i, next)
+		}
+	}
+
+	backends = []string{backend1, backend2}
+	_, err := nextBackend()
+	if err != nil {
+		t.Fatalf("managed to get backend from empty list")
+	}
+}
+
+func arreq(a, b []string) bool {
+	if len(a) != len(b) {
+		return false
+	}
+
+	for i, v := range a {
+		if b[i] != v {
+			return false
+		}
+	}
+
+	return true
+}
+
+func Test_parseBackends(t *testing.T) {
+	cases := []struct {
+		backends string
+		expected []string
+		ok       bool
+	}{
+		{"localhost:8080", []string{"localhost:8080"}, true},
+		{"localhost:8080,localhost:8887", []string{"localhost:8080", "localhost:8887"}, true},
+		{"", []string{}, false},
+		{"foo", []string{}, false},
+		{"localhost:8080,localhost", []string{}, false},
+	}
+
+	for _, c := range cases {
+		value, err := parseBackends(c.backends)
+		ok := err == nil
+
+		if ok != c.ok {
+			t.Fatalf("bad error for %v", c.backends)
+		}
+
+		if !arreq(value, c.expected) {
+			t.Fatalf("go %v for %s (expected %v)", value, c.backends, c.expected)
+		}
+
+
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.