dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/rest/rest_protocol_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  //	"errors"
    24  //	"fmt"
    25  //	"strings"
    26  //	"testing"
    27  //	"time"
    28  //)
    29  //
    30  //import (
    31  //	"github.com/stretchr/testify/assert"
    32  //)
    33  //
    34  //import (
    35  //	"dubbo.apache.org/dubbo-go/v3/common"
    36  //	"dubbo.apache.org/dubbo-go/v3/common/extension"
    37  //	_ "dubbo.apache.org/dubbo-go/v3/proxy/proxy_factory"
    38  //	"dubbo.apache.org/dubbo-go/v3/config"
    39  //	rest_config "dubbo.apache.org/dubbo-go/v3/protocol/rest/config"
    40  //)
    41  //
    42  //func TestRestProtocolRefer(t *testing.T) {
    43  //	// Refer
    44  //	proto := GetRestProtocol()
    45  //	url, err := common.NewURL(mockRestCommonUrl)
    46  //	assert.NoError(t, err)
    47  //	con := config.ConsumerConfig{
    48  //		ConnectTimeout: "5s",
    49  //		RequestTimeout: "5s",
    50  //	}
    51  //	config.SetConsumerConfig(con)
    52  //	configMap := make(map[string]*rest_config.RestServiceConfig)
    53  //	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
    54  //		Client: "resty",
    55  //	}
    56  //	rest_config.SetRestConsumerServiceConfigMap(configMap)
    57  //	invoker := proto.Refer(url)
    58  //
    59  //	// make sure url
    60  //	eq := invoker.GetURL().URLEqual(url)
    61  //	assert.True(t, eq)
    62  //
    63  //	// make sure invokers after 'Destroy'
    64  //	invokersLen := len(proto.(*RestProtocol).Invokers())
    65  //	assert.Equal(t, 1, invokersLen)
    66  //	proto.Destroy()
    67  //	invokersLen = len(proto.(*RestProtocol).Invokers())
    68  //	assert.Equal(t, 0, invokersLen)
    69  //}
    70  //
    71  //func TestRestProtocolExport(t *testing.T) {
    72  //	// Export
    73  //	proto := GetRestProtocol()
    74  //	url, err := common.NewURL(mockRestCommonUrl)
    75  //	assert.NoError(t, err)
    76  //	_, err = common.ServiceMap.Register(url.Service(), url.Protocol, "", "", &UserProvider{})
    77  //	assert.NoError(t, err)
    78  //	con := config.ProviderConfig{}
    79  //	config.SetProviderConfig(con)
    80  //	configMap := make(map[string]*rest_config.RestServiceConfig)
    81  //	methodConfigMap := make(map[string]*rest_config.RestMethodConfig)
    82  //	queryParamsMap := make(map[int]string)
    83  //	queryParamsMap[1] = "age"
    84  //	queryParamsMap[2] = "name"
    85  //	pathParamsMap := make(map[int]string)
    86  //	pathParamsMap[0] = "userid"
    87  //	methodConfigMap["GetUser"] = &rest_config.RestMethodConfig{
    88  //		InterfaceName:  "",
    89  //		MethodName:     "GetUser",
    90  //		Path:           "/GetUser/{userid}",
    91  //		Produces:       "application/json",
    92  //		Consumes:       "application/json",
    93  //		MethodType:     "GET",
    94  //		PathParams:     "",
    95  //		PathParamsMap:  pathParamsMap,
    96  //		QueryParams:    "",
    97  //		QueryParamsMap: queryParamsMap,
    98  //		Body:           -1,
    99  //	}
   100  //	configMap["com.ikurento.user.UserProvider"] = &rest_config.RestServiceConfig{
   101  //		Server:               "go-restful",
   102  //		RestMethodConfigsMap: methodConfigMap,
   103  //	}
   104  //	rest_config.SetRestProviderServiceConfigMap(configMap)
   105  //	proxyFactory := extension.GetProxyFactory("default")
   106  //	exporter := proto.Export(proxyFactory.GetInvoker(url))
   107  //	// make sure url
   108  //	eq := exporter.GetInvoker().GetURL().URLEqual(url)
   109  //	assert.True(t, eq)
   110  //	// make sure exporterMap after 'UnExport'
   111  //	fmt.Println(url.Path)
   112  //	_, ok := proto.(*RestProtocol).ExporterMap().Load(strings.TrimPrefix(url.Path, "/"))
   113  //	assert.True(t, ok)
   114  //	exporter.UnExport()
   115  //	_, ok = proto.(*RestProtocol).ExporterMap().Load(strings.TrimPrefix(url.Path, "/"))
   116  //	assert.False(t, ok)
   117  //
   118  //	// make sure serverMap after 'Destroy'
   119  //	_, ok = proto.(*RestProtocol).serverMap[url.Location]
   120  //	assert.True(t, ok)
   121  //	proto.Destroy()
   122  //	_, ok = proto.(*RestProtocol).serverMap[url.Location]
   123  //	assert.False(t, ok)
   124  //}
   125  //
   126  //type UserProvider struct{}
   127  //
   128  //func (p *UserProvider) Reference() string {
   129  //	return "com.ikurento.user.UserProvider"
   130  //}
   131  //
   132  //func (p *UserProvider) GetUser(ctx context.Context, id int, age int32, name string, contentType string) (*User, error) {
   133  //	return &User{
   134  //		ID:   id,
   135  //		Time: nil,
   136  //		Age:  age,
   137  //		Name: name,
   138  //	}, nil
   139  //}
   140  //
   141  //func (p *UserProvider) GetUserOne(ctx context.Context, user *User) (*User, error) {
   142  //	return user, nil
   143  //}
   144  //
   145  //func (p *UserProvider) GetUserTwo(ctx context.Context, req []interface{}, rsp *User) error {
   146  //	m := req[0].(map[string]interface{})
   147  //	rsp.Name = m["Name"].(string)
   148  //	return nil
   149  //}
   150  //
   151  //func (p *UserProvider) GetUserThree(ctx context.Context, user interface{}) (*User, error) {
   152  //	m := user.(map[string]interface{})
   153  //
   154  //	u := &User{}
   155  //	u.Name = m["Name"].(string)
   156  //	return u, nil
   157  //}
   158  //
   159  //func (p *UserProvider) GetUserFour(ctx context.Context, user []interface{}, id string) (*User, error) {
   160  //	m := user[0].(map[string]interface{})
   161  //
   162  //	u := &User{}
   163  //	u.Name = m["Name"].(string)
   164  //	return u, nil
   165  //}
   166  //
   167  //func (p *UserProvider) GetUserFive(ctx context.Context, user []interface{}) (*User, error) {
   168  //	return nil, errors.New("test error")
   169  //}
   170  //
   171  //type User struct {
   172  //	ID   int
   173  //	Time *time.Time
   174  //	Age  int32
   175  //	Name string
   176  //}