github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/http_test.go (about) 1 // Copyright 2021 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package cdc 15 16 import ( 17 "bytes" 18 "context" 19 "fmt" 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 24 "github.com/gin-gonic/gin" 25 "github.com/pingcap/failpoint" 26 "github.com/pingcap/tiflow/cdc/capture" 27 "github.com/stretchr/testify/require" 28 ) 29 30 type testCase struct { 31 url string 32 method string 33 } 34 35 func (a *testCase) String() string { 36 return fmt.Sprintf("%s:%s", a.method, a.url) 37 } 38 39 func TestPProfPath(t *testing.T) { 40 router := gin.New() 41 RegisterRoutes(router, capture.NewCapture4Test(nil), nil) 42 43 apis := []*testCase{ 44 {"/debug/pprof/", http.MethodGet}, 45 {"/debug/pprof/cmdline", http.MethodGet}, 46 {"/debug/pprof/symbol", http.MethodGet}, 47 // these two apis make will make ut slow 48 //{"/debug/pprof/profile", http.MethodGet}, 49 //{"/debug/pprof/trace", http.MethodGet}, 50 {"/debug/pprof/threadcreate", http.MethodGet}, 51 {"/debug/pprof/allocs", http.MethodGet}, 52 {"/debug/pprof/block", http.MethodGet}, 53 {"/debug/pprof/goroutine?debug=1", http.MethodGet}, 54 {"/debug/pprof/mutex?debug=1", http.MethodGet}, 55 } 56 for _, api := range apis { 57 w := httptest.NewRecorder() 58 req, _ := http.NewRequestWithContext(context.Background(), api.method, api.url, nil) 59 router.ServeHTTP(w, req) 60 require.Equal(t, 200, w.Code, api.String()) 61 } 62 } 63 64 func TestHandleFailpoint(t *testing.T) { 65 router := gin.New() 66 RegisterRoutes(router, capture.NewCapture4Test(nil), nil) 67 fp := "github.com/pingcap/tiflow/cdc/TestHandleFailpoint" 68 uri := fmt.Sprintf("/debug/fail/%s", fp) 69 body := bytes.NewReader([]byte("return(true)")) 70 req, err := http.NewRequestWithContext(context.Background(), "PUT", uri, body) 71 require.Nil(t, err) 72 w := httptest.NewRecorder() 73 router.ServeHTTP(w, req) 74 require.True(t, w.Code >= 200 && w.Code <= 300) 75 76 failpointHit := false 77 failpoint.Inject("TestHandleFailpoint", func() { 78 failpointHit = true 79 }) 80 require.True(t, failpointHit) 81 82 req, err = http.NewRequestWithContext(context.Background(), "DELETE", uri, body) 83 require.Nil(t, err) 84 w = httptest.NewRecorder() 85 router.ServeHTTP(w, req) 86 require.True(t, w.Code >= 200 && w.Code <= 300) 87 88 failpointHit = false 89 failpoint.Inject("TestHandleFailpoint", func() { 90 failpointHit = true 91 }) 92 require.False(t, failpointHit) 93 }