Skip to content
Snippets Groups Projects
rest_invoker_test.go 8.13 KiB
Newer Older
Patrick's avatar
Patrick committed
/*
 * 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.
 */

Patrick's avatar
Patrick committed
package rest

import (
	"context"
Patrick's avatar
Patrick committed
	"testing"
	"time"
)

import (
Patrick's avatar
Patrick committed
	"github.com/emicklei/go-restful/v3"
Patrick's avatar
Patrick committed
	"github.com/stretchr/testify/assert"
)

import (
Patrick's avatar
Patrick committed
	"github.com/apache/dubbo-go/common"
Patrick's avatar
Patrick committed
	"github.com/apache/dubbo-go/common/extension"
Patrick's avatar
Patrick committed
	"github.com/apache/dubbo-go/config"
	"github.com/apache/dubbo-go/protocol/invocation"
Patrick's avatar
Patrick committed
	"github.com/apache/dubbo-go/protocol/rest/client"
	"github.com/apache/dubbo-go/protocol/rest/client/client_impl"
fangyincheng's avatar
fangyincheng committed
	rest_config "github.com/apache/dubbo-go/protocol/rest/config"
Patrick's avatar
Patrick committed
	"github.com/apache/dubbo-go/protocol/rest/server/server_impl"
watermelo's avatar
watermelo committed
const (
	mockRestCommonUrl = "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&" +
Patrick's avatar
Patrick committed
		"side=provider&timeout=3000&timestamp=1556509797245&bean.name=com.ikurento.user.UserProvider"
watermelo's avatar
watermelo committed
)

func TestRestInvokerInvoke(t *testing.T) {
Patrick's avatar
Patrick committed
	// Refer
Patrick's avatar
Patrick committed
	proto := GetRestProtocol()
	defer proto.Destroy()
Patrick's avatar
Patrick committed
	var filterNum int
	server_impl.AddGoRestfulServerFilter(func(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
		println(request.SelectedRoutePath())
		filterNum = filterNum + 1
		chain.ProcessFilter(request, response)
	})
	server_impl.AddGoRestfulServerFilter(func(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
		println("filter2")
		filterNum = filterNum + 1
		chain.ProcessFilter(request, response)
	})

watermelo's avatar
watermelo committed
	url, err := common.NewURL(mockRestCommonUrl)
Patrick's avatar
Patrick committed
	assert.NoError(t, err)
	_, err = common.ServiceMap.Register(url.Service(), url.Protocol, "", "", &UserProvider{})
Patrick's avatar
Patrick committed
	assert.NoError(t, err)
	con := config.ProviderConfig{}
	config.SetProviderConfig(con)
fangyincheng's avatar
fangyincheng committed
	configMap := make(map[string]*rest_config.RestServiceConfig)
	methodConfigMap := make(map[string]*rest_config.RestMethodConfig)
Patrick's avatar
Patrick committed
	queryParamsMap := make(map[int]string)
	queryParamsMap[1] = "age"
	queryParamsMap[2] = "name"
	pathParamsMap := make(map[int]string)
	pathParamsMap[0] = "userid"
Patrick's avatar
Patrick committed
	headersMap := make(map[int]string)
	headersMap[3] = "Content-Type"
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUserOne"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName:  "",
		MethodName:     "GetUserOne",
		Path:           "/GetUserOne",
		Produces:       "application/json",
		Consumes:       "application/json",
		MethodType:     "POST",
		PathParams:     "",
		PathParamsMap:  nil,
		QueryParams:    "",
		QueryParamsMap: nil,
		Body:           0,
	}
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUserTwo"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName:  "",
		MethodName:     "GetUserTwo",
		Path:           "/GetUserTwo",
		Produces:       "application/json",
		Consumes:       "application/json",
		MethodType:     "POST",
		PathParams:     "",
		PathParamsMap:  nil,
		QueryParams:    "",
		QueryParamsMap: nil,
		Body:           0,
	}
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUserThree"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName:  "",
		MethodName:     "GetUserThree",
		Path:           "/GetUserThree",
		Produces:       "application/json",
		Consumes:       "application/json",
		MethodType:     "POST",
		PathParams:     "",
		PathParamsMap:  nil,
		QueryParams:    "",
		QueryParamsMap: nil,
		Body:           0,
	}
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUserFour"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName:  "",
		MethodName:     "GetUserFour",
		Path:           "/GetUserFour",
		Produces:       "application/json",
		Consumes:       "application/json",
		MethodType:     "POST",
		PathParams:     "",
		PathParamsMap:  nil,
		QueryParams:    "",
		QueryParamsMap: nil,
		Body:           0,
	}
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUserFive"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName: "",
		MethodName:    "GetUserFive",
		Path:          "/GetUserFive",
		Produces:      "*/*",
		Consumes:      "*/*",
		MethodType:    "GET",
	}
