delete_handler_test.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package v2
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. "github.com/coreos/etcd/server"
  8. "github.com/coreos/etcd/tests"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // Ensures that a key is deleted.
  12. //
  13. // $ curl -X PUT localhost:4001/v2/keys/foo/bar -d value=XXX
  14. // $ curl -X DELETE localhost:4001/v2/keys/foo/bar
  15. //
  16. func TestV2DeleteKey(t *testing.T) {
  17. tests.RunServer(func(s *server.Server) {
  18. v := url.Values{}
  19. v.Set("value", "XXX")
  20. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), v)
  21. tests.ReadBody(resp)
  22. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar"), url.Values{})
  23. assert.Equal(t, resp.StatusCode, http.StatusOK)
  24. body := tests.ReadBody(resp)
  25. assert.Nil(t, err, "")
  26. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo/bar","modifiedIndex":3,"createdIndex":2}}`, "")
  27. })
  28. }
  29. // Ensures that an empty directory is deleted when dir is set.
  30. //
  31. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  32. // $ curl -X DELETE localhost:4001/v2/keys/foo ->fail
  33. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true
  34. //
  35. func TestV2DeleteEmptyDirectory(t *testing.T) {
  36. tests.RunServer(func(s *server.Server) {
  37. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
  38. tests.ReadBody(resp)
  39. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo"), url.Values{})
  40. assert.Equal(t, resp.StatusCode, http.StatusForbidden)
  41. bodyJson := tests.ReadBodyJSON(resp)
  42. assert.Equal(t, bodyJson["errorCode"], 102, "")
  43. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
  44. assert.Equal(t, resp.StatusCode, http.StatusOK)
  45. body := tests.ReadBody(resp)
  46. assert.Nil(t, err, "")
  47. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2}}`, "")
  48. })
  49. }
  50. // Ensures that a not-empty directory is deleted when dir is set.
  51. //
  52. // $ curl -X PUT localhost:4001/v2/keys/foo/bar?dir=true
  53. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true ->fail
  54. // $ curl -X DELETE localhost:4001/v2/keys/foo?dir=true&recursive=true
  55. //
  56. func TestV2DeleteNonEmptyDirectory(t *testing.T) {
  57. tests.RunServer(func(s *server.Server) {
  58. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar?dir=true"), url.Values{})
  59. tests.ReadBody(resp)
  60. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
  61. assert.Equal(t, resp.StatusCode, http.StatusForbidden)
  62. bodyJson := tests.ReadBodyJSON(resp)
  63. assert.Equal(t, bodyJson["errorCode"], 108, "")
  64. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true&recursive=true"), url.Values{})
  65. assert.Equal(t, resp.StatusCode, http.StatusOK)
  66. body := tests.ReadBody(resp)
  67. assert.Nil(t, err, "")
  68. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2}}`, "")
  69. })
  70. }
  71. // Ensures that a directory is deleted when recursive is set.
  72. //
  73. // $ curl -X PUT localhost:4001/v2/keys/foo?dir=true
  74. // $ curl -X DELETE localhost:4001/v2/keys/foo?recursive=true
  75. //
  76. func TestV2DeleteDirectoryRecursiveImpliesDir(t *testing.T) {
  77. tests.RunServer(func(s *server.Server) {
  78. resp, err := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?dir=true"), url.Values{})
  79. tests.ReadBody(resp)
  80. resp, err = tests.DeleteForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo?recursive=true"), url.Values{})
  81. assert.Equal(t, resp.StatusCode, http.StatusOK)
  82. body := tests.ReadBody(resp)
  83. assert.Nil(t, err, "")
  84. assert.Equal(t, string(body), `{"action":"delete","node":{"key":"/foo","dir":true,"modifiedIndex":3,"createdIndex":2}}`, "")
  85. })
  86. }