dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/token/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 token
    19  
    20  import (
    21  	"context"
    22  	"net/url"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  import (
    31  	"dubbo.apache.org/dubbo-go/v3/common"
    32  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    33  	"dubbo.apache.org/dubbo-go/v3/protocol"
    34  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    35  )
    36  
    37  func TestTokenFilterInvoke(t *testing.T) {
    38  	filter := &tokenFilter{}
    39  
    40  	url := common.NewURLWithOptions(
    41  		common.WithParams(url.Values{}),
    42  		common.WithParamsValue(constant.TokenKey, "ori_key"))
    43  	attch := make(map[string]interface{})
    44  	attch[constant.TokenKey] = "ori_key"
    45  	result := filter.Invoke(context.Background(),
    46  		protocol.NewBaseInvoker(url),
    47  		invocation.NewRPCInvocation("MethodName",
    48  			[]interface{}{"OK"}, attch))
    49  	assert.Nil(t, result.Error())
    50  	assert.Nil(t, result.Result())
    51  }
    52  
    53  func TestTokenFilterInvokeEmptyToken(t *testing.T) {
    54  	filter := &tokenFilter{}
    55  
    56  	testUrl := common.URL{}
    57  	attch := make(map[string]interface{})
    58  	attch[constant.TokenKey] = "ori_key"
    59  	result := filter.Invoke(context.Background(), protocol.NewBaseInvoker(&testUrl), invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch))
    60  	assert.Nil(t, result.Error())
    61  	assert.Nil(t, result.Result())
    62  }
    63  
    64  func TestTokenFilterInvokeEmptyAttach(t *testing.T) {
    65  	filter := &tokenFilter{}
    66  
    67  	testUrl := common.NewURLWithOptions(
    68  		common.WithParams(url.Values{}),
    69  		common.WithParamsValue(constant.TokenKey, "ori_key"))
    70  	attch := make(map[string]interface{})
    71  	result := filter.Invoke(context.Background(), protocol.NewBaseInvoker(testUrl), invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch))
    72  	assert.NotNil(t, result.Error())
    73  }
    74  
    75  func TestTokenFilterInvokeNotEqual(t *testing.T) {
    76  	filter := &tokenFilter{}
    77  
    78  	testUrl := common.NewURLWithOptions(
    79  		common.WithParams(url.Values{}),
    80  		common.WithParamsValue(constant.TokenKey, "ori_key"))
    81  	attch := make(map[string]interface{})
    82  	attch[constant.TokenKey] = "err_key"
    83  	result := filter.Invoke(context.Background(),
    84  		protocol.NewBaseInvoker(testUrl), invocation.NewRPCInvocation("MethodName", []interface{}{"OK"}, attch))
    85  	assert.NotNil(t, result.Error())
    86  }