gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/handlers/handlers_go18_test.go (about)

     1  //go:build go1.8
     2  // +build go1.8
     3  
     4  package handlers
     5  
     6  import (
     7  	http "gitee.com/ks-custle/core-gm/gmhttp"
     8  	"gitee.com/ks-custle/core-gm/gmhttp/httptest"
     9  	"io/ioutil"
    10  	"testing"
    11  )
    12  
    13  // *httptest.ResponseRecorder doesn't implement Pusher, so wrap it.
    14  type pushRecorder struct {
    15  	*httptest.ResponseRecorder
    16  }
    17  
    18  func (pr pushRecorder) Push(target string, opts *http.PushOptions) error {
    19  	return nil
    20  }
    21  
    22  func TestLoggingHandlerWithPush(t *testing.T) {
    23  	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    24  		if _, ok := w.(http.Pusher); !ok {
    25  			t.Fatalf("%T from LoggingHandler does not satisfy http.Pusher interface when built with Go >=1.8", w)
    26  		}
    27  		w.WriteHeader(200)
    28  	})
    29  
    30  	logger := LoggingHandler(ioutil.Discard, handler)
    31  	logger.ServeHTTP(pushRecorder{httptest.NewRecorder()}, newRequest("GET", "/"))
    32  }
    33  
    34  func TestCombinedLoggingHandlerWithPush(t *testing.T) {
    35  	handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    36  		if _, ok := w.(http.Pusher); !ok {
    37  			t.Fatalf("%T from CombinedLoggingHandler does not satisfy http.Pusher interface when built with Go >=1.8", w)
    38  		}
    39  		w.WriteHeader(200)
    40  	})
    41  
    42  	logger := CombinedLoggingHandler(ioutil.Discard, handler)
    43  	logger.ServeHTTP(pushRecorder{httptest.NewRecorder()}, newRequest("GET", "/"))
    44  }