dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/grpcutil/method_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  /*
    19   *
    20   * Copyright 2018 gRPC authors.
    21   *
    22   */
    23  
    24  package grpcutil
    25  
    26  import (
    27  	"testing"
    28  )
    29  
    30  func TestParseMethod(t *testing.T) {
    31  	testCases := []struct {
    32  		methodName  string
    33  		wantService string
    34  		wantMethod  string
    35  		wantError   bool
    36  	}{
    37  		{methodName: "/s/m", wantService: "s", wantMethod: "m", wantError: false},
    38  		{methodName: "/p.s/m", wantService: "p.s", wantMethod: "m", wantError: false},
    39  		{methodName: "/p/s/m", wantService: "p/s", wantMethod: "m", wantError: false},
    40  		{methodName: "/", wantError: true},
    41  		{methodName: "/sm", wantError: true},
    42  		{methodName: "", wantError: true},
    43  		{methodName: "sm", wantError: true},
    44  	}
    45  	for _, tc := range testCases {
    46  		s, m, err := ParseMethod(tc.methodName)
    47  		if (err != nil) != tc.wantError || s != tc.wantService || m != tc.wantMethod {
    48  			t.Errorf("ParseMethod(%s) = (%s, %s, %v), want (%s, %s, %v)", tc.methodName, s, m, err, tc.wantService, tc.wantMethod, tc.wantError)
    49  		}
    50  	}
    51  }
    52  
    53  func TestContentSubtype(t *testing.T) {
    54  	tests := []struct {
    55  		contentType string
    56  		want        string
    57  		wantValid   bool
    58  	}{
    59  		{"application/grpc", "", true},
    60  		{"application/grpc+", "", true},
    61  		{"application/grpc+blah", "blah", true},
    62  		{"application/grpc;", "", true},
    63  		{"application/grpc;blah", "blah", true},
    64  		{"application/grpcd", "", false},
    65  		{"application/grpd", "", false},
    66  		{"application/grp", "", false},
    67  	}
    68  	for _, tt := range tests {
    69  		got, gotValid := ContentSubtype(tt.contentType)
    70  		if got != tt.want || gotValid != tt.wantValid {
    71  			t.Errorf("contentSubtype(%q) = (%v, %v); want (%v, %v)", tt.contentType, got, gotValid, tt.want, tt.wantValid)
    72  		}
    73  	}
    74  }