client: tweak test fields

This commit is contained in:
Brian Waldon 2015-01-29 09:47:57 -08:00 committed by Yicheng Qin
parent b174732812
commit 8a6b72b08d

View File

@ -481,76 +481,111 @@ func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHe
func TestUnmarshalSuccessfulResponse(t *testing.T) { func TestUnmarshalSuccessfulResponse(t *testing.T) {
tests := []struct { tests := []struct {
indexHeader string hdr string
body string body string
res *Response wantRes *Response
expectError bool wantErr bool
}{ }{
// Neither PrevNode or Node // Neither PrevNode or Node
{ {
"1", hdr: "1",
`{"action":"delete"}`, body: `{"action":"delete"}`,
&Response{Action: "delete", Index: 1}, wantRes: &Response{Action: "delete", Index: 1},
false, wantErr: false,
}, },
// PrevNode // PrevNode
{ {
"15", hdr: "15",
`{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`, body: `{"action":"delete", "prevNode": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
&Response{Action: "delete", Index: 15, PrevNode: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}}, wantRes: &Response{
false, Action: "delete",
Index: 15,
Node: nil,
PrevNode: &Node{
Key: "/foo",
Value: "bar",
ModifiedIndex: 12,
CreatedIndex: 10,
},
},
wantErr: false,
}, },
// Node // Node
{ {
"15", hdr: "15",
`{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`, body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
&Response{Action: "get", Index: 15, Node: &Node{Key: "/foo", Value: "bar", ModifiedIndex: 12, CreatedIndex: 10}}, wantRes: &Response{
false, Action: "get",
Index: 15,
Node: &Node{
Key: "/foo",
Value: "bar",
ModifiedIndex: 12,
CreatedIndex: 10,
},
PrevNode: nil,
},
wantErr: false,
}, },
// PrevNode and Node // PrevNode and Node
{ {
"15", hdr: "15",
`{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`, body: `{"action":"update", "prevNode": {"key": "/foo", "value": "baz", "modifiedIndex": 10, "createdIndex": 10}, "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
&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}}, wantRes: &Response{
false, 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,
}, },
// Garbage in body // Garbage in body
{ {
"", hdr: "",
`garbage`, body: `garbage`,
nil, wantRes: nil,
true, wantErr: true,
}, },
} }
for i, tt := range tests { for i, tt := range tests {
h := make(http.Header) h := make(http.Header)
h.Add("X-Etcd-Index", tt.indexHeader) h.Add("X-Etcd-Index", tt.hdr)
res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body)) res, err := unmarshalSuccessfulKeysResponse(h, []byte(tt.body))
if tt.expectError != (err != nil) { if tt.wantErr != (err != nil) {
t.Errorf("#%d: expectError=%t, err=%v", i, tt.expectError, err) t.Errorf("#%d: wantErr=%t, err=%v", i, tt.wantErr, err)
} }
if (res == nil) != (tt.res == nil) { if (res == nil) != (tt.wantRes == nil) {
t.Errorf("#%d: received res==%v, but expected res==%v", i, res, tt.res) t.Errorf("#%d: received res=%#v, but expected res=%#v", i, res, tt.wantRes)
continue continue
} else if tt.res == nil { } else if tt.wantRes == nil {
// expected and successfully got nil response // expected and successfully got nil response
continue continue
} }
if res.Action != tt.res.Action { if res.Action != tt.wantRes.Action {
t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.res.Action) t.Errorf("#%d: Action=%s, expected %s", i, res.Action, tt.wantRes.Action)
} }
if res.Index != tt.res.Index { if res.Index != tt.wantRes.Index {
t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.res.Index) t.Errorf("#%d: Index=%d, expected %d", i, res.Index, tt.wantRes.Index)
} }
if !reflect.DeepEqual(res.Node, tt.res.Node) { if !reflect.DeepEqual(res.Node, tt.wantRes.Node) {
t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.res.Node) t.Errorf("#%d: Node=%v, expected %v", i, res.Node, tt.wantRes.Node)
} }
} }
} }