post_handler_test.go 1.2 KB

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