Browse Source

proxy: add a test for configHandler

Xiang Li 10 years ago
parent
commit
ac29432aab
1 changed files with 45 additions and 0 deletions
  1. 45 0
      proxy/proxy_test.go

+ 45 - 0
proxy/proxy_test.go

@@ -15,9 +15,12 @@
 package proxy
 package proxy
 
 
 import (
 import (
+	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
+	"net/url"
 	"testing"
 	"testing"
+	"time"
 )
 )
 
 
 func TestReadonlyHandler(t *testing.T) {
 func TestReadonlyHandler(t *testing.T) {
@@ -51,3 +54,45 @@ func TestReadonlyHandler(t *testing.T) {
 		}
 		}
 	}
 	}
 }
 }
+
+func TestConfigHandlerGET(t *testing.T) {
+	var err error
+	us := make([]*url.URL, 3)
+	us[0], err = url.Parse("http://example1.com")
+	if err != nil {
+		t.Fatal(err)
+	}
+	us[1], err = url.Parse("http://example2.com")
+	if err != nil {
+		t.Fatal(err)
+	}
+	us[2], err = url.Parse("http://example3.com")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	rp := reverseProxy{
+		director: &director{
+			ep: []*endpoint{
+				newEndpoint(*us[0], 1*time.Second),
+				newEndpoint(*us[1], 1*time.Second),
+				newEndpoint(*us[2], 1*time.Second),
+			},
+		},
+	}
+
+	req, _ := http.NewRequest("GET", "http://example.com//v2/config/local/proxy", nil)
+	rr := httptest.NewRecorder()
+	rp.configHandler(rr, req)
+
+	wbody := "{\"endpoints\":[\"http://example1.com\",\"http://example2.com\",\"http://example3.com\"]}\n"
+
+	body, err := ioutil.ReadAll(rr.Body)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if string(body) != wbody {
+		t.Errorf("body = %s, want %s", string(body), wbody)
+	}
+}