http_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package etcdhttp
  14. import (
  15. "errors"
  16. "net/http"
  17. "net/http/httptest"
  18. "net/url"
  19. "sort"
  20. "testing"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.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. func mustNewURL(t *testing.T, s string) *url.URL {
  29. u, err := url.Parse(s)
  30. if err != nil {
  31. t.Fatalf("error creating URL from %q: %v", s, err)
  32. }
  33. return u
  34. }
  35. type fakeCluster struct {
  36. id uint64
  37. clientURLs []string
  38. members map[uint64]*etcdserver.Member
  39. }
  40. func (c *fakeCluster) ID() types.ID { return types.ID(c.id) }
  41. func (c *fakeCluster) ClientURLs() []string { return c.clientURLs }
  42. func (c *fakeCluster) Members() []*etcdserver.Member {
  43. var sms etcdserver.SortableMemberSlice
  44. for _, m := range c.members {
  45. sms = append(sms, m)
  46. }
  47. sort.Sort(sms)
  48. return []*etcdserver.Member(sms)
  49. }
  50. func (c *fakeCluster) Member(id types.ID) *etcdserver.Member { return c.members[uint64(id)] }
  51. func (c *fakeCluster) IsIDRemoved(id types.ID) bool { return false }
  52. // errServer implements the etcd.Server interface for testing.
  53. // It returns the given error from any Do/Process/AddMember/RemoveMember calls.
  54. type errServer struct {
  55. err error
  56. }
  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) Start() {}
  64. func (fs *errServer) Stop() {}
  65. func (fs *errServer) AddMember(ctx context.Context, m etcdserver.Member) error {
  66. return fs.err
  67. }
  68. func (fs *errServer) RemoveMember(ctx context.Context, id uint64) error {
  69. return fs.err
  70. }
  71. func TestWriteError(t *testing.T) {
  72. // nil error should not panic
  73. rw := httptest.NewRecorder()
  74. writeError(rw, nil)
  75. h := rw.Header()
  76. if len(h) > 0 {
  77. t.Fatalf("unexpected non-empty headers: %#v", h)
  78. }
  79. b := rw.Body.String()
  80. if len(b) > 0 {
  81. t.Fatalf("unexpected non-empty body: %q", b)
  82. }
  83. tests := []struct {
  84. err error
  85. wcode int
  86. wi string
  87. }{
  88. {
  89. etcdErr.NewError(etcdErr.EcodeKeyNotFound, "/foo/bar", 123),
  90. http.StatusNotFound,
  91. "123",
  92. },
  93. {
  94. etcdErr.NewError(etcdErr.EcodeTestFailed, "/foo/bar", 456),
  95. http.StatusPreconditionFailed,
  96. "456",
  97. },
  98. {
  99. err: errors.New("something went wrong"),
  100. wcode: http.StatusInternalServerError,
  101. },
  102. }
  103. for i, tt := range tests {
  104. rw := httptest.NewRecorder()
  105. writeError(rw, tt.err)
  106. if code := rw.Code; code != tt.wcode {
  107. t.Errorf("#%d: code=%d, want %d", i, code, tt.wcode)
  108. }
  109. if idx := rw.Header().Get("X-Etcd-Index"); idx != tt.wi {
  110. t.Errorf("#%d: X-Etcd-Index=%q, want %q", i, idx, tt.wi)
  111. }
  112. }
  113. }
  114. func TestAllowMethod(t *testing.T) {
  115. tests := []struct {
  116. m string
  117. ms []string
  118. w bool
  119. wh string
  120. }{
  121. // Accepted methods
  122. {
  123. m: "GET",
  124. ms: []string{"GET", "POST", "PUT"},
  125. w: true,
  126. },
  127. {
  128. m: "POST",
  129. ms: []string{"POST"},
  130. w: true,
  131. },
  132. // Made-up methods no good
  133. {
  134. m: "FAKE",
  135. ms: []string{"GET", "POST", "PUT"},
  136. w: false,
  137. wh: "GET,POST,PUT",
  138. },
  139. // Empty methods no good
  140. {
  141. m: "",
  142. ms: []string{"GET", "POST"},
  143. w: false,
  144. wh: "GET,POST",
  145. },
  146. // Empty accepted methods no good
  147. {
  148. m: "GET",
  149. ms: []string{""},
  150. w: false,
  151. wh: "",
  152. },
  153. // No methods accepted
  154. {
  155. m: "GET",
  156. ms: []string{},
  157. w: false,
  158. wh: "",
  159. },
  160. }
  161. for i, tt := range tests {
  162. rw := httptest.NewRecorder()
  163. g := allowMethod(rw, tt.m, tt.ms...)
  164. if g != tt.w {
  165. t.Errorf("#%d: got allowMethod()=%t, want %t", i, g, tt.w)
  166. }
  167. if !tt.w {
  168. if rw.Code != http.StatusMethodNotAllowed {
  169. t.Errorf("#%d: code=%d, want %d", i, rw.Code, http.StatusMethodNotAllowed)
  170. }
  171. gh := rw.Header().Get("Allow")
  172. if gh != tt.wh {
  173. t.Errorf("#%d: Allow header=%q, want %q", i, gh, tt.wh)
  174. }
  175. }
  176. }
  177. }