http_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 client
  14. import (
  15. "errors"
  16. "io/ioutil"
  17. "net/http"
  18. "net/url"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "time"
  23. "github.com/coreos/etcd/Godeps/_workspace/src/code.google.com/p/go.net/context"
  24. )
  25. type fakeTransport struct {
  26. respchan chan *http.Response
  27. errchan chan error
  28. startCancel chan struct{}
  29. finishCancel chan struct{}
  30. }
  31. func newFakeTransport() *fakeTransport {
  32. return &fakeTransport{
  33. respchan: make(chan *http.Response, 1),
  34. errchan: make(chan error, 1),
  35. startCancel: make(chan struct{}, 1),
  36. finishCancel: make(chan struct{}, 1),
  37. }
  38. }
  39. func (t *fakeTransport) RoundTrip(*http.Request) (*http.Response, error) {
  40. select {
  41. case resp := <-t.respchan:
  42. return resp, nil
  43. case err := <-t.errchan:
  44. return nil, err
  45. case <-t.startCancel:
  46. // wait on finishCancel to simulate taking some amount of
  47. // time while calling CancelRequest
  48. <-t.finishCancel
  49. return nil, errors.New("cancelled")
  50. }
  51. }
  52. func (t *fakeTransport) CancelRequest(*http.Request) {
  53. t.startCancel <- struct{}{}
  54. }
  55. type fakeAction struct{}
  56. func (a *fakeAction) httpRequest(url.URL) *http.Request {
  57. return &http.Request{}
  58. }
  59. func TestHTTPClientDoSuccess(t *testing.T) {
  60. tr := newFakeTransport()
  61. c := &httpClient{transport: tr}
  62. tr.respchan <- &http.Response{
  63. StatusCode: http.StatusTeapot,
  64. Body: ioutil.NopCloser(strings.NewReader("foo")),
  65. }
  66. code, body, err := c.do(context.Background(), &fakeAction{})
  67. if err != nil {
  68. t.Fatalf("incorrect error value: want=nil got=%v", err)
  69. }
  70. wantCode := http.StatusTeapot
  71. if wantCode != code {
  72. t.Fatalf("invalid response code: want=%d got=%d", wantCode, code)
  73. }
  74. wantBody := []byte("foo")
  75. if !reflect.DeepEqual(wantBody, body) {
  76. t.Fatalf("invalid response body: want=%q got=%q", wantBody, body)
  77. }
  78. }
  79. func TestHTTPClientDoError(t *testing.T) {
  80. tr := newFakeTransport()
  81. c := &httpClient{transport: tr}
  82. tr.errchan <- errors.New("fixture")
  83. _, _, err := c.do(context.Background(), &fakeAction{})
  84. if err == nil {
  85. t.Fatalf("expected non-nil error, got nil")
  86. }
  87. }
  88. func TestHTTPClientDoCancelContext(t *testing.T) {
  89. tr := newFakeTransport()
  90. c := &httpClient{transport: tr}
  91. tr.startCancel <- struct{}{}
  92. tr.finishCancel <- struct{}{}
  93. _, _, err := c.do(context.Background(), &fakeAction{})
  94. if err == nil {
  95. t.Fatalf("expected non-nil error, got nil")
  96. }
  97. }
  98. func TestHTTPClientDoCancelContextWaitForRoundTrip(t *testing.T) {
  99. tr := newFakeTransport()
  100. c := &httpClient{transport: tr}
  101. donechan := make(chan struct{})
  102. ctx, cancel := context.WithCancel(context.Background())
  103. go func() {
  104. c.do(ctx, &fakeAction{})
  105. close(donechan)
  106. }()
  107. // This should call CancelRequest and begin the cancellation process
  108. cancel()
  109. select {
  110. case <-donechan:
  111. t.Fatalf("httpClient.do should not have exited yet")
  112. default:
  113. }
  114. tr.finishCancel <- struct{}{}
  115. select {
  116. case <-donechan:
  117. //expected behavior
  118. return
  119. case <-time.After(time.Second):
  120. t.Fatalf("httpClient.do did not exit within 1s")
  121. }
  122. }