http_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "errors"
  17. "net/http"
  18. "net/http/httptest"
  19. "sort"
  20. "testing"
  21. etcdErr "github.com/coreos/etcd/error"
  22. "github.com/coreos/etcd/etcdserver"
  23. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  24. "github.com/coreos/etcd/etcdserver/membership"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. "github.com/coreos/go-semver/semver"
  28. "golang.org/x/net/context"
  29. )
  30. type fakeCluster struct {
  31. id uint64
  32. clientURLs []string
  33. members map[uint64]*membership.Member
  34. }
  35. func (c *fakeCluster) ID() types.ID { return types.ID(c.id) }
  36. func (c *fakeCluster) ClientURLs() []string { return c.clientURLs }
  37. func (c *fakeCluster) Members() []*membership.Member {
  38. var ms membership.MembersByID
  39. for _, m := range c.members {
  40. ms = append(ms, m)
  41. }
  42. sort.Sort(ms)
  43. return []*membership.Member(ms)
  44. }
  45. func (c *fakeCluster) Member(id types.ID) *membership.Member { return c.members[uint64(id)] }
  46. func (c *fakeCluster) IsIDRemoved(id types.ID) bool { return false }
  47. func (c *fakeCluster) Version() *semver.Version { return nil }
  48. // errServer implements the etcd.Server interface for testing.
  49. // It returns the given error from any Do/Process/AddMember/RemoveMember calls.
  50. type errServer struct {
  51. err error
  52. }
  53. func (fs *errServer) Start() {}
  54. func (fs *errServer) Stop() {}
  55. func (fs *errServer) ID() types.ID { return types.ID(1) }
  56. func (fs *errServer) Leader() types.ID { return types.ID(1) }
  57. func (fs *errServer) Do(ctx context.Context, r etcdserverpb.Request) (etcdserver.Response, error) {
  58. return etcdserver.Response{}, fs.err
  59. }
  60. func (fs *errServer) Process(ctx context.Context, m raftpb.Message) error {
  61. return fs.err
  62. }
  63. func (fs *errServer) AddMember(ctx context.Context, m membership.Member) ([]*membership.Member, error) {
  64. return nil, fs.err
  65. }
  66. func (fs *errServer) RemoveMember(ctx context.Context, id uint64) ([]*membership.Member, error) {
  67. return nil, fs.err
  68. }
  69. func (fs *errServer) UpdateMember(ctx context.Context, m membership.Member) ([]*membership.Member, error) {
  70. return nil, fs.err
  71. }
  72. func (fs *errServer) ClusterVersion() *semver.Version { return nil }
  73. func TestWriteError(t *testing.T) {
  74. // nil error should not panic
  75. rec := httptest.NewRecorder()
  76. r := new(http.Request)
  77. writeError(rec, r, nil)
  78. h := rec.Header()
  79. if len(h) > 0 {
  80. t.Fatalf("unexpected non-empty headers: %#v", h)
  81. }
  82. b := rec.Body.String()
  83. if len(b) > 0 {
  84. t.Fatalf("unexpected non-empty body: %q", b)
  85. }
  86. tests := []struct {
  87. err error
  88. wcode int
  89. wi string
  90. }{
  91. {
  92. etcdErr.NewError(etcdErr.EcodeKeyNotFound, "/foo/bar", 123),
  93. http.StatusNotFound,
  94. "123",
  95. },
  96. {
  97. etcdErr.NewError(etcdErr.EcodeTestFailed, "/foo/bar", 456),
  98. http.StatusPreconditionFailed,
  99. "456",
  100. },
  101. {
  102. err: errors.New("something went wrong"),
  103. wcode: http.StatusInternalServerError,
  104. },
  105. }
  106. for i, tt := range tests {
  107. rw := httptest.NewRecorder()
  108. writeError(rw, r, tt.err)
  109. if code := rw.Code; code != tt.wcode {
  110. t.Errorf("#%d: code=%d, want %d", i, code, tt.wcode)
  111. }
  112. if idx := rw.Header().Get("X-Etcd-Index"); idx != tt.wi {
  113. t.Errorf("#%d: X-Etcd-Index=%q, want %q", i, idx, tt.wi)
  114. }
  115. }
  116. }
  117. func TestAllowMethod(t *testing.T) {
  118. tests := []struct {
  119. m string
  120. ms []string
  121. w bool
  122. wh string
  123. }{
  124. // Accepted methods
  125. {
  126. m: "GET",
  127. ms: []string{"GET", "POST", "PUT"},
  128. w: true,
  129. },
  130. {
  131. m: "POST",
  132. ms: []string{"POST"},
  133. w: true,
  134. },
  135. // Made-up methods no good
  136. {
  137. m: "FAKE",
  138. ms: []string{"GET", "POST", "PUT"},
  139. w: false,
  140. wh: "GET,POST,PUT",
  141. },
  142. // Empty methods no good
  143. {
  144. m: "",
  145. ms: []string{"GET", "POST"},
  146. w: false,
  147. wh: "GET,POST",
  148. },
  149. // Empty accepted methods no good
  150. {
  151. m: "GET",
  152. ms: []string{""},
  153. w: false,
  154. wh: "",
  155. },
  156. // No methods accepted
  157. {
  158. m: "GET",
  159. ms: []string{},
  160. w: false,
  161. wh: "",
  162. },
  163. }
  164. for i, tt := range tests {
  165. rw := httptest.NewRecorder()
  166. g := allowMethod(rw, tt.m, tt.ms...)
  167. if g != tt.w {
  168. t.Errorf("#%d: got allowMethod()=%t, want %t", i, g, tt.w)
  169. }
  170. if !tt.w {
  171. if rw.Code != http.StatusMethodNotAllowed {
  172. t.Errorf("#%d: code=%d, want %d", i, rw.Code, http.StatusMethodNotAllowed)
  173. }
  174. gh := rw.Header().Get("Allow")
  175. if gh != tt.wh {
  176. t.Errorf("#%d: Allow header=%q, want %q", i, gh, tt.wh)
  177. }
  178. }
  179. }
  180. }