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