gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/internal/grpcutil/method_test.go (about)

     1  /*
     2   *
     3   * Copyright 2018 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * 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  
    19  package grpcutil
    20  
    21  import (
    22  	"testing"
    23  )
    24  
    25  func TestParseMethod(t *testing.T) {
    26  	testCases := []struct {
    27  		methodName  string
    28  		wantService string
    29  		wantMethod  string
    30  		wantError   bool
    31  	}{
    32  		{methodName: "/s/m", wantService: "s", wantMethod: "m", wantError: false},
    33  		{methodName: "/p.s/m", wantService: "p.s", wantMethod: "m", wantError: false},
    34  		{methodName: "/p/s/m", wantService: "p/s", wantMethod: "m", wantError: false},
    35  		{methodName: "/", wantError: true},
    36  		{methodName: "/sm", wantError: true},
    37  		{methodName: "", wantError: true},
    38  		{methodName: "sm", wantError: true},
    39  	}
    40  	for _, tc := range testCases {
    41  		s, m, err := ParseMethod(tc.methodName)
    42  		if (err != nil) != tc.wantError || s != tc.wantService || m != tc.wantMethod {
    43  			t.Errorf("ParseMethod(%s) = (%s, %s, %v), want (%s, %s, %v)", tc.methodName, s, m, err, tc.wantService, tc.wantMethod, tc.wantError)
    44  		}
    45  	}
    46  }
    47  
    48  func TestContentSubtype(t *testing.T) {
    49  	tests := []struct {
    50  		contentType string
    51  		want        string
    52  		wantValid   bool
    53  	}{
    54  		{"application/grpc", "", true},
    55  		{"application/grpc+", "", true},
    56  		{"application/grpc+blah", "blah", true},
    57  		{"application/grpc;", "", true},
    58  		{"application/grpc;blah", "blah", true},
    59  		{"application/grpcd", "", false},
    60  		{"application/grpd", "", false},
    61  		{"application/grp", "", false},
    62  	}
    63  	for _, tt := range tests {
    64  		got, gotValid := ContentSubtype(tt.contentType)
    65  		if got != tt.want || gotValid != tt.wantValid {
    66  			t.Errorf("contentSubtype(%q) = (%v, %v); want (%v, %v)", tt.contentType, got, gotValid, tt.want, tt.wantValid)
    67  		}
    68  	}
    69  }