|
|
@@ -2,6 +2,7 @@ package v2
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
+ "net/http"
|
|
|
"net/url"
|
|
|
"testing"
|
|
|
"time"
|
|
|
@@ -20,6 +21,7 @@ func TestV2SetKey(t *testing.T) {
|
|
|
v := url.Values{}
|
|
|
v.Set("value", "XXX")
|
|
|
resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
body := tests.ReadBody(resp)
|
|
|
assert.Nil(t, err, "")
|
|
|
assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo/bar","value":"XXX","modifiedIndex":2,"createdIndex":2}}`, "")
|
|
|
@@ -33,6 +35,7 @@ func TestV2SetKey(t *testing.T) {
|
|
|
func TestV2SetDirectory(t *testing.T) {
|
|
|
tests.RunServer(func(s *server.Server) {
|
|
|
resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
body := tests.ReadBody(resp)
|
|
|
assert.Nil(t, err, "")
|
|
|
assert.Equal(t, string(body), `{"action":"set","node":{"key":"/foo","dir":true,"modifiedIndex":2,"createdIndex":2}}`, "")
|
|
|
@@ -50,6 +53,7 @@ func TestV2SetKeyWithTTL(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
v.Set("ttl", "20")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
node := body["node"].(map[string]interface{})
|
|
|
assert.Equal(t, node["ttl"], 20, "")
|
|
|
@@ -70,6 +74,7 @@ func TestV2SetKeyWithBadTTL(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
v.Set("ttl", "bad_ttl")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 202, "")
|
|
|
assert.Equal(t, body["message"], "The given TTL in POST form is not a number", "")
|
|
|
@@ -87,6 +92,7 @@ func TestV2CreateKeySuccess(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
v.Set("prevExist", "false")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
node := body["node"].(map[string]interface{})
|
|
|
assert.Equal(t, node["value"], "XXX", "")
|
|
|
@@ -105,8 +111,10 @@ func TestV2CreateKeyFail(t *testing.T) {
|
|
|
v.Set("prevExist", "false")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 105, "")
|
|
|
assert.Equal(t, body["message"], "Key already exists", "")
|
|
|
@@ -126,11 +134,13 @@ func TestV2UpdateKeySuccess(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevExist", "true")
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["action"], "update", "")
|
|
|
})
|
|
|
@@ -146,9 +156,11 @@ func TestV2UpdateKeyFailOnValue(t *testing.T) {
|
|
|
v := url.Values{}
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), v)
|
|
|
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevExist", "true")
|
|
|
resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusNotFound)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 100, "")
|
|
|
assert.Equal(t, body["message"], "Key not found", "")
|
|
|
@@ -167,12 +179,14 @@ func TestV2UpdateKeyFailOnMissingDirectory(t *testing.T) {
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevExist", "true")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusNotFound)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 100, "")
|
|
|
assert.Equal(t, body["message"], "Key not found", "")
|
|
|
assert.Equal(t, body["cause"], "/foo", "")
|
|
|
|
|
|
resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusNotFound)
|
|
|
body = tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 100, "")
|
|
|
assert.Equal(t, body["message"], "Key not found", "")
|
|
|
@@ -191,10 +205,12 @@ func TestV2SetKeyCASOnIndexSuccess(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevIndex", "2")
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["action"], "compareAndSwap", "")
|
|
|
node := body["node"].(map[string]interface{})
|
|
|
@@ -214,10 +230,12 @@ func TestV2SetKeyCASOnIndexFail(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevIndex", "10")
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 101, "")
|
|
|
assert.Equal(t, body["message"], "Compare failed", "")
|
|
|
@@ -236,6 +254,7 @@ func TestV2SetKeyCASWithInvalidIndex(t *testing.T) {
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevIndex", "bad_index")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 203, "")
|
|
|
assert.Equal(t, body["message"], "The given index in POST form is not a number", "")
|
|
|
@@ -254,10 +273,12 @@ func TestV2SetKeyCASOnValueSuccess(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevValue", "XXX")
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusOK)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["action"], "compareAndSwap", "")
|
|
|
node := body["node"].(map[string]interface{})
|
|
|
@@ -277,10 +298,12 @@ func TestV2SetKeyCASOnValueFail(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
|
|
|
resp, _ := tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusCreated)
|
|
|
tests.ReadBody(resp)
|
|
|
v.Set("value", "YYY")
|
|
|
v.Set("prevValue", "AAA")
|
|
|
resp, _ = tests.PutForm(fullURL, v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusPreconditionFailed)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 101, "")
|
|
|
assert.Equal(t, body["message"], "Compare failed", "")
|
|
|
@@ -299,6 +322,7 @@ func TestV2SetKeyCASWithMissingValueFails(t *testing.T) {
|
|
|
v.Set("value", "XXX")
|
|
|
v.Set("prevValue", "")
|
|
|
resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
|
|
|
+ assert.Equal(t, resp.StatusCode, http.StatusBadRequest)
|
|
|
body := tests.ReadBodyJSON(resp)
|
|
|
assert.Equal(t, body["errorCode"], 201, "")
|
|
|
assert.Equal(t, body["message"], "PrevValue is Required in POST form", "")
|