post_handler_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package v2
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/coreos/etcd/server"
  6. "github.com/coreos/etcd/tests"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. // Ensures a unique value is added to the key's children.
  10. //
  11. // $ curl -X POST localhost:4001/v2/keys/foo/bar
  12. // $ curl -X POST localhost:4001/v2/keys/foo/bar
  13. // $ curl -X POST localhost:4001/v2/keys/foo/baz
  14. //
  15. func TestV2CreateUnique(t *testing.T) {
  16. tests.RunServer(func(s *server.Server) {
  17. // POST should add index to list.
  18. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
  19. resp, _ := tests.PostForm(fullURL, nil)
  20. body := tests.ReadBodyJSON(resp)
  21. assert.Equal(t, body["action"], "create", "")
  22. node := body["node"].(map[string]interface{})
  23. assert.Equal(t, node["key"], "/foo/bar/2", "")
  24. assert.Nil(t, node["dir"], "")
  25. assert.Equal(t, node["modifiedIndex"], 2, "")
  26. // Second POST should add next index to list.
  27. resp, _ = tests.PostForm(fullURL, nil)
  28. body = tests.ReadBodyJSON(resp)
  29. node = body["node"].(map[string]interface{})
  30. assert.Equal(t, node["key"], "/foo/bar/3", "")
  31. // POST to a different key should add index to that list.
  32. resp, _ = tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/baz"), nil)
  33. body = tests.ReadBodyJSON(resp)
  34. node = body["node"].(map[string]interface{})
  35. assert.Equal(t, node["key"], "/foo/baz/4", "")
  36. })
  37. }