proxy: add a test for configHandler

This commit is contained in:
Xiang Li 2015-09-18 13:43:54 -07:00
parent e079f87410
commit ac29432aab

View File

@ -15,9 +15,12 @@
package proxy
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
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)
}
}