http_test.go 4.4 KB

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