|
@@ -5,7 +5,10 @@ import (
|
|
|
"net/http/httptest"
|
|
"net/http/httptest"
|
|
|
"reflect"
|
|
"reflect"
|
|
|
"sort"
|
|
"sort"
|
|
|
|
|
+ "strings"
|
|
|
"testing"
|
|
"testing"
|
|
|
|
|
+
|
|
|
|
|
+ "github.com/coreos/etcd/raft/raftpb"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
func TestPeers(t *testing.T) {
|
|
func TestPeers(t *testing.T) {
|
|
@@ -152,7 +155,7 @@ func TestHttpPost(t *testing.T) {
|
|
|
t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
|
t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
|
|
}
|
|
}
|
|
|
if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
|
if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
|
|
- t.Errorf("%#d: Content-Type=%q, want %q", ct, "application/protobuf")
|
|
|
|
|
|
|
+ t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
|
|
|
}
|
|
}
|
|
|
tr = nil
|
|
tr = nil
|
|
|
ts.Close()
|
|
ts.Close()
|
|
@@ -162,3 +165,75 @@ func TestHttpPost(t *testing.T) {
|
|
|
t.Errorf("httpPost with bad URL returned true unexpectedly!")
|
|
t.Errorf("httpPost with bad URL returned true unexpectedly!")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func TestSend(t *testing.T) {
|
|
|
|
|
+ var tr *http.Request
|
|
|
|
|
+ var rc int
|
|
|
|
|
+ h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
+ tr = r
|
|
|
|
|
+ w.WriteHeader(rc)
|
|
|
|
|
+ })
|
|
|
|
|
+ tests := []struct {
|
|
|
|
|
+ m raftpb.Message
|
|
|
|
|
+ code int
|
|
|
|
|
+
|
|
|
|
|
+ ok bool
|
|
|
|
|
+ }{
|
|
|
|
|
+ {
|
|
|
|
|
+ // all good
|
|
|
|
|
+ raftpb.Message{
|
|
|
|
|
+ To: 42,
|
|
|
|
|
+ Type: 4,
|
|
|
|
|
+ },
|
|
|
|
|
+ http.StatusNoContent,
|
|
|
|
|
+ true,
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ // bad response from server should be silently ignored
|
|
|
|
|
+ raftpb.Message{
|
|
|
|
|
+ To: 42,
|
|
|
|
|
+ Type: 2,
|
|
|
|
|
+ },
|
|
|
|
|
+ http.StatusInternalServerError,
|
|
|
|
|
+ true,
|
|
|
|
|
+ },
|
|
|
|
|
+ {
|
|
|
|
|
+ // unknown destination!
|
|
|
|
|
+ raftpb.Message{
|
|
|
|
|
+ To: 3,
|
|
|
|
|
+ Type: 2,
|
|
|
|
|
+ },
|
|
|
|
|
+ 0,
|
|
|
|
|
+ false,
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for i, tt := range tests {
|
|
|
|
|
+ tr = nil
|
|
|
|
|
+ rc = tt.code
|
|
|
|
|
+ ts := httptest.NewServer(h)
|
|
|
|
|
+ ps := Peers{
|
|
|
|
|
+ 42: []string{strings.TrimPrefix(ts.URL, "http://")},
|
|
|
|
|
+ }
|
|
|
|
|
+ send(ps, tt.m)
|
|
|
|
|
+
|
|
|
|
|
+ if !tt.ok {
|
|
|
|
|
+ if tr != nil {
|
|
|
|
|
+ t.Errorf("#%d: got request=%#v, want nil", i, tr)
|
|
|
|
|
+ }
|
|
|
|
|
+ ts.Close()
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if tr.Method != "POST" {
|
|
|
|
|
+ t.Errorf("#%d: Method=%q, want %q", i, tr.Method, "POST")
|
|
|
|
|
+ }
|
|
|
|
|
+ if ct := tr.Header.Get("Content-Type"); ct != "application/protobuf" {
|
|
|
|
|
+ t.Errorf("#%d: Content-Type=%q, want %q", i, ct, "application/protobuf")
|
|
|
|
|
+ }
|
|
|
|
|
+ if tr.URL.String() != "/raft" {
|
|
|
|
|
+ t.Errorf("#%d: URL=%q, want %q", i, tr.URL.String(), "/raft")
|
|
|
|
|
+ }
|
|
|
|
|
+ ts.Close()
|
|
|
|
|
+ }
|
|
|
|
|
+}
|