Commits

Miki Tebeka committed 2df7d8b

More tests

  • Participants
  • Parent commits 09559d4
  • Branches multi

Comments (0)

Files changed (2)

 			* `curl http://localhost:6777/remove?backend=localhost:4444`
 	   Or
 		`curl http://localhost:6777/set?backends=localhost:4445`
-		
+
 	   New traffic will be directed to new server(s).
 
 Original forward code by Roger Peppe (see http://bit.ly/Oc1YtF)
 	"time"
 )
 
-func TestHttpOld(t *testing.T) {
-	backends = []string{"hello"}
-	port := 6777
+var port int = 6777
+
+func init() {
+	backends = []string{"localhost:8888"}
 	go startHttpServer(port)
+	time.Sleep(1 * time.Second)
+}
 
-	time.Sleep(1 * time.Second)
-
-	resp, err := http.Get(fmt.Sprintf("http://localhost:%d/current", port))
+func callAPI(suffix string) (string, error) {
+	url := fmt.Sprintf("http://localhost:%d/%s", port, suffix)
+	resp, err := http.Get(url)
 	if err != nil {
-		t.Fatalf("error connecting to /current: %v\n", err)
+		return "", fmt.Errorf("error connecting to /current: %v\n", err)
 	}
 	defer resp.Body.Close()
 
 	reply, err := ioutil.ReadAll(resp.Body)
 	if err != nil {
-		t.Fatalf("error reading reply: %v\n", err)
+		return "", fmt.Errorf("error reading reply: %v\n", err)
 	}
 
-	if string(reply) != fmt.Sprintf("%s\n", backends[0]) {
+	return string(reply), nil
+}
+
+func getTest(suffix string, t *testing.T) {
+	reply, err := callAPI(suffix)
+	if err != nil {
+		t.Fatalf("%s", err)
+	}
+
+	if reply != fmt.Sprintf("%s\n", backends[0]) {
 		t.Fatalf("bad reply: %s\n", string(reply))
 	}
 }
+
+func TestHttpOldAPI(t *testing.T) {
+	getTest("current", t)
+}
+
+func TestHTTPGet(t *testing.T) {
+	getTest("get", t)
+}
+
+func TestAdd(t *testing.T) {
+	backends = []string{"localhost:8888"}
+	backend := "localhost:8887"
+
+	reply, err := callAPI(fmt.Sprintf("add?backend=%s", backend))
+	if err != nil {
+		t.Fatalf("%s", err)
+	}
+
+	if len(backends) != 2 {
+		t.Fatalf("bad number of backends (%d)\nreply: %s", len(backends), reply)
+	}
+
+	if backends[1] != backend {
+		t.Fatalf("bad backend - %s", backends[0])
+	}
+
+	if reply != fmt.Sprintf("%s,%s\n", backends[0], backends[1]) {
+		t.Fatalf("bad reply - %s\n", reply)
+	}
+}