post_handler_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. resp, _ := tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), nil)
  19. body := tests.ReadBodyJSON(resp)
  20. assert.Equal(t, body["action"], "create", "")
  21. node := body["node"].(map[string]interface{})
  22. assert.Equal(t, node["key"], "/foo/bar/2", "")
  23. assert.Equal(t, node["dir"], true, "")
  24. assert.Equal(t, node["modifiedIndex"], 2, "")
  25. // Second POST should add next index to list.
  26. resp, _ = tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), nil)
  27. body = tests.ReadBodyJSON(resp)
  28. node = body["node"].(map[string]interface{})
  29. assert.Equal(t, node["key"], "/foo/bar/3", "")
  30. // POST to a different key should add index to that list.
  31. resp, _ = tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/baz"), nil)
  32. body = tests.ReadBodyJSON(resp)
  33. node = body["node"].(map[string]interface{})
  34. assert.Equal(t, node["key"], "/foo/baz/4", "")
  35. })
  36. }