1
0

ctxhttp_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. "io"
  8. "io/ioutil"
  9. "net/http"
  10. "net/http/httptest"
  11. "testing"
  12. "time"
  13. "golang.org/x/net/context"
  14. )
  15. const (
  16. requestDuration = 100 * time.Millisecond
  17. requestBody = "ok"
  18. )
  19. func okHandler(w http.ResponseWriter, r *http.Request) {
  20. time.Sleep(requestDuration)
  21. io.WriteString(w, requestBody)
  22. }
  23. func TestNoTimeout(t *testing.T) {
  24. ts := httptest.NewServer(http.HandlerFunc(okHandler))
  25. defer ts.Close()
  26. ctx := context.Background()
  27. res, err := Get(ctx, nil, ts.URL)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. defer res.Body.Close()
  32. slurp, err := ioutil.ReadAll(res.Body)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if string(slurp) != requestBody {
  37. t.Errorf("body = %q; want %q", slurp, requestBody)
  38. }
  39. }
  40. func TestCancelBeforeHeaders(t *testing.T) {
  41. ctx, cancel := context.WithCancel(context.Background())
  42. blockServer := make(chan struct{})
  43. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  44. cancel()
  45. <-blockServer
  46. io.WriteString(w, requestBody)
  47. }))
  48. defer ts.Close()
  49. defer close(blockServer)
  50. res, err := Get(ctx, nil, ts.URL)
  51. if err == nil {
  52. res.Body.Close()
  53. t.Fatal("Get returned unexpected nil error")
  54. }
  55. if err != context.Canceled {
  56. t.Errorf("err = %v; want %v", err, context.Canceled)
  57. }
  58. }
  59. func TestCancelAfterHangingRequest(t *testing.T) {
  60. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  61. w.WriteHeader(http.StatusOK)
  62. w.(http.Flusher).Flush()
  63. <-w.(http.CloseNotifier).CloseNotify()
  64. }))
  65. defer ts.Close()
  66. ctx, cancel := context.WithCancel(context.Background())
  67. resp, err := Get(ctx, nil, ts.URL)
  68. if err != nil {
  69. t.Fatalf("unexpected error in Get: %v", err)
  70. }
  71. // Cancel befer reading the body.
  72. // Reading Request.Body should fail, since the request was
  73. // canceled before anything was written.
  74. cancel()
  75. done := make(chan struct{})
  76. go func() {
  77. b, err := ioutil.ReadAll(resp.Body)
  78. if len(b) != 0 || err == nil {
  79. t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
  80. }
  81. close(done)
  82. }()
  83. select {
  84. case <-time.After(1 * time.Second):
  85. t.Errorf("Test timed out")
  86. case <-done:
  87. }
  88. }