client_test.go 13 KB

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