github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/api/v2/log_test.go (about)

     1  // Copyright 2023 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 v2
    15  
    16  import (
    17  	"bytes"
    18  	"context"
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"testing"
    22  
    23  	"github.com/golang/mock/gomock"
    24  	mock_capture "github.com/pingcap/tiflow/cdc/capture/mock"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestSetLogLevel(t *testing.T) {
    29  	t.Parallel()
    30  	cases := []struct {
    31  		body string
    32  		code int
    33  	}{
    34  		{`{`, 400},
    35  		{`{}`, 200},
    36  		{`{"log_level":"debug"}`, 200},
    37  		{`{"log_level":"info"}`, 200},
    38  		{`{"log_level":"warn"}`, 200},
    39  		{`{"log_level":"error"}`, 200},
    40  		{`{"log_level":"dpanic"}`, 200},
    41  		{`{"log_level":"panic"}`, 200},
    42  		{`{"log_level":"fatal"}`, 200},
    43  		{`{"log_level":"xxxx"}`, 400},
    44  	}
    45  	for _, c := range cases {
    46  		ctrl := gomock.NewController(t)
    47  		cp := mock_capture.NewMockCapture(ctrl)
    48  		cp.EXPECT().IsReady().Return(true).AnyTimes()
    49  		apiV2 := NewOpenAPIV2ForTest(cp, APIV2HelpersImpl{})
    50  		router := newRouter(apiV2)
    51  		w := httptest.NewRecorder()
    52  		req, _ := http.NewRequestWithContext(
    53  			context.Background(),
    54  			"POST",
    55  			"/api/v2/log",
    56  			bytes.NewReader([]byte(c.body)),
    57  		)
    58  		router.ServeHTTP(w, req)
    59  		require.Equal(t, c.code, w.Code)
    60  		if c.code == 200 {
    61  			require.Equal(t, "{}", w.Body.String())
    62  		}
    63  	}
    64  }