trpc.group/trpc-go/trpc-go@v1.0.3/internal/httprule/match_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package httprule_test
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  	"trpc.group/trpc-go/trpc-go/internal/httprule"
    22  )
    23  
    24  func TestMatch(t *testing.T) {
    25  	tests := []struct {
    26  		toParse      string
    27  		toMatch      string
    28  		wantCaptured map[string]string
    29  		wantErr      bool
    30  		desc         string
    31  	}{
    32  		{
    33  			toParse: "/foobar/{foo}/bar/{baz}",
    34  			toMatch: "/foobar/x/bar/y",
    35  			wantCaptured: map[string]string{
    36  				"foo": "x",
    37  				"baz": "y",
    38  			},
    39  			wantErr: false,
    40  			desc:    "MatchTest01",
    41  		},
    42  		{
    43  			toParse: "/foobar/{foo=x/*}/bar/{baz}",
    44  			toMatch: "/foobar/x/y/bar/z",
    45  			wantCaptured: map[string]string{
    46  				"foo": "x/y",
    47  				"baz": "z",
    48  			},
    49  			wantErr: false,
    50  			desc:    "MatchTest02",
    51  		},
    52  		{
    53  			toParse:      "/foo/bar/**",
    54  			toMatch:      "/foo/bar/x/y/z",
    55  			wantCaptured: map[string]string{},
    56  			wantErr:      false,
    57  			desc:         "MatchTest03",
    58  		},
    59  		{
    60  			toParse:      "/foo/*/bar",
    61  			toMatch:      "/foo/x/bar",
    62  			wantCaptured: map[string]string{},
    63  			wantErr:      false,
    64  			desc:         "MatchTest04",
    65  		},
    66  		{
    67  			toParse: "/a/b/{c}:d:e",
    68  			toMatch: "/a/b/x:y:z:d:e",
    69  			wantCaptured: map[string]string{
    70  				"c": "x:y:z",
    71  			},
    72  			wantErr: false,
    73  			desc:    "MatchTest05",
    74  		},
    75  		{
    76  			toParse:      "/a/b/{c}:d:e",
    77  			toMatch:      "/a/b/anything",
    78  			wantCaptured: nil,
    79  			wantErr:      true,
    80  			desc:         "MatchTest06",
    81  		},
    82  		{
    83  			toParse:      "/a/b/{c}:d:e",
    84  			toMatch:      "/a/b/:d:e",
    85  			wantCaptured: nil,
    86  			wantErr:      true,
    87  			desc:         "MatchTest07",
    88  		},
    89  		{
    90  			toParse:      "/foo/bar/{a=*}",
    91  			toMatch:      "/foo/bar/x/y/z",
    92  			wantCaptured: nil,
    93  			wantErr:      true,
    94  			desc:         "MatchTest08",
    95  		},
    96  		{
    97  			toParse: "/foo/bar/{a=**}",
    98  			toMatch: "/foo/bar/x/y/z",
    99  			wantCaptured: map[string]string{
   100  				"a": "x/y/z",
   101  			},
   102  			wantErr: false,
   103  			desc:    "MatchTest09",
   104  		},
   105  		{
   106  			toParse: "/foo/bar/{a=**}",
   107  			toMatch: "/foo/bar/",
   108  			wantCaptured: map[string]string{
   109  				"a": "",
   110  			},
   111  			wantErr: false,
   112  			desc:    "MatchTest10",
   113  		},
   114  		{
   115  			toParse: "/foo/bar/{a=**}",
   116  			toMatch: "/foo/bar",
   117  			wantCaptured: map[string]string{
   118  				"a": "",
   119  			},
   120  			wantErr: false,
   121  			desc:    "MatchTest11",
   122  		},
   123  	}
   124  
   125  	for _, test := range tests {
   126  		tpl, err := httprule.Parse(test.toParse)
   127  		require.Nil(t, err)
   128  		gotCaptured, gotErr := tpl.Match(test.toMatch)
   129  		require.Equal(t, true, reflect.DeepEqual(test.wantCaptured, gotCaptured), test.desc)
   130  		require.Equal(t, test.wantErr, gotErr != nil, test.desc)
   131  	}
   132  }