Browse Source

client: copy v2KeysPrefix to httpClient

It's poor form to muck with a global variable. Make a copy when the
httpClient object is instantiated to make httpClient.SetPrefix safe.
Brian Waldon 11 years ago
parent
commit
992e7c76e0
2 changed files with 34 additions and 23 deletions
  1. 20 9
      client/http.go
  2. 14 14
      client/http_test.go

+ 20 - 9
client/http.go

@@ -44,9 +44,10 @@ type transport interface {
 }
 
 type httpClient struct {
-	transport transport
-	endpoint  url.URL
-	timeout   time.Duration
+	transport    transport
+	endpoint     url.URL
+	timeout      time.Duration
+	v2KeysPrefix string
 }
 
 func NewHTTPClient(tr *http.Transport, ep string, timeout time.Duration) (*httpClient, error) {
@@ -56,16 +57,23 @@ func NewHTTPClient(tr *http.Transport, ep string, timeout time.Duration) (*httpC
 	}
 
 	c := &httpClient{
-		transport: tr,
-		endpoint:  *u,
-		timeout:   timeout,
+		transport:    tr,
+		endpoint:     *u,
+		timeout:      timeout,
+		v2KeysPrefix: DefaultV2KeysPrefix,
 	}
 
 	return c, nil
 }
 
 func (c *httpClient) SetPrefix(p string) {
-	DefaultV2KeysPrefix = p
+	c.v2KeysPrefix = p
+}
+
+func (c *httpClient) Endpoint() url.URL {
+	ep := c.endpoint
+	ep.Path = path.Join(ep.Path, c.v2KeysPrefix)
+	return ep
 }
 
 func (c *httpClient) Create(key, val string, ttl time.Duration) (*Response, error) {
@@ -112,7 +120,7 @@ type roundTripResponse struct {
 }
 
 func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
-	req := act.httpRequest(c.endpoint)
+	req := act.httpRequest(c.Endpoint())
 
 	rtchan := make(chan roundTripResponse, 1)
 	go func() {
@@ -192,8 +200,11 @@ func (hw *httpWatcher) Next() (*Response, error) {
 	return resp, nil
 }
 
+// v2KeysURL forms a URL representing the location of a key. The provided
+// endpoint must be the root of the etcd keys API. For example, a valid
+// endpoint probably has the path "/v2/keys".
 func v2KeysURL(ep url.URL, key string) *url.URL {
-	ep.Path = path.Join(ep.Path, DefaultV2KeysPrefix, key)
+	ep.Path = path.Join(ep.Path, key)
 	return &ep
 }
 

+ 14 - 14
client/http_test.go

@@ -38,38 +38,38 @@ func TestV2URLHelper(t *testing.T) {
 	}{
 		// key is empty, no problem
 		{
-			endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
+			endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
 			key:      "",
 			want:     url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
 		},
 
 		// key is joined to path
 		{
-			endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
+			endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
 			key:      "/foo/bar",
 			want:     url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys/foo/bar"},
 		},
 
+		// key is joined to path when path is empty
+		{
+			endpoint: url.URL{Scheme: "http", Host: "example.com", Path: ""},
+			key:      "/foo/bar",
+			want:     url.URL{Scheme: "http", Host: "example.com", Path: "/foo/bar"},
+		},
+
 		// Host field carries through with port
 		{
-			endpoint: url.URL{Scheme: "http", Host: "example.com:8080", Path: ""},
+			endpoint: url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
 			key:      "",
 			want:     url.URL{Scheme: "http", Host: "example.com:8080", Path: "/v2/keys"},
 		},
 
 		// Scheme carries through
 		{
-			endpoint: url.URL{Scheme: "https", Host: "example.com", Path: ""},
+			endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
 			key:      "",
 			want:     url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
 		},
-
-		// Path on endpoint is not ignored
-		{
-			endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/prefix"},
-			key:      "/foo",
-			want:     url.URL{Scheme: "https", Host: "example.com", Path: "/prefix/v2/keys/foo"},
-		},
 	}
 
 	for i, tt := range tests {
@@ -81,7 +81,7 @@ func TestV2URLHelper(t *testing.T) {
 }
 
 func TestGetAction(t *testing.T) {
-	ep := url.URL{Scheme: "http", Host: "example.com"}
+	ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
 	wantURL := &url.URL{
 		Scheme: "http",
 		Host:   "example.com",
@@ -121,7 +121,7 @@ func TestGetAction(t *testing.T) {
 }
 
 func TestWaitAction(t *testing.T) {
-	ep := url.URL{Scheme: "http", Host: "example.com"}
+	ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
 	wantURL := &url.URL{
 		Scheme: "http",
 		Host:   "example.com",
@@ -170,7 +170,7 @@ func TestWaitAction(t *testing.T) {
 }
 
 func TestCreateAction(t *testing.T) {
-	ep := url.URL{Scheme: "http", Host: "example.com"}
+	ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
 	wantURL := &url.URL{
 		Scheme:   "http",
 		Host:     "example.com",