github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/handlers/log_handler_test.go (about) 1 package handlers 2 3 import ( 4 "context" 5 "net/http" 6 "time" 7 8 "github.com/machinefi/w3bstream/pkg/depends/conf/log" 9 "github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/mock" 10 ) 11 12 func ExampleLogHandler() { 13 ctx := log.WithLogger(context.Background(), log.Std()) 14 15 var handle http.HandlerFunc = func(rw http.ResponseWriter, req *http.Request) { 16 time.Sleep(20 * time.Millisecond) 17 18 switch req.Method { 19 case http.MethodGet: 20 rw.WriteHeader(http.StatusOK) 21 _, _ = rw.Write([]byte(`{"status":"ok"}`)) 22 case http.MethodPost: 23 rw.WriteHeader(http.StatusNoContent) 24 case http.MethodDelete: 25 rw.WriteHeader(http.StatusBadRequest) 26 _, _ = rw.Write([]byte(`{"key":"StatusBadRequest","msg":"something wrong"}`)) 27 case http.MethodPut: 28 rw.WriteHeader(http.StatusInternalServerError) 29 _, _ = rw.Write([]byte(`{"key":"StatusInternalServerError","msg":"internal server error"}`)) 30 } 31 } 32 33 handler := LogHandler()(handle).(*loggerHandler) 34 35 for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodDelete, http.MethodPost} { 36 req, _ := http.NewRequestWithContext(ctx, method, "/", nil) 37 handler.ServeHTTP(mock.NewMockResponseWriter(), req) 38 } 39 // Output: 40 }