dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/rest/rest_invoker_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package rest
    19  
    20  //
    21  //import (
    22  //	"context"
    23  //	"testing"
    24  //	"time"
    25  //)
    26  //
    27  //import (
    28  //	"github.com/emicklei/go-restful/v3"
    29  //
    30  //	"github.com/stretchr/testify/assert"
    31  //)
    32  //
    33  //import (
    34  //	"dubbo.apache.org/dubbo-go/v3/common"
    35  //	"dubbo.apache.org/dubbo-go/v3/common/extension"
    36  //	"dubbo.apache.org/dubbo-go/v3/config"
    37  //	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    38  //	"dubbo.apache.org/dubbo-go/v3/protocol/rest/client"
    39  //	"dubbo.apache.org/dubbo-go/v3/protocol/rest/client/client_impl"
    40  //	rest_config "dubbo.apache.org/dubbo-go/v3/protocol/rest/config"
    41  //	"dubbo.apache.org/dubbo-go/v3/protocol/rest/server/server_impl"
    42  //)
    43  //
    44  //const (
    45  //	mockRestCommonUrl = "rest://127.0.0.1:8877/com.ikurento.user.UserProvider?anyhost=true&" +
    46  //		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
    47  //		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
    48  //		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
    49  //		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=com.ikurento.user.UserProvider"
    50  //)
    51  //
    52  //func TestRestInvokerInvoke(t *testing.T) {
    53  //	// Refer
    54  //	proto := GetRestProtocol()
    55  //	defer proto.Destroy()
    56  //	var filterNum int
    57  //	server_impl.AddGoRestfulServerFilter(func(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
    58  //		println(request.SelectedRoutePath())
    59  //		filterNum = filterNum + 1
    60  //		chain.ProcessFilter(request, response)
    61  //	})
    62  //	server_impl.AddGoRestfulServerFilter(func(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
    63  //		println("filter2")
    64  //		filterNum = filterNum + 1
    65  //		chain.ProcessFilter(request, response)
    66  //	})
    67  //
    68  //	url, err := common.NewURL(mockRestCommonUrl)
    69  //	assert.NoError(t, err)
    70  //	_, err = common.ServiceMap.Register(url.Service(), url.Protocol, "", "", &UserProvider{})
    71  //	assert.NoError(t, err)
    72  //	con := config.ProviderConfig{}
    73  //	config.SetProviderConfig(con)
    74  //	configMap := make(map[string]*rest_config.RestServiceConfig)
    75  //	methodConfigMap := make(map[string]*rest_config.RestMethodConfig)
    76  //	queryParamsMap := make(map[int]string)
    77  //	queryParamsMap[1] = "age"
    78  //	queryParamsMap[2] = "name"
    79  //	pathParamsMap := make(map[int]string)
    80  //	pathParamsMap[0] = "userid"
    81  //	headersMap := make(map[int]string)
    82  //	headersMap[3] = "Content-Type"
    83  //	methodConfigMap["GetUserOne"] = &rest_config.RestMethodConfig{
    84  //		InterfaceName:  "",
    85  //		MethodName:     "GetUserOne",
    86  //		Path:           "/GetUserOne",
    87  //		Produces:       "application/json",
    88  //		Consumes:       "application/json",
    89  //		MethodType:     "POST",
    90  //		PathParams:     "",
    91  //		PathParamsMap:  nil,
    92  //		QueryParams:    "",
    93  //		QueryParamsMap: nil,
    94  //		Body:           0,
    95  //	}
    96  //	methodConfigMap["GetUserTwo"] = &rest_config.RestMethodConfig{
    97  //		InterfaceName:  "",
    98  //		MethodName:     "GetUserTwo",
    99  //		Path:           "/GetUserTwo",
   100  //		Produces:       "application/json",
   101  //		Consumes:       "application/json",
   102  //		MethodType:     "POST",
   103  //		PathParams:     "",
   104  //		PathParamsMap:  nil,
   105  //		QueryParams:    "",
   106  //		QueryParamsMap: nil,
   107  //		Body:           0,
   108  //	}
   109  //	methodConfigMap["GetUserThree"] = &rest_config.RestMethodConfig{
   110  //		InterfaceName:  "",
   111  //		MethodName:     "GetUserThree",
   112  //		Path:           "/GetUserThree",
   113  //		Produces:       "application/json",
   114  //		Consumes:       "application/json",
   115  //		MethodType:     "POST",
   116  //		PathParams:     "",
   117  //		PathParamsMap:  nil,
   118  //		QueryParams:    "",
   119  //		QueryParamsMap: nil,
   120  //		Body:           0,
   121  //	}
   122  //	methodConfigMap["GetUserFour"] = &rest_config.RestMethodConfig{
   123  //		InterfaceName:  "",
   124  //		MethodName:     "GetUserFour",
   125  //		Path:           "/GetUserFour",
   126  //		Produces:       "application/json",
   127  //		Consumes:       "application/json",
   128  //		MethodType:     "POST",
   129  //		PathParams:     "",
   130  //		PathParamsMap:  nil,
   131  //		QueryParams:    "",
   132  //		QueryParamsMap: nil,
   133  //		Body:           0,
   134  //	}
   135  //	methodConfigMap["GetUserFive"] = &rest_config.RestMethodConfig{
   136  //		InterfaceName: "",
   137  //		MethodName:    "GetUserFive",
   138  //		Path:          "/GetUserFive",
   139  //		Produces:      "*/*",
   140  //		Consumes:      "*/*",
   141  //		MethodType:    "GET",
   142  //	}
   143  //	methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{
   144  //		InterfaceName:  "",
   145  //		MethodName:     "GetUser",
   146  //		Path:           "/GetUser/{userid}",
   147  //		Produces:       "application/json",
   148  //		Consumes:       "application/json",
   149  //		MethodType:     "GET",
   150  //		PathParams:     "",
   151  //		PathParamsMap:  pathParamsMap,
   152  //		QueryParams:    "",
   153  //		QueryParamsMap: queryParamsMap,
   154  //		Body:           -1,
   155  //		HeadersMap:     headersMap,
   156  //	}
   157  //
   158  //	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
   159  //		Server:               "go-restful",
   160  //		RestMethodConfigsMap: methodConfigMap,
   161  //	}
   162  //	rest_config.SetRestProviderServiceConfigMap(configMap)
   163  //	proxyFactory := extension.GetProxyFactory("default")
   164  //	proto.Export(proxyFactory.GetInvoker(url))
   165  //	time.Sleep(5 * time.Second)
   166  //	configMap = make(map[string]*rest_config.RestServiceConfig)
   167  //	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
   168  //		RestMethodConfigsMap: methodConfigMap,
   169  //	}
   170  //	restClient := client_impl.NewRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second})
   171  //	invoker := NewRestInvoker(url, &restClient, methodConfigMap)
   172  //	user := &User{}
   173  //	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"),
   174  //		invocation.WithArguments([]interface{}{1, int32(23), "username", "application/json"}), invocation.WithReply(user))
   175  //	res := invoker.Invoke(context.Background(), inv)
   176  //	assert.NoError(t, res.Error())
   177  //	assert.Equal(t, User{ID: 1, Age: int32(23), Name: "username"}, *res.Result().(*User))
   178  //	now := time.Now()
   179  //	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserOne"),
   180  //		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
   181  //	res = invoker.Invoke(context.Background(), inv)
   182  //	assert.NoError(t, res.Error())
   183  //	assert.NotNil(t, res.Result())
   184  //	assert.Equal(t, 1, res.Result().(*User).ID)
   185  //	assert.Equal(t, now.Unix(), res.Result().(*User).Time.Unix())
   186  //	assert.Equal(t, int32(23), res.Result().(*User).Age)
   187  //	assert.Equal(t, "username", res.Result().(*User).Name)
   188  //	// test 1
   189  //	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserTwo"),
   190  //		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
   191  //	res = invoker.Invoke(context.Background(), inv)
   192  //	assert.NoError(t, res.Error())
   193  //	assert.NotNil(t, res.Result())
   194  //	assert.Equal(t, "username", res.Result().(*User).Name)
   195  //	// test 2
   196  //	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserThree"),
   197  //		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
   198  //	res = invoker.Invoke(context.Background(), inv)
   199  //	assert.NoError(t, res.Error())
   200  //	assert.NotNil(t, res.Result())
   201  //	assert.Equal(t, "username", res.Result().(*User).Name)
   202  //	// test 3
   203  //	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFour"),
   204  //		invocation.WithArguments([]interface{}{[]User{{1, nil, int32(23), "username"}}}), invocation.WithReply(user))
   205  //	res = invoker.Invoke(context.Background(), inv)
   206  //	assert.NoError(t, res.Error())
   207  //	assert.NotNil(t, res.Result())
   208  //	assert.Equal(t, "username", res.Result().(*User).Name)
   209  //	// test 4
   210  //	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFive"), invocation.WithReply(user))
   211  //	res = invoker.Invoke(context.Background(), inv)
   212  //	assert.Error(t, res.Error(), "test error")
   213  //
   214  //	assert.Equal(t, filterNum, 12)
   215  //	err = common.ServiceMap.UnRegister(url.Service(), url.Protocol, url.ServiceKey())
   216  //	assert.NoError(t, err)
   217  //}