Browse Source

client: use v2MembersURL helper

Brian Waldon 11 years ago
parent
commit
eab4692744
2 changed files with 32 additions and 6 deletions
  1. 14 6
      client/members.go
  2. 18 0
      client/members_test.go

+ 14 - 6
client/members.go

@@ -121,8 +121,8 @@ func (m *httpMembersAPI) Remove(memberID string) error {
 type membersAPIActionList struct{}
 type membersAPIActionList struct{}
 
 
 func (l *membersAPIActionList) httpRequest(ep url.URL) *http.Request {
 func (l *membersAPIActionList) httpRequest(ep url.URL) *http.Request {
-	ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
-	req, _ := http.NewRequest("GET", ep.String(), nil)
+	u := v2MembersURL(ep)
+	req, _ := http.NewRequest("GET", u.String(), nil)
 	return req
 	return req
 }
 }
 
 
@@ -131,8 +131,9 @@ type membersAPIActionRemove struct {
 }
 }
 
 
 func (d *membersAPIActionRemove) httpRequest(ep url.URL) *http.Request {
 func (d *membersAPIActionRemove) httpRequest(ep url.URL) *http.Request {
-	ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix, d.memberID)
-	req, _ := http.NewRequest("DELETE", ep.String(), nil)
+	u := v2MembersURL(ep)
+	u.Path = path.Join(u.Path, d.memberID)
+	req, _ := http.NewRequest("DELETE", u.String(), nil)
 	return req
 	return req
 }
 }
 
 
@@ -141,10 +142,10 @@ type membersAPIActionAdd struct {
 }
 }
 
 
 func (a *membersAPIActionAdd) httpRequest(ep url.URL) *http.Request {
 func (a *membersAPIActionAdd) httpRequest(ep url.URL) *http.Request {
-	ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
+	u := v2MembersURL(ep)
 	m := httptypes.MemberCreateRequest{PeerURLs: a.peerURLs}
 	m := httptypes.MemberCreateRequest{PeerURLs: a.peerURLs}
 	b, _ := json.Marshal(&m)
 	b, _ := json.Marshal(&m)
-	req, _ := http.NewRequest("POST", ep.String(), bytes.NewReader(b))
+	req, _ := http.NewRequest("POST", u.String(), bytes.NewReader(b))
 	req.Header.Set("Content-Type", "application/json")
 	req.Header.Set("Content-Type", "application/json")
 	return req
 	return req
 }
 }
@@ -155,3 +156,10 @@ func assertStatusCode(want, got int) (err error) {
 	}
 	}
 	return err
 	return err
 }
 }
+
+// v2MembersURL add the necessary path to the provided endpoint
+// to route requests to the default v2 members API.
+func v2MembersURL(ep url.URL) *url.URL {
+	ep.Path = path.Join(ep.Path, DefaultV2MembersPrefix)
+	return &ep
+}

+ 18 - 0
client/members_test.go

@@ -19,6 +19,7 @@ package client
 import (
 import (
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
+	"reflect"
 	"testing"
 	"testing"
 
 
 	"github.com/coreos/etcd/pkg/types"
 	"github.com/coreos/etcd/pkg/types"
@@ -93,3 +94,20 @@ func TestAssertStatusCode(t *testing.T) {
 		t.Errorf("assertStatusCode found conflict in 400 vs 400: %v", err)
 		t.Errorf("assertStatusCode found conflict in 400 vs 400: %v", err)
 	}
 	}
 }
 }
+
+func TestV2MembersURL(t *testing.T) {
+	got := v2MembersURL(url.URL{
+		Scheme: "http",
+		Host:   "foo.example.com:4002",
+		Path:   "/pants",
+	})
+	want := &url.URL{
+		Scheme: "http",
+		Host:   "foo.example.com:4002",
+		Path:   "/pants/v2/members",
+	}
+
+	if !reflect.DeepEqual(want, got) {
+		t.Fatalf("v2MembersURL got %#v, want %#v", got, want)
+	}
+}