get_handler_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/server"
  10. "github.com/coreos/etcd/tests"
  11. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  12. )
  13. // Ensures that a value can be retrieve for a given key.
  14. //
  15. // $ curl localhost:4001/v1/keys/foo/bar -> fail
  16. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  17. // $ curl localhost:4001/v1/keys/foo/bar
  18. //
  19. func TestV1GetKey(t *testing.T) {
  20. tests.RunServer(func(s *server.Server) {
  21. v := url.Values{}
  22. v.Set("value", "XXX")
  23. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  24. resp, _ := tests.Get(fullURL)
  25. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  26. resp, _ = tests.PutForm(fullURL, v)
  27. tests.ReadBody(resp)
  28. resp, _ = tests.Get(fullURL)
  29. assert.Equal(t, resp.StatusCode, http.StatusOK)
  30. body := tests.ReadBodyJSON(resp)
  31. assert.Equal(t, body["action"], "get", "")
  32. assert.Equal(t, body["key"], "/foo/bar", "")
  33. assert.Equal(t, body["value"], "XXX", "")
  34. assert.Equal(t, body["index"], 2, "")
  35. })
  36. }
  37. // Ensures that a directory of values can be retrieved for a given key.
  38. //
  39. // $ curl -X PUT localhost:4001/v1/keys/foo/x -d value=XXX
  40. // $ curl -X PUT localhost:4001/v1/keys/foo/y/z -d value=YYY
  41. // $ curl localhost:4001/v1/keys/foo
  42. //
  43. func TestV1GetKeyDir(t *testing.T) {
  44. tests.RunServer(func(s *server.Server) {
  45. v := url.Values{}
  46. v.Set("value", "XXX")
  47. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/x"), v)
  48. tests.ReadBody(resp)
  49. v.Set("value", "YYY")
  50. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/y/z"), v)
  51. tests.ReadBody(resp)
  52. resp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo"))
  53. assert.Equal(t, resp.StatusCode, http.StatusOK)
  54. body := tests.ReadBody(resp)
  55. nodes := make([]interface{}, 0)
  56. if err := json.Unmarshal(body, &nodes); err != nil {
  57. panic(fmt.Sprintf("HTTP body JSON parse error: %v", err))
  58. }
  59. assert.Equal(t, len(nodes), 2, "")
  60. node0 := nodes[0].(map[string]interface{})
  61. assert.Equal(t, node0["action"], "get", "")
  62. assert.Equal(t, node0["key"], "/foo/x", "")
  63. assert.Equal(t, node0["value"], "XXX", "")
  64. node1 := nodes[1].(map[string]interface{})
  65. assert.Equal(t, node1["action"], "get", "")
  66. assert.Equal(t, node1["key"], "/foo/y", "")
  67. assert.Equal(t, node1["dir"], true, "")
  68. })
  69. }
  70. // Ensures that a watcher can wait for a value to be set and return it to the client.
  71. //
  72. // $ curl localhost:4001/v1/watch/foo/bar
  73. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  74. //
  75. func TestV1WatchKey(t *testing.T) {
  76. tests.RunServer(func(s *server.Server) {
  77. var watchResp *http.Response
  78. c := make(chan bool)
  79. go func() {
  80. watchResp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"))
  81. c <- true
  82. }()
  83. // Make sure response didn't fire early.
  84. time.Sleep(1 * time.Millisecond)
  85. // Set a value.
  86. v := url.Values{}
  87. v.Set("value", "XXX")
  88. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  89. tests.ReadBody(resp)
  90. // A response should follow from the GET above.
  91. time.Sleep(1 * time.Millisecond)
  92. select {
  93. case <-c:
  94. default:
  95. t.Fatal("cannot get watch result")
  96. }
  97. body := tests.ReadBodyJSON(watchResp)
  98. assert.NotNil(t, body, "")
  99. assert.Equal(t, body["action"], "set", "")
  100. assert.Equal(t, body["key"], "/foo/bar", "")
  101. assert.Equal(t, body["value"], "XXX", "")
  102. assert.Equal(t, body["index"], 2, "")
  103. })
  104. }
  105. // Ensures that a watcher can wait for a value to be set after a given index.
  106. //
  107. // $ curl -X POST localhost:4001/v1/watch/foo/bar -d index=4
  108. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  109. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY
  110. //
  111. func TestV1WatchKeyWithIndex(t *testing.T) {
  112. tests.RunServer(func(s *server.Server) {
  113. var body map[string]interface{}
  114. c := make(chan bool)
  115. go func() {
  116. v := url.Values{}
  117. v.Set("index", "3")
  118. resp, _ := tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"), v)
  119. body = tests.ReadBodyJSON(resp)
  120. c <- true
  121. }()
  122. // Make sure response didn't fire early.
  123. time.Sleep(1 * time.Millisecond)
  124. assert.Nil(t, body, "")
  125. // Set a value (before given index).
  126. v := url.Values{}
  127. v.Set("value", "XXX")
  128. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  129. tests.ReadBody(resp)
  130. // Make sure response didn't fire early.
  131. time.Sleep(1 * time.Millisecond)
  132. assert.Nil(t, body, "")
  133. // Set a value (before given index).
  134. v.Set("value", "YYY")
  135. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  136. tests.ReadBody(resp)
  137. // A response should follow from the GET above.
  138. time.Sleep(1 * time.Millisecond)
  139. select {
  140. case <-c:
  141. default:
  142. t.Fatal("cannot get watch result")
  143. }
  144. assert.NotNil(t, body, "")
  145. assert.Equal(t, body["action"], "set", "")
  146. assert.Equal(t, body["key"], "/foo/bar", "")
  147. assert.Equal(t, body["value"], "YYY", "")
  148. assert.Equal(t, body["index"], 3, "")
  149. })
  150. }
  151. // Ensures that HEAD works.
  152. //
  153. // $ curl -I localhost:4001/v1/keys/foo/bar -> fail
  154. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  155. // $ curl -I localhost:4001/v1/keys/foo/bar
  156. //
  157. func TestV1HeadKey(t *testing.T) {
  158. tests.RunServer(func(s *server.Server) {
  159. v := url.Values{}
  160. v.Set("value", "XXX")
  161. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  162. resp, _ := tests.Get(fullURL)
  163. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  164. assert.Equal(t, resp.ContentLength, -1)
  165. resp, _ = tests.PutForm(fullURL, v)
  166. tests.ReadBody(resp)
  167. resp, _ = tests.Get(fullURL)
  168. assert.Equal(t, resp.StatusCode, http.StatusOK)
  169. assert.Equal(t, resp.ContentLength, -1)
  170. })
  171. }