http_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package etcdhttp
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "path"
  7. "reflect"
  8. "sync"
  9. "testing"
  10. "github.com/coreos/etcd/etcdserver"
  11. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  12. "github.com/coreos/etcd/store"
  13. "github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
  14. )
  15. func boolp(b bool) *bool { return &b }
  16. func mustNewURL(t *testing.T, s string) *url.URL {
  17. u, err := url.Parse(s)
  18. if err != nil {
  19. t.Fatalf("error creating URL from %q: %v", s, err)
  20. }
  21. return u
  22. }
  23. func TestBadParseRequest(t *testing.T) {
  24. tests := []struct {
  25. in *http.Request
  26. }{
  27. {
  28. // parseForm failure
  29. &http.Request{
  30. Body: nil,
  31. Method: "PUT",
  32. },
  33. },
  34. {
  35. // bad key prefix
  36. &http.Request{
  37. URL: mustNewURL(t, "/badprefix/"),
  38. },
  39. },
  40. }
  41. for i, tt := range tests {
  42. got, err := parseRequest(tt.in, 1234)
  43. if err == nil {
  44. t.Errorf("case %d: unexpected nil error!", i)
  45. }
  46. if !reflect.DeepEqual(got, etcdserverpb.Request{}) {
  47. t.Errorf("case %d: unexpected non-empty Request: %#v", i, got)
  48. }
  49. }
  50. }
  51. func TestGoodParseRequest(t *testing.T) {
  52. tests := []struct {
  53. in *http.Request
  54. w etcdserverpb.Request
  55. }{
  56. {
  57. // good prefix, all other values default
  58. &http.Request{
  59. URL: mustNewURL(t, path.Join(keysPrefix, "foo")),
  60. },
  61. etcdserverpb.Request{
  62. Id: 1234,
  63. Path: "/foo",
  64. },
  65. },
  66. {
  67. // value specified
  68. &http.Request{
  69. URL: mustNewURL(t, path.Join(keysPrefix, "foo?value=some_value")),
  70. },
  71. etcdserverpb.Request{
  72. Id: 1234,
  73. Val: "some_value",
  74. Path: "/foo",
  75. },
  76. },
  77. {
  78. // prevIndex specified
  79. &http.Request{
  80. URL: mustNewURL(t, path.Join(keysPrefix, "foo?prevIndex=98765")),
  81. },
  82. etcdserverpb.Request{
  83. Id: 1234,
  84. PrevIndex: 98765,
  85. Path: "/foo",
  86. },
  87. },
  88. {
  89. // recursive specified
  90. &http.Request{
  91. URL: mustNewURL(t, path.Join(keysPrefix, "foo?recursive=true")),
  92. },
  93. etcdserverpb.Request{
  94. Id: 1234,
  95. Recursive: true,
  96. Path: "/foo",
  97. },
  98. },
  99. {
  100. // sorted specified
  101. &http.Request{
  102. URL: mustNewURL(t, path.Join(keysPrefix, "foo?sorted=true")),
  103. },
  104. etcdserverpb.Request{
  105. Id: 1234,
  106. Sorted: true,
  107. Path: "/foo",
  108. },
  109. },
  110. {
  111. // wait specified
  112. &http.Request{
  113. URL: mustNewURL(t, path.Join(keysPrefix, "foo?wait=true")),
  114. },
  115. etcdserverpb.Request{
  116. Id: 1234,
  117. Wait: true,
  118. Path: "/foo",
  119. },
  120. },
  121. {
  122. // prevExists should be non-null if specified
  123. &http.Request{
  124. URL: mustNewURL(t, path.Join(keysPrefix, "foo?prevExists=true")),
  125. },
  126. etcdserverpb.Request{
  127. Id: 1234,
  128. PrevExists: boolp(true),
  129. Path: "/foo",
  130. },
  131. },
  132. {
  133. // prevExists should be non-null if specified
  134. &http.Request{
  135. URL: mustNewURL(t, path.Join(keysPrefix, "foo?prevExists=false")),
  136. },
  137. etcdserverpb.Request{
  138. Id: 1234,
  139. PrevExists: boolp(false),
  140. Path: "/foo",
  141. },
  142. },
  143. }
  144. for i, tt := range tests {
  145. got, err := parseRequest(tt.in, 1234)
  146. if err != nil {
  147. t.Errorf("#%d: err = %v, want %v", i, err, nil)
  148. }
  149. if !reflect.DeepEqual(got, tt.w) {
  150. t.Errorf("#%d: bad request: got %#v, want %#v", i, got, tt.w)
  151. }
  152. }
  153. }
  154. // eventingWatcher immediately returns a simple event of the given action on its channel
  155. type eventingWatcher struct {
  156. action string
  157. }
  158. func (w *eventingWatcher) EventChan() chan *store.Event {
  159. ch := make(chan *store.Event)
  160. go func() {
  161. ch <- &store.Event{
  162. Action: w.action,
  163. Node: &store.NodeExtern{},
  164. }
  165. }()
  166. return ch
  167. }
  168. func (w *eventingWatcher) Remove() {}
  169. func TestEncodeResponse(t *testing.T) {
  170. tests := []struct {
  171. resp etcdserver.Response
  172. idx string
  173. code int
  174. err error
  175. }{
  176. // standard case, standard 200 response
  177. {
  178. etcdserver.Response{
  179. Event: &store.Event{
  180. Action: store.Get,
  181. Node: &store.NodeExtern{},
  182. PrevNode: &store.NodeExtern{},
  183. },
  184. Watcher: nil,
  185. },
  186. "0",
  187. http.StatusOK,
  188. nil,
  189. },
  190. // check new nodes return StatusCreated
  191. {
  192. etcdserver.Response{
  193. Event: &store.Event{
  194. Action: store.Create,
  195. Node: &store.NodeExtern{},
  196. PrevNode: &store.NodeExtern{},
  197. },
  198. Watcher: nil,
  199. },
  200. "0",
  201. http.StatusCreated,
  202. nil,
  203. },
  204. {
  205. etcdserver.Response{
  206. Watcher: &eventingWatcher{store.Create},
  207. },
  208. "0",
  209. http.StatusCreated,
  210. nil,
  211. },
  212. }
  213. for i, tt := range tests {
  214. rw := httptest.NewRecorder()
  215. err := encodeResponse(context.Background(), rw, tt.resp)
  216. if err != tt.err {
  217. t.Errorf("case %d: unexpected err: got %v, want %v", i, err, tt.err)
  218. continue
  219. }
  220. if gct := rw.Header().Get("Content-Type"); gct != "application/json" {
  221. t.Errorf("case %d: bad Content-Type: got %q, want application/json", i, gct)
  222. }
  223. if gei := rw.Header().Get("X-Etcd-Index"); gei != tt.idx {
  224. t.Errorf("case %d: bad X-Etcd-Index header: got %s, want %s", i, gei, tt.idx)
  225. }
  226. if rw.Code != tt.code {
  227. t.Errorf("case %d: bad response code: got %d, want %v", i, rw.Code, tt.code)
  228. }
  229. }
  230. }
  231. type dummyWatcher struct {
  232. echan chan *store.Event
  233. }
  234. func (w *dummyWatcher) EventChan() chan *store.Event {
  235. return w.echan
  236. }
  237. func (w *dummyWatcher) Remove() {}
  238. type dummyResponseWriter struct {
  239. cnchan chan bool
  240. http.ResponseWriter
  241. }
  242. func (rw *dummyResponseWriter) CloseNotify() <-chan bool {
  243. return rw.cnchan
  244. }
  245. func TestWaitForEventChan(t *testing.T) {
  246. ctx := context.Background()
  247. ec := make(chan *store.Event)
  248. dw := &dummyWatcher{
  249. echan: ec,
  250. }
  251. w := httptest.NewRecorder()
  252. var wg sync.WaitGroup
  253. var ev *store.Event
  254. var err error
  255. wg.Add(1)
  256. go func() {
  257. ev, err = waitForEvent(ctx, w, dw)
  258. wg.Done()
  259. }()
  260. ec <- &store.Event{
  261. Action: store.Get,
  262. Node: &store.NodeExtern{
  263. Key: "/foo/bar",
  264. ModifiedIndex: 12345,
  265. },
  266. }
  267. wg.Wait()
  268. want := &store.Event{
  269. Action: store.Get,
  270. Node: &store.NodeExtern{
  271. Key: "/foo/bar",
  272. ModifiedIndex: 12345,
  273. },
  274. }
  275. if !reflect.DeepEqual(ev, want) {
  276. t.Fatalf("bad event: got %#v, want %#v", ev, want)
  277. }
  278. if err != nil {
  279. t.Fatalf("unexpected error: %v", err)
  280. }
  281. }
  282. func TestWaitForEventCloseNotify(t *testing.T) {
  283. ctx := context.Background()
  284. dw := &dummyWatcher{}
  285. cnchan := make(chan bool)
  286. w := &dummyResponseWriter{
  287. cnchan: cnchan,
  288. }
  289. var wg sync.WaitGroup
  290. var ev *store.Event
  291. var err error
  292. wg.Add(1)
  293. go func() {
  294. ev, err = waitForEvent(ctx, w, dw)
  295. wg.Done()
  296. }()
  297. close(cnchan)
  298. wg.Wait()
  299. if ev != nil {
  300. t.Fatalf("non-nil Event returned with CloseNotifier: %v", ev)
  301. }
  302. if err == nil {
  303. t.Fatalf("nil err returned with CloseNotifier!")
  304. }
  305. }
  306. func TestWaitForEventCancelledContext(t *testing.T) {
  307. cctx, cancel := context.WithCancel(context.Background())
  308. dw := &dummyWatcher{}
  309. w := httptest.NewRecorder()
  310. var wg sync.WaitGroup
  311. var ev *store.Event
  312. var err error
  313. wg.Add(1)
  314. go func() {
  315. ev, err = waitForEvent(cctx, w, dw)
  316. wg.Done()
  317. }()
  318. cancel()
  319. wg.Wait()
  320. if ev != nil {
  321. t.Fatalf("non-nil Event returned with cancelled context: %v", ev)
  322. }
  323. if err == nil {
  324. t.Fatalf("nil err returned with cancelled context!")
  325. }
  326. }