Skip to content
Snippets Groups Projects
Select Git revision
  • 57a2a8fd96f8813a7547c87b8bc1cc07ab282c1c
  • master default protected
  • 3.0
  • develop
  • revert-2069-tripleVersion
  • 3.1
  • rest-protocol
  • feat/remoting_rocketmq
  • dapr-support
  • 1.5
  • 1.4
  • 1.3
  • 1.2
  • 1.1
  • v3.0.3-rc2
  • v3.0.3-rc1
  • v3.0.2
  • v1.5.8
  • v1.5.9-rc1
  • v3.0.1
  • v1.5.8-rc1
  • v3.0.0
  • v3.0.0-rc4-1
  • v3.0.0-rc4
  • v3.0.0-rc3
  • v1.5.7
  • v1.5.7-rc2
  • v3.0.0-rc2
  • remove
  • v1.5.7-rc1
  • v3.0.0-rc1
  • v1.5.7-rc1-tmp
  • 1.5.6
  • v1.5.6
34 results

rest_invoker_test.go

Blame
  • rest_invoker_test.go 7.43 KiB
    /*
     * Licensed to the Apache Software Foundation (ASF) under one or more
     * contributor license agreements.  See the NOTICE file distributed with
     * this work for additional information regarding copyright ownership.
     * The ASF licenses this file to You under the Apache License, Version 2.0
     * (the "License"); you may not use this file except in compliance with
     * the License.  You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package rest
    
    import (
    	"context"
    	"testing"
    	"time"
    )
    
    import (
    	"github.com/stretchr/testify/assert"
    )
    
    import (
    	"github.com/apache/dubbo-go/common"
    	"github.com/apache/dubbo-go/common/extension"
    	"github.com/apache/dubbo-go/config"
    	"github.com/apache/dubbo-go/protocol/invocation"
    	"github.com/apache/dubbo-go/protocol/rest/client"
    	"github.com/apache/dubbo-go/protocol/rest/client/client_impl"
    	rest_config "github.com/apache/dubbo-go/protocol/rest/config"
    )
    
    func TestRestInvoker_Invoke(t *testing.T) {
    	// Refer
    	proto := GetRestProtocol()
    	defer proto.Destroy()
    	url, err := common.NewURL("rest://127.0.0.1:8877/com.ikurento.user.UserProvider?anyhost=true&" +
    		"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&" +
    		"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&" +
    		"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&" +
    		"side=provider&timeout=3000&timestamp=1556509797245")
    	assert.NoError(t, err)
    	_, err = common.ServiceMap.Register(url.Protocol, &UserProvider{})
    	assert.NoError(t, err)
    	con := config.ProviderConfig{}
    	config.SetProviderConfig(con)
    	configMap := make(map[string]*rest_config.RestServiceConfig)
    	methodConfigMap := make(map[string]*rest_config.RestMethodConfig)
    	queryParamsMap := make(map[int]string)
    	queryParamsMap[1] = "age"
    	queryParamsMap[2] = "name"
    	pathParamsMap := make(map[int]string)
    	pathParamsMap[0] = "userid"
    	headersMap := make(map[int]string)
    	headersMap[3] = "Content-Type"
    	methodConfigMap["GetUserOne"] = &rest_config.RestMethodConfig{
    		InterfaceName:  "",
    		MethodName:     "GetUserOne",
    		Path:           "/GetUserOne",
    		Produces:       "application/json",
    		Consumes:       "application/json",
    		MethodType:     "POST",
    		PathParams:     "",
    		PathParamsMap:  nil,
    		QueryParams:    "",
    		QueryParamsMap: nil,
    		Body:           0,
    	}
    	methodConfigMap["GetUserTwo"] = &rest_config.RestMethodConfig{
    		InterfaceName:  "",
    		MethodName:     "GetUserTwo",
    		Path:           "/GetUserTwo",
    		Produces:       "application/json",
    		Consumes:       "application/json",
    		MethodType:     "POST",
    		PathParams:     "",
    		PathParamsMap:  nil,
    		QueryParams:    "",
    		QueryParamsMap: nil,
    		Body:           0,
    	}
    	methodConfigMap["GetUserThree"] = &rest_config.RestMethodConfig{
    		InterfaceName:  "",
    		MethodName:     "GetUserThree",
    		Path:           "/GetUserThree",
    		Produces:       "application/json",
    		Consumes:       "application/json",
    		MethodType:     "POST",
    		PathParams:     "",
    		PathParamsMap:  nil,
    		QueryParams:    "",
    		QueryParamsMap: nil,
    		Body:           0,
    	}
    	methodConfigMap["GetUserFour"] = &rest_config.RestMethodConfig{
    		InterfaceName:  "",
    		MethodName:     "GetUserFour",
    		Path:           "/GetUserFour",
    		Produces:       "application/json",
    		Consumes:       "application/json",
    		MethodType:     "POST",
    		PathParams:     "",
    		PathParamsMap:  nil,
    		QueryParams:    "",
    		QueryParamsMap: nil,
    		Body:           0,
    	}
    	methodConfigMap["GetUserFive"] = &rest_config.RestMethodConfig{
    		InterfaceName: "",
    		MethodName:    "GetUserFive",
    		Path:          "/GetUserFive",
    		Produces:      "*/*",
    		Consumes:      "*/*",
    		MethodType:    "GET",
    	}
    	methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{
    		InterfaceName:  "",
    		MethodName:     "GetUser",
    		Path:           "/GetUser/{userid}",
    		Produces:       "application/json",
    		Consumes:       "application/json",
    		MethodType:     "GET",
    		PathParams:     "",
    		PathParamsMap:  pathParamsMap,
    		QueryParams:    "",
    		QueryParamsMap: queryParamsMap,
    		Body:           -1,
    		HeadersMap:     headersMap,
    	}
    
    	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
    		Server:               "go-restful",
    		RestMethodConfigsMap: methodConfigMap,
    	}
    	rest_config.SetRestProviderServiceConfigMap(configMap)
    	proxyFactory := extension.GetProxyFactory("default")
    	proto.Export(proxyFactory.GetInvoker(url))
    	time.Sleep(5 * time.Second)
    	configMap = make(map[string]*rest_config.RestServiceConfig)
    	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
    		RestMethodConfigsMap: methodConfigMap,
    	}
    	restClient := client_impl.NewRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second})
    	invoker := NewRestInvoker(url, &restClient, methodConfigMap)
    	user := &User{}
    	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"),
    		invocation.WithArguments([]interface{}{1, int32(23), "username", "application/json"}), invocation.WithReply(user))
    	res := invoker.Invoke(context.Background(), inv)
    	assert.NoError(t, res.Error())
    	assert.Equal(t, User{Id: 1, Age: int32(23), Name: "username"}, *res.Result().(*User))
    	now := time.Now()
    	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserOne"),
    		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
    	res = invoker.Invoke(context.Background(), inv)
    	assert.NoError(t, res.Error())
    	assert.NotNil(t, res.Result())
    	assert.Equal(t, 1, res.Result().(*User).Id)
    	assert.Equal(t, now.Unix(), res.Result().(*User).Time.Unix())
    	assert.Equal(t, int32(23), res.Result().(*User).Age)
    	assert.Equal(t, "username", res.Result().(*User).Name)
    	// test 1
    	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserTwo"),
    		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
    	res = invoker.Invoke(context.Background(), inv)
    	assert.NoError(t, res.Error())
    	assert.NotNil(t, res.Result())
    	assert.Equal(t, "username", res.Result().(*User).Name)
    	// test 2
    	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserThree"),
    		invocation.WithArguments([]interface{}{&User{1, &now, int32(23), "username"}}), invocation.WithReply(user))
    	res = invoker.Invoke(context.Background(), inv)
    	assert.NoError(t, res.Error())
    	assert.NotNil(t, res.Result())
    	assert.Equal(t, "username", res.Result().(*User).Name)
    	// test 3
    	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFour"),
    		invocation.WithArguments([]interface{}{[]User{User{1, nil, int32(23), "username"}}}), invocation.WithReply(user))
    	res = invoker.Invoke(context.Background(), inv)
    	assert.NoError(t, res.Error())
    	assert.NotNil(t, res.Result())
    	assert.Equal(t, "username", res.Result().(*User).Name)
    	// test 4
    	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFive"), invocation.WithReply(user))
    	res = invoker.Invoke(context.Background(), inv)
    	assert.Error(t, res.Error(), "test error")
    
    	err = common.ServiceMap.UnRegister(url.Protocol, "com.ikurento.user.UserProvider")
    	assert.NoError(t, err)
    }