http_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 rafthttp
  15. import (
  16. "bytes"
  17. "errors"
  18. "io"
  19. "net/http"
  20. "net/http/httptest"
  21. "strings"
  22. "testing"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  24. "github.com/coreos/etcd/pkg/pbutil"
  25. "github.com/coreos/etcd/pkg/types"
  26. "github.com/coreos/etcd/raft"
  27. "github.com/coreos/etcd/raft/raftpb"
  28. )
  29. func TestServeRaftPrefix(t *testing.T) {
  30. testCases := []struct {
  31. method string
  32. body io.Reader
  33. p Raft
  34. clusterID string
  35. wcode int
  36. }{
  37. {
  38. // bad method
  39. "GET",
  40. bytes.NewReader(
  41. pbutil.MustMarshal(&raftpb.Message{}),
  42. ),
  43. &nopProcessor{},
  44. "0",
  45. http.StatusMethodNotAllowed,
  46. },
  47. {
  48. // bad method
  49. "PUT",
  50. bytes.NewReader(
  51. pbutil.MustMarshal(&raftpb.Message{}),
  52. ),
  53. &nopProcessor{},
  54. "0",
  55. http.StatusMethodNotAllowed,
  56. },
  57. {
  58. // bad method
  59. "DELETE",
  60. bytes.NewReader(
  61. pbutil.MustMarshal(&raftpb.Message{}),
  62. ),
  63. &nopProcessor{},
  64. "0",
  65. http.StatusMethodNotAllowed,
  66. },
  67. {
  68. // bad request body
  69. "POST",
  70. &errReader{},
  71. &nopProcessor{},
  72. "0",
  73. http.StatusBadRequest,
  74. },
  75. {
  76. // bad request protobuf
  77. "POST",
  78. strings.NewReader("malformed garbage"),
  79. &nopProcessor{},
  80. "0",
  81. http.StatusBadRequest,
  82. },
  83. {
  84. // good request, wrong cluster ID
  85. "POST",
  86. bytes.NewReader(
  87. pbutil.MustMarshal(&raftpb.Message{}),
  88. ),
  89. &nopProcessor{},
  90. "1",
  91. http.StatusPreconditionFailed,
  92. },
  93. {
  94. // good request, Processor failure
  95. "POST",
  96. bytes.NewReader(
  97. pbutil.MustMarshal(&raftpb.Message{}),
  98. ),
  99. &errProcessor{
  100. err: &resWriterToError{code: http.StatusForbidden},
  101. },
  102. "0",
  103. http.StatusForbidden,
  104. },
  105. {
  106. // good request, Processor failure
  107. "POST",
  108. bytes.NewReader(
  109. pbutil.MustMarshal(&raftpb.Message{}),
  110. ),
  111. &errProcessor{
  112. err: &resWriterToError{code: http.StatusInternalServerError},
  113. },
  114. "0",
  115. http.StatusInternalServerError,
  116. },
  117. {
  118. // good request, Processor failure
  119. "POST",
  120. bytes.NewReader(
  121. pbutil.MustMarshal(&raftpb.Message{}),
  122. ),
  123. &errProcessor{err: errors.New("blah")},
  124. "0",
  125. http.StatusInternalServerError,
  126. },
  127. {
  128. // good request
  129. "POST",
  130. bytes.NewReader(
  131. pbutil.MustMarshal(&raftpb.Message{}),
  132. ),
  133. &nopProcessor{},
  134. "0",
  135. http.StatusNoContent,
  136. },
  137. }
  138. for i, tt := range testCases {
  139. req, err := http.NewRequest(tt.method, "foo", tt.body)
  140. if err != nil {
  141. t.Fatalf("#%d: could not create request: %#v", i, err)
  142. }
  143. req.Header.Set("X-Etcd-Cluster-ID", tt.clusterID)
  144. rw := httptest.NewRecorder()
  145. h := NewHandler(tt.p, types.ID(0))
  146. h.ServeHTTP(rw, req)
  147. if rw.Code != tt.wcode {
  148. t.Errorf("#%d: got code=%d, want %d", i, rw.Code, tt.wcode)
  149. }
  150. }
  151. }
  152. // errReader implements io.Reader to facilitate a broken request.
  153. type errReader struct{}
  154. func (er *errReader) Read(_ []byte) (int, error) { return 0, errors.New("some error") }
  155. type nopProcessor struct{}
  156. func (p *nopProcessor) Process(ctx context.Context, m raftpb.Message) error { return nil }
  157. func (p *nopProcessor) ReportUnreachable(id uint64) {}
  158. func (p *nopProcessor) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
  159. type errProcessor struct {
  160. err error
  161. }
  162. func (p *errProcessor) Process(ctx context.Context, m raftpb.Message) error { return p.err }
  163. func (p *errProcessor) ReportUnreachable(id uint64) {}
  164. func (p *errProcessor) ReportSnapshot(id uint64, status raft.SnapshotStatus) {}
  165. type resWriterToError struct {
  166. code int
  167. }
  168. func (e *resWriterToError) Error() string { return "" }
  169. func (e *resWriterToError) WriteTo(w http.ResponseWriter) { w.WriteHeader(e.code) }