http_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 client
  15. import (
  16. "errors"
  17. "io/ioutil"
  18. "net/http"
  19. "net/url"
  20. "reflect"
  21. "strings"
  22. "testing"
  23. "time"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. )
  26. type staticHTTPClient struct {
  27. resp http.Response
  28. err error
  29. }
  30. func (s *staticHTTPClient) Do(context.Context, HTTPAction) (*http.Response, []byte, error) {
  31. return &s.resp, nil, s.err
  32. }
  33. type staticHTTPAction struct {
  34. request http.Request
  35. }
  36. type staticHTTPResponse struct {
  37. resp http.Response
  38. err error
  39. }
  40. func (s *staticHTTPAction) HTTPRequest(url.URL) *http.Request {
  41. return &s.request
  42. }
  43. type multiStaticHTTPClient struct {
  44. responses []staticHTTPResponse
  45. cur int
  46. }
  47. func (s *multiStaticHTTPClient) Do(context.Context, HTTPAction) (*http.Response, []byte, error) {
  48. r := s.responses[s.cur]
  49. s.cur++
  50. return &r.resp, nil, r.err
  51. }
  52. type fakeTransport struct {
  53. respchan chan *http.Response
  54. errchan chan error
  55. startCancel chan struct{}
  56. finishCancel chan struct{}
  57. }
  58. func newFakeTransport() *fakeTransport {
  59. return &fakeTransport{
  60. respchan: make(chan *http.Response, 1),
  61. errchan: make(chan error, 1),
  62. startCancel: make(chan struct{}, 1),
  63. finishCancel: make(chan struct{}, 1),
  64. }
  65. }
  66. func (t *fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
  67. select {
  68. case resp := <-t.respchan:
  69. return resp, nil
  70. case err := <-t.errchan:
  71. return nil, err
  72. case <-t.startCancel:
  73. // wait on finishCancel to simulate taking some amount of
  74. // time while calling CancelRequest
  75. <-t.finishCancel
  76. return nil, errors.New("cancelled")
  77. }
  78. }
  79. func (t *fakeTransport) CancelRequest(*http.Request) {
  80. t.startCancel <- struct{}{}
  81. }
  82. type fakeAction struct{}
  83. func (a *fakeAction) HTTPRequest(url.URL) *http.Request {
  84. return &http.Request{}
  85. }
  86. func TestHTTPClientDoSuccess(t *testing.T) {
  87. tr := newFakeTransport()
  88. c := &httpClient{transport: tr}
  89. tr.respchan <- &http.Response{
  90. StatusCode: http.StatusTeapot,
  91. Body: ioutil.NopCloser(strings.NewReader("foo")),
  92. }
  93. resp, body, err := c.Do(context.Background(), &fakeAction{})
  94. if err != nil {
  95. t.Fatalf("incorrect error value: want=nil got=%v", err)
  96. }
  97. wantCode := http.StatusTeapot
  98. if wantCode != resp.StatusCode {
  99. t.Fatalf("invalid response code: want=%d got=%d", wantCode, resp.StatusCode)
  100. }
  101. wantBody := []byte("foo")
  102. if !reflect.DeepEqual(wantBody, body) {
  103. t.Fatalf("invalid response body: want=%q got=%q", wantBody, body)
  104. }
  105. }
  106. func TestHTTPClientDoError(t *testing.T) {
  107. tr := newFakeTransport()
  108. c := &httpClient{transport: tr}
  109. tr.errchan <- errors.New("fixture")
  110. _, _, err := c.Do(context.Background(), &fakeAction{})
  111. if err == nil {
  112. t.Fatalf("expected non-nil error, got nil")
  113. }
  114. }
  115. func TestHTTPClientDoCancelContext(t *testing.T) {
  116. tr := newFakeTransport()
  117. c := &httpClient{transport: tr}
  118. tr.startCancel <- struct{}{}
  119. tr.finishCancel <- struct{}{}
  120. _, _, err := c.Do(context.Background(), &fakeAction{})
  121. if err == nil {
  122. t.Fatalf("expected non-nil error, got nil")
  123. }
  124. }
  125. func TestHTTPClientDoCancelContextWaitForRoundTrip(t *testing.T) {
  126. tr := newFakeTransport()
  127. c := &httpClient{transport: tr}
  128. donechan := make(chan struct{})
  129. ctx, cancel := context.WithCancel(context.Background())
  130. go func() {
  131. c.Do(ctx, &fakeAction{})
  132. close(donechan)
  133. }()
  134. // This should call CancelRequest and begin the cancellation process
  135. cancel()
  136. select {
  137. case <-donechan:
  138. t.Fatalf("httpClient.do should not have exited yet")
  139. default:
  140. }
  141. tr.finishCancel <- struct{}{}
  142. select {
  143. case <-donechan:
  144. //expected behavior
  145. return
  146. case <-time.After(time.Second):
  147. t.Fatalf("httpClient.do did not exit within 1s")
  148. }
  149. }
  150. func TestHTTPClusterClientDo(t *testing.T) {
  151. fakeErr := errors.New("fake!")
  152. tests := []struct {
  153. client *httpClusterClient
  154. wantCode int
  155. wantErr error
  156. }{
  157. // first good response short-circuits Do
  158. {
  159. client: &httpClusterClient{
  160. clients: []HTTPClient{
  161. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
  162. &staticHTTPClient{err: fakeErr},
  163. },
  164. },
  165. wantCode: http.StatusTeapot,
  166. },
  167. // fall through to good endpoint if err is arbitrary
  168. {
  169. client: &httpClusterClient{
  170. clients: []HTTPClient{
  171. &staticHTTPClient{err: fakeErr},
  172. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
  173. },
  174. },
  175. wantCode: http.StatusTeapot,
  176. },
  177. // ErrTimeout short-circuits Do
  178. {
  179. client: &httpClusterClient{
  180. clients: []HTTPClient{
  181. &staticHTTPClient{err: ErrTimeout},
  182. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
  183. },
  184. },
  185. wantErr: ErrTimeout,
  186. },
  187. // ErrCanceled short-circuits Do
  188. {
  189. client: &httpClusterClient{
  190. clients: []HTTPClient{
  191. &staticHTTPClient{err: ErrCanceled},
  192. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
  193. },
  194. },
  195. wantErr: ErrCanceled,
  196. },
  197. // return err if there are no endpoints
  198. {
  199. client: &httpClusterClient{
  200. clients: []HTTPClient{},
  201. },
  202. wantErr: ErrNoEndpoints,
  203. },
  204. // return err if all endpoints return arbitrary errors
  205. {
  206. client: &httpClusterClient{
  207. clients: []HTTPClient{
  208. &staticHTTPClient{err: fakeErr},
  209. &staticHTTPClient{err: fakeErr},
  210. },
  211. },
  212. wantErr: fakeErr,
  213. },
  214. // 500-level errors cause Do to fallthrough to next endpoint
  215. {
  216. client: &httpClusterClient{
  217. clients: []HTTPClient{
  218. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusBadGateway}},
  219. &staticHTTPClient{resp: http.Response{StatusCode: http.StatusTeapot}},
  220. },
  221. },
  222. wantCode: http.StatusTeapot,
  223. },
  224. }
  225. for i, tt := range tests {
  226. resp, _, err := tt.client.Do(context.Background(), nil)
  227. if !reflect.DeepEqual(tt.wantErr, err) {
  228. t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
  229. continue
  230. }
  231. if resp == nil {
  232. if tt.wantCode != 0 {
  233. t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
  234. }
  235. continue
  236. }
  237. if resp.StatusCode != tt.wantCode {
  238. t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
  239. continue
  240. }
  241. }
  242. }
  243. func TestRedirectedHTTPAction(t *testing.T) {
  244. act := &redirectedHTTPAction{
  245. action: &staticHTTPAction{
  246. request: http.Request{
  247. Method: "DELETE",
  248. URL: &url.URL{
  249. Scheme: "https",
  250. Host: "foo.example.com",
  251. Path: "/ping",
  252. },
  253. },
  254. },
  255. location: url.URL{
  256. Scheme: "https",
  257. Host: "bar.example.com",
  258. Path: "/pong",
  259. },
  260. }
  261. want := &http.Request{
  262. Method: "DELETE",
  263. URL: &url.URL{
  264. Scheme: "https",
  265. Host: "bar.example.com",
  266. Path: "/pong",
  267. },
  268. }
  269. got := act.HTTPRequest(url.URL{Scheme: "http", Host: "baz.example.com", Path: "/pang"})
  270. if !reflect.DeepEqual(want, got) {
  271. t.Fatalf("HTTPRequest is %#v, want %#v", want, got)
  272. }
  273. }
  274. func TestRedirectFollowingHTTPClient(t *testing.T) {
  275. tests := []struct {
  276. max int
  277. client HTTPClient
  278. wantCode int
  279. wantErr error
  280. }{
  281. // errors bubbled up
  282. {
  283. max: 2,
  284. client: &multiStaticHTTPClient{
  285. responses: []staticHTTPResponse{
  286. staticHTTPResponse{
  287. err: errors.New("fail!"),
  288. },
  289. },
  290. },
  291. wantErr: errors.New("fail!"),
  292. },
  293. // no need to follow redirect if none given
  294. {
  295. max: 2,
  296. client: &multiStaticHTTPClient{
  297. responses: []staticHTTPResponse{
  298. staticHTTPResponse{
  299. resp: http.Response{
  300. StatusCode: http.StatusTeapot,
  301. },
  302. },
  303. },
  304. },
  305. wantCode: http.StatusTeapot,
  306. },
  307. // redirects if less than max
  308. {
  309. max: 2,
  310. client: &multiStaticHTTPClient{
  311. responses: []staticHTTPResponse{
  312. staticHTTPResponse{
  313. resp: http.Response{
  314. StatusCode: http.StatusTemporaryRedirect,
  315. Header: http.Header{"Location": []string{"http://example.com"}},
  316. },
  317. },
  318. staticHTTPResponse{
  319. resp: http.Response{
  320. StatusCode: http.StatusTeapot,
  321. },
  322. },
  323. },
  324. },
  325. wantCode: http.StatusTeapot,
  326. },
  327. // succeed after reaching max redirects
  328. {
  329. max: 2,
  330. client: &multiStaticHTTPClient{
  331. responses: []staticHTTPResponse{
  332. staticHTTPResponse{
  333. resp: http.Response{
  334. StatusCode: http.StatusTemporaryRedirect,
  335. Header: http.Header{"Location": []string{"http://example.com"}},
  336. },
  337. },
  338. staticHTTPResponse{
  339. resp: http.Response{
  340. StatusCode: http.StatusTemporaryRedirect,
  341. Header: http.Header{"Location": []string{"http://example.com"}},
  342. },
  343. },
  344. staticHTTPResponse{
  345. resp: http.Response{
  346. StatusCode: http.StatusTeapot,
  347. },
  348. },
  349. },
  350. },
  351. wantCode: http.StatusTeapot,
  352. },
  353. // fail at max+1 redirects
  354. {
  355. max: 1,
  356. client: &multiStaticHTTPClient{
  357. responses: []staticHTTPResponse{
  358. staticHTTPResponse{
  359. resp: http.Response{
  360. StatusCode: http.StatusTemporaryRedirect,
  361. Header: http.Header{"Location": []string{"http://example.com"}},
  362. },
  363. },
  364. staticHTTPResponse{
  365. resp: http.Response{
  366. StatusCode: http.StatusTemporaryRedirect,
  367. Header: http.Header{"Location": []string{"http://example.com"}},
  368. },
  369. },
  370. staticHTTPResponse{
  371. resp: http.Response{
  372. StatusCode: http.StatusTeapot,
  373. },
  374. },
  375. },
  376. },
  377. wantErr: ErrTooManyRedirects,
  378. },
  379. // fail if Location header not set
  380. {
  381. max: 1,
  382. client: &multiStaticHTTPClient{
  383. responses: []staticHTTPResponse{
  384. staticHTTPResponse{
  385. resp: http.Response{
  386. StatusCode: http.StatusTemporaryRedirect,
  387. },
  388. },
  389. },
  390. },
  391. wantErr: errors.New("Location header not set"),
  392. },
  393. // fail if Location header is invalid
  394. {
  395. max: 1,
  396. client: &multiStaticHTTPClient{
  397. responses: []staticHTTPResponse{
  398. staticHTTPResponse{
  399. resp: http.Response{
  400. StatusCode: http.StatusTemporaryRedirect,
  401. Header: http.Header{"Location": []string{":"}},
  402. },
  403. },
  404. },
  405. },
  406. wantErr: errors.New("Location header not valid URL: :"),
  407. },
  408. }
  409. for i, tt := range tests {
  410. client := &redirectFollowingHTTPClient{client: tt.client, max: tt.max}
  411. resp, _, err := client.Do(context.Background(), nil)
  412. if !reflect.DeepEqual(tt.wantErr, err) {
  413. t.Errorf("#%d: got err=%v, want=%v", i, err, tt.wantErr)
  414. continue
  415. }
  416. if resp == nil {
  417. if tt.wantCode != 0 {
  418. t.Errorf("#%d: resp is nil, want=%d", i, tt.wantCode)
  419. }
  420. continue
  421. }
  422. if resp.StatusCode != tt.wantCode {
  423. t.Errorf("#%d: resp code=%d, want=%d", i, resp.StatusCode, tt.wantCode)
  424. continue
  425. }
  426. }
  427. }