http_test.go 4.8 KB

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