github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/api/internal/rest/client_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 rest
    15  
    16  import (
    17  	"context"
    18  	"net/http"
    19  	"net/http/httptest"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func restClient(testServer *httptest.Server) (*CDCRESTClient, error) {
    26  	c, err := CDCRESTClientFromConfig(&Config{
    27  		Host:    testServer.URL,
    28  		APIPath: "/api",
    29  		Version: "v1",
    30  	})
    31  	return c, err
    32  }
    33  
    34  func TestRestRequestSuccess(t *testing.T) {
    35  	testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    36  		rw.Header().Set("Content-Type", "application/json")
    37  		rw.WriteHeader(http.StatusOK)
    38  		if r.URL.Path == "/api/v1/test" {
    39  			_, _ = rw.Write([]byte(`{"cdc": "hello world"}`))
    40  		}
    41  	}))
    42  	defer testServer.Close()
    43  
    44  	c, err := restClient(testServer)
    45  	require.Nil(t, err)
    46  	body, err := c.Get().WithPrefix("test").Do(context.Background()).Raw()
    47  	require.Equal(t, `{"cdc": "hello world"}`, string(body))
    48  	require.NoError(t, err)
    49  }
    50  
    51  func TestRestRequestFailed(t *testing.T) {
    52  	testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    53  		rw.WriteHeader(http.StatusNotFound)
    54  		_, _ = rw.Write([]byte(`{
    55  			"error_msg": "test rest request failed",
    56  			"error_code": "test rest request failed"
    57  		}`))
    58  	}))
    59  	defer testServer.Close()
    60  
    61  	c, err := restClient(testServer)
    62  	require.Nil(t, err)
    63  	err = c.Get().WithMaxRetries(1).Do(context.Background()).Error()
    64  	require.NotNil(t, err)
    65  }
    66  
    67  func TestRestRawRequestFailed(t *testing.T) {
    68  	testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    69  		rw.WriteHeader(http.StatusNotFound)
    70  		_, _ = rw.Write([]byte(`{
    71  			"error_msg": "test rest request failed",
    72  			"error_code": "test rest request failed"
    73  		}`))
    74  	}))
    75  	defer testServer.Close()
    76  
    77  	c, err := restClient(testServer)
    78  	require.Nil(t, err)
    79  	body, err := c.Get().WithMaxRetries(1).Do(context.Background()).Raw()
    80  	require.NotNil(t, body)
    81  	require.NotNil(t, err)
    82  }
    83  
    84  func TestHTTPMethods(t *testing.T) {
    85  	testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
    86  		rw.WriteHeader(http.StatusOK)
    87  	}))
    88  	defer testServer.Close()
    89  
    90  	c, _ := restClient(testServer)
    91  
    92  	req := c.Post()
    93  	require.NotNil(t, req)
    94  
    95  	req = c.Get()
    96  	require.NotNil(t, req)
    97  
    98  	req = c.Put()
    99  	require.NotNil(t, req)
   100  
   101  	req = c.Delete()
   102  	require.NotNil(t, req)
   103  }