dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/accesslog/filter_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 accesslog
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/golang/mock/gomock"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  import (
    32  	"dubbo.apache.org/dubbo-go/v3/common"
    33  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    34  	"dubbo.apache.org/dubbo-go/v3/protocol"
    35  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    36  )
    37  
    38  func TestFilter_Invoke_Not_Config(t *testing.T) {
    39  	ctrl := gomock.NewController(t)
    40  	defer ctrl.Finish()
    41  	url, _ := common.NewURL(
    42  		"dubbo://:20000/UserProvider?app.version=0.0.1&application=BDTService&bean.name=UserProvider" +
    43  			"&cluster=failover&environment=dev&group=&interface=com.ikurento.user.UserProvider&loadbalance=random&methods.GetUser." +
    44  			"loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=" +
    45  			"BDTService&organization=ikurento.com&owner=ZX&registry.role=3&retries=&" +
    46  			"service.filter=echo%2Ctoken%2Caccesslog&timestamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100")
    47  	invoker := protocol.NewBaseInvoker(url)
    48  
    49  	attach := make(map[string]interface{}, 10)
    50  	inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
    51  
    52  	accessLogFilter := &Filter{}
    53  	result := accessLogFilter.Invoke(context.Background(), invoker, inv)
    54  	assert.Nil(t, result.Error())
    55  }
    56  
    57  func TestFilterInvokeDefaultConfig(t *testing.T) {
    58  	ctrl := gomock.NewController(t)
    59  	defer ctrl.Finish()
    60  	url, _ := common.NewURL(
    61  		"dubbo://:20000/UserProvider?app.version=0.0.1&application=BDTService&bean.name=UserProvider" +
    62  			"&cluster=failover&accesslog=true&environment=dev&group=&interface=com.ikurento.user.UserProvider&loadbalance=random&methods.GetUser." +
    63  			"loadbalance=random&methods.GetUser.retries=1&methods.GetUser.weight=0&module=dubbogo+user-info+server&name=" +
    64  			"BDTService&organization=ikurento.com&owner=ZX&registry.role=3&retries=&" +
    65  			"service.filter=echo%2Ctoken%2Caccesslog&timestamp=1569153406&token=934804bf-b007-4174-94eb-96e3e1d60cc7&version=&warmup=100")
    66  	invoker := protocol.NewBaseInvoker(url)
    67  
    68  	attach := make(map[string]interface{}, 10)
    69  	attach[constant.VersionKey] = "1.0"
    70  	attach[constant.GroupKey] = "MyGroup"
    71  	inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
    72  
    73  	accessLogFilter := &Filter{}
    74  	result := accessLogFilter.Invoke(context.Background(), invoker, inv)
    75  	assert.Nil(t, result.Error())
    76  }
    77  
    78  func TestFilterOnResponse(t *testing.T) {
    79  	result := &protocol.RPCResult{}
    80  	accessLogFilter := &Filter{}
    81  	response := accessLogFilter.OnResponse(context.TODO(), result, nil, nil)
    82  	assert.Equal(t, result, response)
    83  }