get_handler_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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"], 3, "")
  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. // There exists a little gap between etcd ready to serve and
  78. // it actually serves the first request, which means the response
  79. // delay could be a little bigger.
  80. // This test is time sensitive, so it does one request to ensure
  81. // that the server is working.
  82. tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"))
  83. var watchResp *http.Response
  84. c := make(chan bool)
  85. go func() {
  86. watchResp, _ = tests.Get(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"))
  87. c <- true
  88. }()
  89. // Make sure response didn't fire early.
  90. time.Sleep(1 * time.Millisecond)
  91. // Set a value.
  92. v := url.Values{}
  93. v.Set("value", "XXX")
  94. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  95. tests.ReadBody(resp)
  96. // A response should follow from the GET above.
  97. time.Sleep(1 * time.Millisecond)
  98. select {
  99. case <-c:
  100. default:
  101. t.Fatal("cannot get watch result")
  102. }
  103. body := tests.ReadBodyJSON(watchResp)
  104. assert.NotNil(t, body, "")
  105. assert.Equal(t, body["action"], "set", "")
  106. assert.Equal(t, body["key"], "/foo/bar", "")
  107. assert.Equal(t, body["value"], "XXX", "")
  108. assert.Equal(t, body["index"], 3, "")
  109. })
  110. }
  111. // Ensures that a watcher can wait for a value to be set after a given index.
  112. //
  113. // $ curl -X POST localhost:4001/v1/watch/foo/bar -d index=4
  114. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  115. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY
  116. //
  117. func TestV1WatchKeyWithIndex(t *testing.T) {
  118. tests.RunServer(func(s *server.Server) {
  119. var body map[string]interface{}
  120. c := make(chan bool)
  121. go func() {
  122. v := url.Values{}
  123. v.Set("index", "4")
  124. resp, _ := tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"), v)
  125. body = tests.ReadBodyJSON(resp)
  126. c <- true
  127. }()
  128. // Make sure response didn't fire early.
  129. time.Sleep(1 * time.Millisecond)
  130. assert.Nil(t, body, "")
  131. // Set a value (before given index).
  132. v := url.Values{}
  133. v.Set("value", "XXX")
  134. resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  135. tests.ReadBody(resp)
  136. // Make sure response didn't fire early.
  137. time.Sleep(1 * time.Millisecond)
  138. assert.Nil(t, body, "")
  139. // Set a value (before given index).
  140. v.Set("value", "YYY")
  141. resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
  142. tests.ReadBody(resp)
  143. // A response should follow from the GET above.
  144. time.Sleep(1 * time.Millisecond)
  145. select {
  146. case <-c:
  147. default:
  148. t.Fatal("cannot get watch result")
  149. }
  150. assert.NotNil(t, body, "")
  151. assert.Equal(t, body["action"], "set", "")
  152. assert.Equal(t, body["key"], "/foo/bar", "")
  153. assert.Equal(t, body["value"], "YYY", "")
  154. assert.Equal(t, body["index"], 4, "")
  155. })
  156. }
  157. // Ensures that HEAD works.
  158. //
  159. // $ curl -I localhost:4001/v1/keys/foo/bar -> fail
  160. // $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
  161. // $ curl -I localhost:4001/v1/keys/foo/bar
  162. //
  163. func TestV1HeadKey(t *testing.T) {
  164. tests.RunServer(func(s *server.Server) {
  165. v := url.Values{}
  166. v.Set("value", "XXX")
  167. fullURL := fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar")
  168. resp, _ := tests.Get(fullURL)
  169. assert.Equal(t, resp.StatusCode, http.StatusNotFound)
  170. assert.Equal(t, resp.ContentLength, -1)
  171. resp, _ = tests.PutForm(fullURL, v)
  172. tests.ReadBody(resp)
  173. resp, _ = tests.Get(fullURL)
  174. assert.Equal(t, resp.StatusCode, http.StatusOK)
  175. assert.Equal(t, resp.ContentLength, -1)
  176. })
  177. }