ctxhttp_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !plan9
  5. package ctxhttp
  6. import (
  7. "context"
  8. "io"
  9. "io/ioutil"
  10. "net/http"
  11. "net/http/httptest"
  12. "testing"
  13. "time"
  14. )
  15. func TestGo17Context(t *testing.T) {
  16. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  17. io.WriteString(w, "ok")
  18. }))
  19. defer ts.Close()
  20. ctx := context.Background()
  21. resp, err := Get(ctx, http.DefaultClient, ts.URL)
  22. if resp == nil || err != nil {
  23. t.Fatalf("error received from client: %v %v", err, resp)
  24. }
  25. resp.Body.Close()
  26. }
  27. const (
  28. requestDuration = 100 * time.Millisecond
  29. requestBody = "ok"
  30. )
  31. func okHandler(w http.ResponseWriter, r *http.Request) {
  32. time.Sleep(requestDuration)
  33. io.WriteString(w, requestBody)
  34. }
  35. func TestNoTimeout(t *testing.T) {
  36. ts := httptest.NewServer(http.HandlerFunc(okHandler))
  37. defer ts.Close()
  38. ctx := context.Background()
  39. res, err := Get(ctx, nil, ts.URL)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer res.Body.Close()
  44. slurp, err := ioutil.ReadAll(res.Body)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if string(slurp) != requestBody {
  49. t.Errorf("body = %q; want %q", slurp, requestBody)
  50. }
  51. }
  52. func TestCancelBeforeHeaders(t *testing.T) {
  53. ctx, cancel := context.WithCancel(context.Background())
  54. blockServer := make(chan struct{})
  55. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  56. cancel()
  57. <-blockServer
  58. io.WriteString(w, requestBody)
  59. }))
  60. defer ts.Close()
  61. defer close(blockServer)
  62. res, err := Get(ctx, nil, ts.URL)
  63. if err == nil {
  64. res.Body.Close()
  65. t.Fatal("Get returned unexpected nil error")
  66. }
  67. if err != context.Canceled {
  68. t.Errorf("err = %v; want %v", err, context.Canceled)
  69. }
  70. }
  71. func TestCancelAfterHangingRequest(t *testing.T) {
  72. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  73. w.WriteHeader(http.StatusOK)
  74. w.(http.Flusher).Flush()
  75. <-w.(http.CloseNotifier).CloseNotify()
  76. }))
  77. defer ts.Close()
  78. ctx, cancel := context.WithCancel(context.Background())
  79. resp, err := Get(ctx, nil, ts.URL)
  80. if err != nil {
  81. t.Fatalf("unexpected error in Get: %v", err)
  82. }
  83. // Cancel befer reading the body.
  84. // Reading Request.Body should fail, since the request was
  85. // canceled before anything was written.
  86. cancel()
  87. done := make(chan struct{})
  88. go func() {
  89. b, err := ioutil.ReadAll(resp.Body)
  90. if len(b) != 0 || err == nil {
  91. t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
  92. }
  93. close(done)
  94. }()
  95. select {
  96. case <-time.After(1 * time.Second):
  97. t.Errorf("Test timed out")
  98. case <-done:
  99. }
  100. }