http_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2015 CoreOS, Inc.
  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 etcdhttp
  15. import (
  16. "errors"
  17. "net/http"
  18. "net/http/httptest"
  19. "sort"
  20. "testing"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  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/pkg/types"
  26. "github.com/coreos/etcd/raft/raftpb"
  27. )
  28. type fakeCluster struct {
  29. id uint64
  30. clientURLs []string
  31. members map[uint64]*etcdserver.Member
  32. }
  33. func (c *fakeCluster) ID() types.ID { return types.ID(c.id) }
  34. func (c *fakeCluster) ClientURLs() []string { return c.clientURLs }
  35. func (c *fakeCluster) Members() []*etcdserver.Member {
  36. var sms etcdserver.SortableMemberSlice
  37. for _, m := range c.members {
  38. sms = append(sms, m)
  39. }
  40. sort.Sort(sms)
  41. return []*etcdserver.Member(sms)
  42. }
  43. func (c *fakeCluster) Member(id types.ID) *etcdserver.Member { return c.members[uint64(id)] }
  44. func (c *fakeCluster) IsIDRemoved(id types.ID) bool { return false }
  45. // errServer implements the etcd.Server interface for testing.
  46. // It returns the given error from any Do/Process/AddMember/RemoveMember calls.
  47. type errServer struct {
  48. err error
  49. }
  50. func (fs *errServer) Start() {}
  51. func (fs *errServer) Stop() {}
  52. func (fs *errServer) ID() types.ID { return types.ID(1) }
  53. func (fs *errServer) Leader() types.ID { return types.ID(1) }
  54. func (fs *errServer) Do(ctx context.Context, r etcdserverpb.Request) (etcdserver.Response, error) {
  55. return etcdserver.Response{}, fs.err
  56. }
  57. func (fs *errServer) Process(ctx context.Context, m raftpb.Message) error {
  58. return fs.err
  59. }
  60. func (fs *errServer) AddMember(ctx context.Context, m etcdserver.Member) error {
  61. return fs.err
  62. }
  63. func (fs *errServer) RemoveMember(ctx context.Context, id uint64) error {
  64. return fs.err
  65. }
  66. func (fs *errServer) UpdateMember(ctx context.Context, m etcdserver.Member) error {
  67. return fs.err
  68. }
  69. func TestWriteError(t *testing.T) {
  70. // nil error should not panic
  71. rec := httptest.NewRecorder()
  72. writeError(rec, 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, 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. }