peer_test.go 3.6 KB

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