github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/context/ctxhttp/ctxhttp_test.go (about)

     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  
     5  package ctxhttp
     6  
     7  import (
     8  	"io/ioutil"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"testing"
    12  	"time"
    13  
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  const (
    18  	requestDuration = 100 * time.Millisecond
    19  	requestBody     = "ok"
    20  )
    21  
    22  func TestNoTimeout(t *testing.T) {
    23  	ctx := context.Background()
    24  	resp, err := doRequest(ctx)
    25  
    26  	if resp == nil || err != nil {
    27  		t.Fatalf("error received from client: %v %v", err, resp)
    28  	}
    29  }
    30  
    31  func TestCancel(t *testing.T) {
    32  	ctx, cancel := context.WithCancel(context.Background())
    33  	go func() {
    34  		time.Sleep(requestDuration / 2)
    35  		cancel()
    36  	}()
    37  
    38  	resp, err := doRequest(ctx)
    39  
    40  	if resp != nil || err == nil {
    41  		t.Fatalf("expected error, didn't get one. resp: %v", resp)
    42  	}
    43  	if err != ctx.Err() {
    44  		t.Fatalf("expected error from context but got: %v", err)
    45  	}
    46  }
    47  
    48  func TestCancelAfterRequest(t *testing.T) {
    49  	ctx, cancel := context.WithCancel(context.Background())
    50  
    51  	resp, err := doRequest(ctx)
    52  
    53  	// Cancel before reading the body.
    54  	// Request.Body should still be readable after the context is canceled.
    55  	cancel()
    56  
    57  	b, err := ioutil.ReadAll(resp.Body)
    58  	if err != nil || string(b) != requestBody {
    59  		t.Fatalf("could not read body: %q %v", b, err)
    60  	}
    61  }
    62  
    63  func TestCancelAfterHangingRequest(t *testing.T) {
    64  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    65  		w.WriteHeader(http.StatusOK)
    66  		w.(http.Flusher).Flush()
    67  		<-w.(http.CloseNotifier).CloseNotify()
    68  	})
    69  
    70  	serv := httptest.NewServer(handler)
    71  	defer serv.Close()
    72  
    73  	ctx, cancel := context.WithCancel(context.Background())
    74  	resp, err := Get(ctx, nil, serv.URL)
    75  	if err != nil {
    76  		t.Fatalf("unexpected error in Get: %v", err)
    77  	}
    78  
    79  	// Cancel befer reading the body.
    80  	// Reading Request.Body should fail, since the request was
    81  	// canceled before anything was written.
    82  	cancel()
    83  
    84  	done := make(chan struct{})
    85  
    86  	go func() {
    87  		b, err := ioutil.ReadAll(resp.Body)
    88  		if len(b) != 0 || err == nil {
    89  			t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
    90  		}
    91  		close(done)
    92  	}()
    93  
    94  	select {
    95  	case <-time.After(1 * time.Second):
    96  		t.Errorf("Test timed out")
    97  	case <-done:
    98  	}
    99  }
   100  
   101  func doRequest(ctx context.Context) (*http.Response, error) {
   102  	var okHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   103  		time.Sleep(requestDuration)
   104  		w.Write([]byte(requestBody))
   105  	})
   106  
   107  	serv := httptest.NewServer(okHandler)
   108  	defer serv.Close()
   109  
   110  	return Get(ctx, nil, serv.URL)
   111  }