2
0

httputil_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package httputil
  2. import (
  3. "context"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. func TestBoundedClientHasTotalTimeout(t *testing.T) {
  10. c := BoundedClient()
  11. if c.Timeout != PricingTimeout {
  12. t.Fatalf("expected total timeout %v, got %v", PricingTimeout, c.Timeout)
  13. }
  14. }
  15. func TestBoundedClientIsShared(t *testing.T) {
  16. if BoundedClient() != BoundedClient() {
  17. t.Fatal("expected BoundedClient to return a shared instance")
  18. }
  19. }
  20. func TestStreamingClientHasNoTotalTimeout(t *testing.T) {
  21. c := StreamingClient()
  22. if c.Timeout != 0 {
  23. t.Fatalf("streaming client must not set a total timeout, got %v", c.Timeout)
  24. }
  25. }
  26. func TestStreamingClientHasResponseHeaderTimeout(t *testing.T) {
  27. c := StreamingClient()
  28. tr, ok := c.Transport.(*http.Transport)
  29. if !ok {
  30. t.Fatalf("expected *http.Transport, got %T", c.Transport)
  31. }
  32. if tr.ResponseHeaderTimeout != PricingTimeout {
  33. t.Fatalf("expected response-header timeout %v, got %v", PricingTimeout, tr.ResponseHeaderTimeout)
  34. }
  35. }
  36. func TestStreamingClientIsShared(t *testing.T) {
  37. if StreamingClient() != StreamingClient() {
  38. t.Fatal("expected StreamingClient to return a shared instance")
  39. }
  40. }
  41. type roundTripperFunc func(*http.Request) (*http.Response, error)
  42. func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
  43. return f(r)
  44. }
  45. // A base transport that is not a *http.Transport must not panic and must still
  46. // yield a usable client with the response-header timeout applied.
  47. func TestNewStreamingClientFallsBackWhenNotTransport(t *testing.T) {
  48. // Honor the RoundTripper contract (non-nil response when error is nil), even
  49. // though this base is only used to exercise the fallback and never round-trips.
  50. base := roundTripperFunc(func(*http.Request) (*http.Response, error) {
  51. return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil
  52. })
  53. c := newStreamingClient(base)
  54. tr, ok := c.Transport.(*http.Transport)
  55. if !ok {
  56. t.Fatalf("expected fallback *http.Transport, got %T", c.Transport)
  57. }
  58. if tr.ResponseHeaderTimeout != PricingTimeout {
  59. t.Fatalf("expected response-header timeout %v, got %v", PricingTimeout, tr.ResponseHeaderTimeout)
  60. }
  61. // The fallback transport must still bound TLS handshake time.
  62. if tr.TLSHandshakeTimeout == 0 {
  63. t.Fatal("expected fallback transport to set a TLS handshake timeout")
  64. }
  65. }
  66. func TestStreamingGetReturnsBody(t *testing.T) {
  67. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  68. _, _ = w.Write([]byte("price-data"))
  69. }))
  70. defer srv.Close()
  71. resp, err := StreamingGet(context.Background(), srv.URL)
  72. if err != nil {
  73. t.Fatalf("unexpected error: %v", err)
  74. }
  75. defer resp.Body.Close()
  76. body, err := io.ReadAll(resp.Body)
  77. if err != nil {
  78. t.Fatalf("reading body: %v", err)
  79. }
  80. if string(body) != "price-data" {
  81. t.Fatalf("expected body %q, got %q", "price-data", string(body))
  82. }
  83. }
  84. func TestStreamingGetHonorsCanceledContext(t *testing.T) {
  85. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  86. _, _ = w.Write([]byte("ok"))
  87. }))
  88. defer srv.Close()
  89. ctx, cancel := context.WithCancel(context.Background())
  90. cancel() // cancel before the request runs
  91. if _, err := StreamingGet(ctx, srv.URL); err == nil {
  92. t.Fatal("expected an error from a canceled context, got nil")
  93. }
  94. }
  95. func TestStreamingGetRejectsBadURL(t *testing.T) {
  96. if _, err := StreamingGet(context.Background(), "://not-a-url"); err == nil {
  97. t.Fatal("expected an error building the request, got nil")
  98. }
  99. }
  100. // Cloning must not mutate the shared default transport.
  101. func TestNewStreamingClientClonesWithoutMutatingDefault(t *testing.T) {
  102. def, ok := http.DefaultTransport.(*http.Transport)
  103. if !ok {
  104. t.Skip("default transport is not *http.Transport in this environment")
  105. }
  106. c := newStreamingClient(def)
  107. tr := c.Transport.(*http.Transport)
  108. if tr == def {
  109. t.Fatal("expected a cloned transport, got the shared default")
  110. }
  111. if def.ResponseHeaderTimeout == PricingTimeout {
  112. t.Fatal("mutated the shared default transport's ResponseHeaderTimeout")
  113. }
  114. }