github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/handlers/handlers_go18_test.go (about)

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