2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-24 04:11:04 +04:00
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-01-23 22:39:45 +03:00
|
|
|
"time"
|
2014-10-24 04:11:04 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestV2KeysURLHelper(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
endpoint url.URL
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix string
|
2014-10-24 04:11:04 +04:00
|
|
|
key string
|
|
|
|
want url.URL
|
|
|
|
}{
|
|
|
|
// key is empty, no problem
|
|
|
|
{
|
|
|
|
endpoint: url.URL{Scheme: "http", Host: "example.com", Path: "/v2/keys"},
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix: "",
|
2014-10-24 04:11:04 +04:00
|
|
|
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: "/v2/keys"},
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix: "",
|
2014-10-24 04:11:04 +04:00
|
|
|
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: ""},
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix: "",
|
2014-10-24 04:11:04 +04:00
|
|
|
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: "/v2/keys"},
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix: "",
|
2014-10-24 04:11:04 +04:00
|
|
|
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: "/v2/keys"},
|
2014-10-31 01:42:34 +03:00
|
|
|
prefix: "",
|
2014-10-24 04:11:04 +04:00
|
|
|
key: "",
|
|
|
|
want: url.URL{Scheme: "https", Host: "example.com", Path: "/v2/keys"},
|
|
|
|
},
|
2014-10-31 01:42:34 +03:00
|
|
|
// Prefix is applied
|
|
|
|
{
|
|
|
|
endpoint: url.URL{Scheme: "https", Host: "example.com", Path: "/foo"},
|
|
|
|
prefix: "/bar",
|
|
|
|
key: "/baz",
|
|
|
|
want: url.URL{Scheme: "https", Host: "example.com", Path: "/foo/bar/baz"},
|
|
|
|
},
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-10-31 01:42:34 +03:00
|
|
|
got := v2KeysURL(tt.endpoint, tt.prefix, tt.key)
|
2014-10-24 04:11:04 +04:00
|
|
|
if tt.want != *got {
|
|
|
|
t.Errorf("#%d: want=%#v, got=%#v", i, tt.want, *got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAction(t *testing.T) {
|
|
|
|
ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
|
2015-02-18 00:15:48 +03:00
|
|
|
baseWantURL := &url.URL{
|
2014-10-24 04:11:04 +04:00
|
|
|
Scheme: "http",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/v2/keys/foo/bar",
|
|
|
|
}
|
|
|
|
wantHeader := http.Header{}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
recursive bool
|
2015-01-28 23:45:05 +03:00
|
|
|
sorted bool
|
2014-10-24 04:11:04 +04:00
|
|
|
wantQuery string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
recursive: false,
|
2015-01-28 23:45:05 +03:00
|
|
|
sorted: false,
|
|
|
|
wantQuery: "recursive=false&sorted=false",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
recursive: true,
|
2015-01-28 23:45:05 +03:00
|
|
|
sorted: false,
|
|
|
|
wantQuery: "recursive=true&sorted=false",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recursive: false,
|
|
|
|
sorted: true,
|
|
|
|
wantQuery: "recursive=false&sorted=true",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
recursive: true,
|
|
|
|
sorted: true,
|
|
|
|
wantQuery: "recursive=true&sorted=true",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := getAction{
|
|
|
|
Key: "/foo/bar",
|
|
|
|
Recursive: tt.recursive,
|
2015-01-28 23:45:05 +03:00
|
|
|
Sorted: tt.sorted,
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
2014-11-01 00:24:14 +03:00
|
|
|
got := *f.HTTPRequest(ep)
|
2014-10-24 04:11:04 +04:00
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
wantURL := baseWantURL
|
2014-10-24 04:11:04 +04:00
|
|
|
wantURL.RawQuery = tt.wantQuery
|
|
|
|
|
2015-01-23 01:30:58 +03:00
|
|
|
err := assertRequest(got, "GET", wantURL, wantHeader, nil)
|
2014-10-24 04:11:04 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitAction(t *testing.T) {
|
|
|
|
ep := url.URL{Scheme: "http", Host: "example.com/v2/keys"}
|
2015-02-18 00:15:48 +03:00
|
|
|
baseWantURL := &url.URL{
|
2014-10-24 04:11:04 +04:00
|
|
|
Scheme: "http",
|
|
|
|
Host: "example.com",
|
|
|
|
Path: "/v2/keys/foo/bar",
|
|
|
|
}
|
|
|
|
wantHeader := http.Header{}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-01-29 01:29:03 +03:00
|
|
|
afterIndex uint64
|
|
|
|
recursive bool
|
|
|
|
wantQuery string
|
2014-10-24 04:11:04 +04:00
|
|
|
}{
|
|
|
|
{
|
2015-01-29 01:29:03 +03:00
|
|
|
recursive: false,
|
|
|
|
afterIndex: uint64(0),
|
|
|
|
wantQuery: "recursive=false&wait=true&waitIndex=0",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
{
|
2015-01-29 01:29:03 +03:00
|
|
|
recursive: false,
|
|
|
|
afterIndex: uint64(12),
|
|
|
|
wantQuery: "recursive=false&wait=true&waitIndex=12",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
{
|
2015-01-29 01:29:03 +03:00
|
|
|
recursive: true,
|
|
|
|
afterIndex: uint64(12),
|
|
|
|
wantQuery: "recursive=true&wait=true&waitIndex=12",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
f := waitAction{
|
2015-01-29 01:29:03 +03:00
|
|
|
Key: "/foo/bar",
|
|
|
|
AfterIndex: tt.afterIndex,
|
|
|
|
Recursive: tt.recursive,
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
2014-11-01 00:24:14 +03:00
|
|
|
got := *f.HTTPRequest(ep)
|
2014-10-24 04:11:04 +04:00
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
wantURL := baseWantURL
|
2014-10-24 04:11:04 +04:00
|
|
|
wantURL.RawQuery = tt.wantQuery
|
|
|
|
|
2015-01-23 01:30:58 +03:00
|
|
|
err := assertRequest(got, "GET", wantURL, wantHeader, nil)
|
2014-10-24 04:11:04 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 23:22:16 +03:00
|
|
|
func TestSetAction(t *testing.T) {
|
2014-10-24 04:11:04 +04:00
|
|
|
wantHeader := http.Header(map[string][]string{
|
|
|
|
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-01-21 23:22:16 +03:00
|
|
|
act setAction
|
|
|
|
wantURL string
|
2014-10-24 04:11:04 +04:00
|
|
|
wantBody string
|
|
|
|
}{
|
2015-01-21 23:22:16 +03:00
|
|
|
// default prefix
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-27 22:25:52 +03:00
|
|
|
Prefix: defaultV2KeysPrefix,
|
2015-01-21 23:22:16 +03:00
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/v2/keys/foo",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// non-default prefix
|
|
|
|
{
|
|
|
|
act: setAction{
|
|
|
|
Prefix: "/pfx",
|
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/pfx/foo",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// no prefix
|
|
|
|
{
|
|
|
|
act: setAction{
|
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with path separators
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-27 22:25:52 +03:00
|
|
|
Prefix: defaultV2KeysPrefix,
|
2015-01-21 23:22:16 +03:00
|
|
|
Key: "foo/bar/baz",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/v2/keys/foo/bar/baz",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with leading slash, Prefix with trailing slash
|
|
|
|
{
|
|
|
|
act: setAction{
|
|
|
|
Prefix: "/foo/",
|
|
|
|
Key: "/bar",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo/bar",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with trailing slash
|
|
|
|
{
|
|
|
|
act: setAction{
|
|
|
|
Key: "/foo/",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Value is set
|
2014-10-24 04:11:04 +04:00
|
|
|
{
|
2015-01-21 23:22:16 +03:00
|
|
|
act: setAction{
|
|
|
|
Key: "foo",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
2014-10-24 04:11:04 +04:00
|
|
|
wantBody: "value=baz",
|
|
|
|
},
|
2015-01-21 23:22:16 +03:00
|
|
|
|
|
|
|
// PrevExist set, but still ignored
|
2014-10-24 04:11:04 +04:00
|
|
|
{
|
2015-01-21 23:22:16 +03:00
|
|
|
act: setAction{
|
2015-01-23 04:04:41 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevExist: PrevIgnore,
|
2015-01-21 23:22:16 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// PrevExist set to true
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-23 04:04:41 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevExist: PrevExist,
|
2015-01-21 23:22:16 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevExist=true",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
|
|
|
|
|
|
|
// PrevExist set to false
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-23 04:04:41 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevExist: PrevNoExist,
|
2015-01-21 23:22:16 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevExist=false",
|
|
|
|
wantBody: "value=",
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
2015-01-23 03:37:53 +03:00
|
|
|
|
|
|
|
// PrevValue is urlencoded
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-23 04:04:41 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevValue: "bar baz",
|
2015-01-23 03:37:53 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevValue=bar+baz",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
2015-01-23 03:42:46 +03:00
|
|
|
|
|
|
|
// PrevIndex is set
|
|
|
|
{
|
|
|
|
act: setAction{
|
2015-01-23 04:04:41 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevIndex: uint64(12),
|
2015-01-23 03:42:46 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevIndex=12",
|
|
|
|
wantBody: "value=",
|
|
|
|
},
|
2015-01-23 22:39:45 +03:00
|
|
|
|
|
|
|
// TTL is set
|
|
|
|
{
|
|
|
|
act: setAction{
|
|
|
|
Key: "foo",
|
|
|
|
TTL: 3 * time.Minute,
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
wantBody: "ttl=180&value=",
|
|
|
|
},
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2015-01-21 23:22:16 +03:00
|
|
|
u, err := url.Parse(tt.wantURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:22:16 +03:00
|
|
|
got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
|
2015-01-23 01:30:58 +03:00
|
|
|
if err := assertRequest(*got, "PUT", u, wantHeader, []byte(tt.wantBody)); err != nil {
|
2014-10-24 04:11:04 +04:00
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 01:35:18 +03:00
|
|
|
func TestDeleteAction(t *testing.T) {
|
|
|
|
wantHeader := http.Header(map[string][]string{
|
|
|
|
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
|
|
|
})
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
act deleteAction
|
|
|
|
wantURL string
|
|
|
|
}{
|
|
|
|
// default prefix
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
2015-01-27 22:25:52 +03:00
|
|
|
Prefix: defaultV2KeysPrefix,
|
2015-01-23 01:35:18 +03:00
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/v2/keys/foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
// non-default prefix
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
|
|
|
Prefix: "/pfx",
|
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/pfx/foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
// no prefix
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
|
|
|
Key: "foo",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with path separators
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
2015-01-27 22:25:52 +03:00
|
|
|
Prefix: defaultV2KeysPrefix,
|
2015-01-23 01:35:18 +03:00
|
|
|
Key: "foo/bar/baz",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/v2/keys/foo/bar/baz",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with leading slash, Prefix with trailing slash
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
|
|
|
Prefix: "/foo/",
|
|
|
|
Key: "/bar",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo/bar",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Key with trailing slash
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
|
|
|
Key: "/foo/",
|
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo",
|
|
|
|
},
|
|
|
|
|
|
|
|
// Recursive set to true
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
2015-01-23 04:05:01 +03:00
|
|
|
Key: "foo",
|
|
|
|
Recursive: true,
|
2015-01-23 01:35:18 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?recursive=true",
|
|
|
|
},
|
2015-01-23 03:37:53 +03:00
|
|
|
|
|
|
|
// PrevValue is urlencoded
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
2015-01-23 04:05:01 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevValue: "bar baz",
|
2015-01-23 03:37:53 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevValue=bar+baz",
|
|
|
|
},
|
2015-01-23 03:42:46 +03:00
|
|
|
|
|
|
|
// PrevIndex is set
|
|
|
|
{
|
|
|
|
act: deleteAction{
|
2015-01-23 04:05:01 +03:00
|
|
|
Key: "foo",
|
|
|
|
PrevIndex: uint64(12),
|
2015-01-23 03:42:46 +03:00
|
|
|
},
|
|
|
|
wantURL: "http://example.com/foo?prevIndex=12",
|
|
|
|
},
|
2015-01-23 01:35:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
u, err := url.Parse(tt.wantURL)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("#%d: unable to use wantURL fixture: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := tt.act.HTTPRequest(url.URL{Scheme: "http", Host: "example.com"})
|
|
|
|
if err := assertRequest(*got, "DELETE", u, wantHeader, nil); err != nil {
|
|
|
|
t.Errorf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 01:30:58 +03:00
|
|
|
func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHeader http.Header, wantBody []byte) error {
|
|
|
|
if wantMethod != got.Method {
|
|
|
|
return fmt.Errorf("want.Method=%#v got.Method=%#v", wantMethod, got.Method)
|
|
|
|
}
|
|
|
|
|
2014-10-24 04:11:04 +04:00
|
|
|
if !reflect.DeepEqual(wantURL, got.URL) {
|
|
|
|
return fmt.Errorf("want.URL=%#v got.URL=%#v", wantURL, got.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wantHeader, got.Header) {
|
|
|
|
return fmt.Errorf("want.Header=%#v got.Header=%#v", wantHeader, got.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.Body == nil {
|
|
|
|
if wantBody != nil {
|
|
|
|
return fmt.Errorf("want.Body=%v got.Body=%v", wantBody, got.Body)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if wantBody == nil {
|
2014-10-30 01:56:19 +03:00
|
|
|
return fmt.Errorf("want.Body=%v got.Body=%s", wantBody, got.Body)
|
2014-10-24 04:11:04 +04:00
|
|
|
} else {
|
|
|
|
gotBytes, err := ioutil.ReadAll(got.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(wantBody, gotBytes) {
|
2014-10-30 01:56:19 +03:00
|
|
|
return fmt.Errorf("want.Body=%s got.Body=%s", wantBody, gotBytes)
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalSuccessfulResponse(t *testing.T) {
|
|
|
|
tests := []struct {
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr string
|
|
|
|
body string
|
|
|
|
wantRes *Response
|
|
|
|
wantErr bool
|
2014-10-24 04:11:04 +04:00
|
|
|
}{
|
|
|
|
// Neither PrevNode or Node
|
|
|
|
{
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr: "1",
|
|
|
|
body: `{"action":"delete"}`,
|
|
|
|
wantRes: &Response{Action: "delete", Index: 1},
|
|
|
|
wantErr: false,
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// PrevNode
|
|
|
|
{
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr: "15",
|
|
|
|
body: `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
|
|
wantRes: &Response{
|
|
|
|
Action: "delete",
|
|
|
|
Index: 15,
|
|
|
|
Node: nil,
|
|
|
|
PrevNode: &Node{
|
|
|
|
Key: "/foo",
|
|
|
|
Value: "bar",
|
|
|
|
ModifiedIndex: 12,
|
|
|
|
CreatedIndex: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Node
|
|
|
|
{
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr: "15",
|
|
|
|
body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
|
|
wantRes: &Response{
|
|
|
|
Action: "get",
|
|
|
|
Index: 15,
|
|
|
|
Node: &Node{
|
|
|
|
Key: "/foo",
|
|
|
|
Value: "bar",
|
|
|
|
ModifiedIndex: 12,
|
|
|
|
CreatedIndex: 10,
|
|
|
|
},
|
|
|
|
PrevNode: nil,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// PrevNode and Node
|
|
|
|
{
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr: "15",
|
|
|
|
body: `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
|
|
|
|
wantRes: &Response{
|
|
|
|
Action: "update",
|
|
|
|
Index: 15,
|
|
|
|
PrevNode: &Node{
|
|
|
|
Key: "/foo",
|
|
|
|
Value: "baz",
|
|
|
|
ModifiedIndex: 10,
|
|
|
|
CreatedIndex: 10,
|
|
|
|
},
|
|
|
|
Node: &Node{
|
|
|
|
Key: "/foo",
|
|
|
|
Value: "bar",
|
|
|
|
ModifiedIndex: 12,
|
|
|
|
CreatedIndex: 10,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// Garbage in body
|
|
|
|
{
|
2015-01-29 20:47:57 +03:00
|
|
|
hdr: "",
|
|
|
|
body: `garbage`,
|
|
|
|
wantRes: nil,
|
|
|
|
wantErr: true,
|
2014-10-24 04:11:04 +04:00
|
|
|
},
|
2015-01-29 20:51:47 +03:00
|
|
|
|
|
|
|
// non-integer index
|
|
|
|
{
|
|
|
|
hdr: "poo",
|
|
|
|
body: `{}`,
|
|
|
|
wantRes: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-12-11 07:02:45 +03:00
|
|
|
h := make(http.Header)
|
2015-01-29 20:47:57 +03:00
|
|
|
h.Add("X-Etcd-Index", tt.hdr)
|
2015-01-29 03:46:58 +03:00
|
|
|
res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
|
2015-01-29 20:47:57 +03:00
|
|
|
if tt.wantErr != (err != nil) {
|
|
|
|
t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
|
2015-01-29 20:47:57 +03:00
|
|
|
if (res == nil) != (tt.wantRes == nil) {
|
|
|
|
t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
|
2014-10-24 04:11:04 +04:00
|
|
|
continue
|
2015-01-29 20:47:57 +03:00
|
|
|
} else if tt.wantRes == nil {
|
2014-12-05 01:12:55 +03:00
|
|
|
// expected and successfully got nil response
|
2014-10-24 04:11:04 +04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-29 20:47:57 +03:00
|
|
|
if res.Action != tt.wantRes.Action {
|
|
|
|
t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
2015-01-29 20:47:57 +03:00
|
|
|
if res.Index != tt.wantRes.Index {
|
|
|
|
t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
|
2014-12-11 07:02:45 +03:00
|
|
|
}
|
2015-01-29 20:47:57 +03:00
|
|
|
if !reflect.DeepEqual(res.Node, tt.wantRes.Node) {
|
|
|
|
t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.wantRes.Node)
|
2014-10-24 04:11:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-30 00:33:55 +03:00
|
|
|
|
|
|
|
func TestUnmarshalFailedKeysResponse(t *testing.T) {
|
|
|
|
body := []byte(`{"errorCode":100,"message":"Key not found","cause":"/foo","index":18}`)
|
|
|
|
|
|
|
|
wantErr := Error{
|
|
|
|
Code: 100,
|
|
|
|
Message: "Key not found",
|
|
|
|
Cause: "/foo",
|
|
|
|
Index: uint64(18),
|
|
|
|
}
|
|
|
|
|
|
|
|
gotErr := unmarshalFailedKeysResponse(body)
|
|
|
|
if !reflect.DeepEqual(wantErr, gotErr) {
|
|
|
|
t.Errorf("unexpected error: want=%#v got=%#v", wantErr, gotErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalFailedKeysResponseBadJSON(t *testing.T) {
|
|
|
|
err := unmarshalFailedKeysResponse([]byte(`{"er`))
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("got nil error")
|
|
|
|
} else if _, ok := err.(Error); ok {
|
|
|
|
t.Errorf("error is of incorrect type *Error: %#v", err)
|
|
|
|
}
|
|
|
|
}
|