post_handler_test.go 1.5 KB

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