github.com/grafana/pyroscope@v1.18.0/pkg/frontend/vcs/client/metrics_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func Test_matchGitHubAPIRoute(t *testing.T) {
    10  	tests := []struct {
    11  		Name string
    12  		Path string
    13  		Want string
    14  	}{
    15  		{
    16  			Name: "GetContents",
    17  			Path: "/repos/grafana/pyroscope/contents/pkg/querier/querier.go",
    18  			Want: "/repos/{owner}/{repo}/contents/{path}",
    19  		},
    20  		{
    21  			Name: "GetContents with dash",
    22  			Path: "/repos/connectrpc/connect-go/contents/protocol.go",
    23  			Want: "/repos/{owner}/{repo}/contents/{path}",
    24  		},
    25  		{
    26  			Name: "GetContents without path",
    27  			Path: "/repos/grafana/pyroscope/contents/",
    28  			Want: "unknown_route",
    29  		},
    30  		{
    31  			Name: "GetContents with whitespace in path",
    32  			Path: "/repos/grafana/pyroscope/contents/path with spaces",
    33  			Want: "unknown_route",
    34  		},
    35  		{
    36  			Name: "GetCommit",
    37  			Path: "/repos/grafana/pyroscope/commits/abcdef1234567890",
    38  			Want: "/repos/{owner}/{repo}/commits/{ref}",
    39  		},
    40  		{
    41  			Name: "GetCommit with lowercase and uppercase ref",
    42  			Path: "/repos/grafana/pyroscope/commits/abcdefABCDEF1234567890",
    43  			Want: "/repos/{owner}/{repo}/commits/{ref}",
    44  		},
    45  		{
    46  			Name: "GetCommit with non-hexadecimal ref",
    47  			Path: "/repos/grafana/pyroscope/commits/HEAD",
    48  			Want: "/repos/{owner}/{repo}/commits/{ref}",
    49  		},
    50  		{
    51  			Name: "GetCommit without commit",
    52  			Path: "/repos/grafana/pyroscope/commits/",
    53  			Want: "unknown_route",
    54  		},
    55  		{
    56  			Name: "Refresh",
    57  			Path: "/login/oauth/access_token",
    58  			Want: "/login/oauth/access_token",
    59  		},
    60  		{
    61  			Name: "empty path",
    62  			Path: "",
    63  			Want: "unknown_route",
    64  		},
    65  		{
    66  			Name: "unmapped path",
    67  			Path: "/some/random/path",
    68  			Want: "unknown_route",
    69  		},
    70  	}
    71  
    72  	for _, tt := range tests {
    73  		t.Run(tt.Name, func(t *testing.T) {
    74  			got := matchGitHubAPIRoute(tt.Path)
    75  			require.Equal(t, tt.Want, got)
    76  		})
    77  	}
    78  }