fangyincheng's avatar
fangyincheng committed
	methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{
Patrick's avatar
Patrick committed
		InterfaceName:  "",
		MethodName:     "GetUser",
Patrick's avatar
Patrick committed
		Path:           "/GetUser/{userid}",
Patrick's avatar
Patrick committed
		Produces:       "application/json",
		Consumes:       "application/json",
		MethodType:     "GET",
		PathParams:     "",
Patrick's avatar
Patrick committed
		PathParamsMap:  pathParamsMap,
Patrick's avatar
Patrick committed
		QueryParams:    "",
Patrick's avatar
Patrick committed
		QueryParamsMap: queryParamsMap,
		Body:           -1,
Patrick's avatar
Patrick committed
		HeadersMap:     headersMap,
Patrick's avatar
Patrick committed
	}
Patrick's avatar
Patrick committed

fangyincheng's avatar
fangyincheng committed
	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
Patrick's avatar
Patrick committed
		Server:               "go-restful",
		RestMethodConfigsMap: methodConfigMap,
	}
fangyincheng's avatar
fangyincheng committed
	rest_config.SetRestProviderServiceConfigMap(configMap)
Patrick's avatar
Patrick committed
	proxyFactory := extension.GetProxyFactory("default")
	proto.Export(proxyFactory.GetInvoker(url))
	time.Sleep(5 * time.Second)
fangyincheng's avatar
fangyincheng committed
	configMap = make(map[string]*rest_config.RestServiceConfig)
	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
Patrick's avatar
Patrick committed
		RestMethodConfigsMap: methodConfigMap,
	}
Patrick's avatar
Patrick committed
	restClient := client_impl.NewRestyClient(&client.RestOptions{ConnectTimeout: 3 * time.Second, RequestTimeout: 3 * time.Second})
	invoker := NewRestInvoker(url, &restClient, methodConfigMap)
Patrick's avatar
Patrick committed
	user := &User{}
	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"),
Patrick's avatar
Patrick committed
		invocation.WithArguments([]interface{}{1, int32(23), "username", "application/json"}), invocation.WithReply(user))
Patrick's avatar
Patrick committed
	res := invoker.Invoke(context.Background(), inv)
	assert.NoError(t, res.Error())
AlexStocks's avatar
AlexStocks committed
	assert.Equal(t, User{ID: 1, Age: int32(23), Name: "username"}, *res.Result().(*User))
Patrick's avatar
Patrick committed
	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())
Patrick's avatar
Patrick committed
	assert.NotNil(t, res.Result())
AlexStocks's avatar
AlexStocks committed
	assert.Equal(t, 1, res.Result().(*User).ID)
Patrick's avatar
Patrick committed
	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)
Patrick's avatar
Patrick committed
	// test 1
Patrick's avatar
Patrick committed
	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)
Patrick's avatar
Patrick committed
	// test 2
Patrick's avatar
Patrick committed
	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)
Patrick's avatar
Patrick committed
	// test 3
Patrick's avatar
Patrick committed
	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFour"),
georgehao's avatar
georgehao committed
		invocation.WithArguments([]interface{}{[]User{{1, nil, int32(23), "username"}}}), invocation.WithReply(user))
Patrick's avatar
Patrick committed
	res = invoker.Invoke(context.Background(), inv)
	assert.NoError(t, res.Error())
	assert.NotNil(t, res.Result())
	assert.Equal(t, "username", res.Result().(*User).Name)
Patrick's avatar
Patrick committed
	// test 4
Patrick's avatar
Patrick committed
	inv = invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUserFive"), invocation.WithReply(user))
	res = invoker.Invoke(context.Background(), inv)
	assert.Error(t, res.Error(), "test error")

Patrick's avatar
Patrick committed
	assert.Equal(t, filterNum, 12)
	err = common.ServiceMap.UnRegister(url.Service(), url.Protocol, url.ServiceKey())
Patrick's avatar
Patrick committed
	assert.NoError(t, err)
Patrick's avatar
Patrick committed
}