peer_test.go 3.5 KB

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