http_test.go 4.5 KB

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