peer_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdhttp
  14. import (
  15. "encoding/json"
  16. "net/http"
  17. "net/http/httptest"
  18. "path"
  19. "testing"
  20. "github.com/coreos/etcd/etcdserver"
  21. )
  22. func TestServeMembersFails(t *testing.T) {
  23. tests := []struct {
  24. method string
  25. wcode int
  26. }{
  27. {
  28. "POST",
  29. http.StatusMethodNotAllowed,
  30. },
  31. {
  32. "DELETE",
  33. http.StatusMethodNotAllowed,
  34. },
  35. {
  36. "BAD",
  37. http.StatusMethodNotAllowed,
  38. },
  39. }
  40. for i, tt := range tests {
  41. rw := httptest.NewRecorder()
  42. h := &peerMembersHandler{clusterInfo: nil}
  43. h.ServeHTTP(rw, &http.Request{Method: tt.method})
  44. if rw.Code != tt.wcode {
  45. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  46. }
  47. }
  48. }
  49. func TestServeMembersGet(t *testing.T) {
  50. memb1 := etcdserver.Member{ID: 1, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8080"}}}
  51. memb2 := etcdserver.Member{ID: 2, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8081"}}}
  52. cluster := &fakeCluster{
  53. id: 1,
  54. members: map[uint64]*etcdserver.Member{1: &memb1, 2: &memb2},
  55. }
  56. h := &peerMembersHandler{clusterInfo: cluster}
  57. msb, err := json.Marshal([]etcdserver.Member{memb1, memb2})
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. wms := string(msb) + "\n"
  62. tests := []struct {
  63. path string
  64. wcode int
  65. wct string
  66. wbody string
  67. }{
  68. {peerMembersPrefix, http.StatusOK, "application/json", wms},
  69. {path.Join(peerMembersPrefix, "bad"), http.StatusBadRequest, "text/plain; charset=utf-8", "bad path\n"},
  70. }
  71. for i, tt := range tests {
  72. req, err := http.NewRequest("GET", mustNewURL(t, tt.path).String(), nil)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. rw := httptest.NewRecorder()
  77. h.ServeHTTP(rw, req)
  78. if rw.Code != tt.wcode {
  79. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  80. }
  81. if gct := rw.Header().Get("Content-Type"); gct != tt.wct {
  82. t.Errorf("#%d: content-type = %s, want %s", i, gct, tt.wct)
  83. }
  84. if rw.Body.String() != tt.wbody {
  85. t.Errorf("#%d: body = %s, want %s", i, rw.Body.String(), tt.wbody)
  86. }
  87. gcid := rw.Header().Get("X-Etcd-Cluster-ID")
  88. wcid := cluster.ID().String()
  89. if gcid != wcid {
  90. t.Errorf("#%d: cid = %s, want %s", i, gcid, wcid)
  91. }
  92. }
  93. }