gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/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  //go:build !plan9
     6  // +build !plan9
     7  
     8  package ctxhttp
     9  
    10  import (
    11  	"context"
    12  	"io"
    13  	"io/ioutil"
    14  	"testing"
    15  	"time"
    16  
    17  	"gitee.com/ks-custle/core-gm/gmhttp/httptest"
    18  
    19  	http "gitee.com/ks-custle/core-gm/gmhttp"
    20  )
    21  
    22  func TestGo17Context(t *testing.T) {
    23  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    24  		io.WriteString(w, "ok")
    25  	}))
    26  	defer ts.Close()
    27  	ctx := context.Background()
    28  	resp, err := Get(ctx, http.DefaultClient, ts.URL)
    29  	if resp == nil || err != nil {
    30  		t.Fatalf("error received from client: %v %v", err, resp)
    31  	}
    32  	resp.Body.Close()
    33  }
    34  
    35  const (
    36  	requestDuration = 100 * time.Millisecond
    37  	requestBody     = "ok"
    38  )
    39  
    40  func okHandler(w http.ResponseWriter, r *http.Request) {
    41  	time.Sleep(requestDuration)
    42  	io.WriteString(w, requestBody)
    43  }
    44  
    45  func TestNoTimeout(t *testing.T) {
    46  	ts := httptest.NewServer(http.HandlerFunc(okHandler))
    47  	defer ts.Close()
    48  
    49  	ctx := context.Background()
    50  	res, err := Get(ctx, nil, ts.URL)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	defer res.Body.Close()
    55  	slurp, err := ioutil.ReadAll(res.Body)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	if string(slurp) != requestBody {
    60  		t.Errorf("body = %q; want %q", slurp, requestBody)
    61  	}
    62  }
    63  
    64  func TestCancelBeforeHeaders(t *testing.T) {
    65  	ctx, cancel := context.WithCancel(context.Background())
    66  
    67  	blockServer := make(chan struct{})
    68  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		cancel()
    70  		<-blockServer
    71  		io.WriteString(w, requestBody)
    72  	}))
    73  	defer ts.Close()
    74  	defer close(blockServer)
    75  
    76  	res, err := Get(ctx, nil, ts.URL)
    77  	if err == nil {
    78  		res.Body.Close()
    79  		t.Fatal("Get returned unexpected nil error")
    80  	}
    81  	if err != context.Canceled {
    82  		t.Errorf("err = %v; want %v", err, context.Canceled)
    83  	}
    84  }
    85  
    86  func TestCancelAfterHangingRequest(t *testing.T) {
    87  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    88  		w.WriteHeader(http.StatusOK)
    89  		w.(http.Flusher).Flush()
    90  		<-w.(http.CloseNotifier).CloseNotify()
    91  	}))
    92  	defer ts.Close()
    93  
    94  	ctx, cancel := context.WithCancel(context.Background())
    95  	resp, err := Get(ctx, nil, ts.URL)
    96  	if err != nil {
    97  		t.Fatalf("unexpected error in Get: %v", err)
    98  	}
    99  
   100  	// Cancel befer reading the body.
   101  	// Reading Request.Body should fail, since the request was
   102  	// canceled before anything was written.
   103  	cancel()
   104  
   105  	done := make(chan struct{})
   106  
   107  	go func() {
   108  		b, err := ioutil.ReadAll(resp.Body)
   109  		if len(b) != 0 || err == nil {
   110  			t.Errorf(`Read got (%q, %v); want ("", error)`, b, err)
   111  		}
   112  		close(done)
   113  	}()
   114  
   115  	select {
   116  	case <-time.After(1 * time.Second):
   117  		t.Errorf("Test timed out")
   118  	case <-done:
   119  	}
   120  }