package goxy import ( "io" "io/ioutil" "net/http" "net/http/httptest" "net/rpc" "testing" ) const cannary = "hello from backend" func TestNewServer(t *testing.T) { backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, cannary) })) defer backendServer.Close() server, err := NewServer("data.gob") if err != nil { t.Error(err) } errc := make(chan error, 2) go func() { errc <- server.ListenAndServe() }() go func() { errc <- http.ListenAndServe(":http-alt", nil) }() select { case err := <-errc: t.Error(err) default: } client, err := rpc.DialHTTP("tcp", ":http-alt") if err != nil { t.Error(err) } defer client.Close() e := Entry{ ServerName: "localhost", Upstream: backendServer.URL, } client.Call("GoXY.Add", e, nil) resp, err := http.Get("http://localhost") if err != nil { t.Error(err) } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { t.Error(err) } if string(b) != cannary { t.Error("got", string(b), "expected", cannary) } }