peer_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "io/ioutil"
  17. "net/http"
  18. "net/http/httptest"
  19. "path"
  20. "testing"
  21. "github.com/coreos/etcd/etcdserver"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. "github.com/coreos/etcd/rafthttp"
  24. )
  25. // TestNewPeerHandler tests that NewPeerHandler returns a handler that
  26. // handles raft-prefix requests well.
  27. func TestNewPeerHandlerOnRaftPrefix(t *testing.T) {
  28. h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  29. w.Write([]byte("test data"))
  30. })
  31. ph := NewPeerHandler(&fakeCluster{}, h)
  32. srv := httptest.NewServer(ph)
  33. defer srv.Close()
  34. tests := []string{
  35. rafthttp.RaftPrefix,
  36. rafthttp.RaftPrefix + "/hello",
  37. }
  38. for i, tt := range tests {
  39. resp, err := http.Get(srv.URL + tt)
  40. if err != nil {
  41. t.Fatalf("unexpected http.Get error: %v", err)
  42. }
  43. body, err := ioutil.ReadAll(resp.Body)
  44. if err != nil {
  45. t.Fatalf("unexpected ioutil.ReadAll error: %v", err)
  46. }
  47. if w := "test data"; string(body) != w {
  48. t.Errorf("#%d: body = %s, want %s", i, body, w)
  49. }
  50. }
  51. }
  52. func TestServeMembersFails(t *testing.T) {
  53. tests := []struct {
  54. method string
  55. wcode int
  56. }{
  57. {
  58. "POST",
  59. http.StatusMethodNotAllowed,
  60. },
  61. {
  62. "DELETE",
  63. http.StatusMethodNotAllowed,
  64. },
  65. {
  66. "BAD",
  67. http.StatusMethodNotAllowed,
  68. },
  69. }
  70. for i, tt := range tests {
  71. rw := httptest.NewRecorder()
  72. h := &peerMembersHandler{clusterInfo: nil}
  73. h.ServeHTTP(rw, &http.Request{Method: tt.method})
  74. if rw.Code != tt.wcode {
  75. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  76. }
  77. }
  78. }
  79. func TestServeMembersGet(t *testing.T) {
  80. memb1 := etcdserver.Member{ID: 1, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8080"}}}
  81. memb2 := etcdserver.Member{ID: 2, Attributes: etcdserver.Attributes{ClientURLs: []string{"http://localhost:8081"}}}
  82. cluster := &fakeCluster{
  83. id: 1,
  84. members: map[uint64]*etcdserver.Member{1: &memb1, 2: &memb2},
  85. }
  86. h := &peerMembersHandler{clusterInfo: cluster}
  87. msb, err := json.Marshal([]etcdserver.Member{memb1, memb2})
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. wms := string(msb) + "\n"
  92. tests := []struct {
  93. path string
  94. wcode int
  95. wct string
  96. wbody string
  97. }{
  98. {peerMembersPrefix, http.StatusOK, "application/json", wms},
  99. {path.Join(peerMembersPrefix, "bad"), http.StatusBadRequest, "text/plain; charset=utf-8", "bad path\n"},
  100. }
  101. for i, tt := range tests {
  102. req, err := http.NewRequest("GET", testutil.MustNewURL(t, tt.path).String(), nil)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. rw := httptest.NewRecorder()
  107. h.ServeHTTP(rw, req)
  108. if rw.Code != tt.wcode {
  109. t.Errorf("#%d: code=%d, want %d", i, rw.Code, tt.wcode)
  110. }
  111. if gct := rw.Header().Get("Content-Type"); gct != tt.wct {
  112. t.Errorf("#%d: content-type = %s, want %s", i, gct, tt.wct)
  113. }
  114. if rw.Body.String() != tt.wbody {
  115. t.Errorf("#%d: body = %s, want %s", i, rw.Body.String(), tt.wbody)
  116. }
  117. gcid := rw.Header().Get("X-Etcd-Cluster-ID")
  118. wcid := cluster.ID().String()
  119. if gcid != wcid {
  120. t.Errorf("#%d: cid = %s, want %s", i, gcid, wcid)
  121. }
  122. }
  123. }