github.com/googleapis/api-linter@v1.65.2/rules/aip0134/http_uri_name_test.go (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0134
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  )
    22  
    23  func TestHttpNameField(t *testing.T) {
    24  	tests := []struct {
    25  		testName   string
    26  		URI        string
    27  		MethodName string
    28  		problems   testutils.Problems
    29  	}{
    30  		{"Valid", "/v1/{big_book.name=publishers/*/books/*}", "UpdateBigBook", nil},
    31  		{"ValidWithNumber", "/v1/{dv360.name=publishers/*/dv360s/*}", "UpdateDv360", nil},
    32  		{
    33  			"InvalidNoUnderscore", "/v1/{bigbook.name=publishers/*/books/*}",
    34  			"UpdateBigBook",
    35  			testutils.Problems{{Message: "`big_book.name`"}},
    36  		},
    37  		{
    38  			"InvalidVarNameBook", "/v1/{big_book=publishers/*/books/*}",
    39  			"UpdateBigBook",
    40  			testutils.Problems{{Message: "`big_book.name`"}},
    41  		},
    42  		{
    43  			"InvalidVarNameName", "/v1/{name=publishers/*/books/*}",
    44  			"UpdateBigBook",
    45  			testutils.Problems{{Message: "`big_book.name`"}},
    46  		},
    47  		{
    48  			"InvalidVarNameReversed", "/v1/{name.big_book=publishers/*/books/*}",
    49  			"UpdateBigBook",
    50  			testutils.Problems{{Message: "`big_book.name`"}},
    51  		},
    52  		{
    53  			"NoVarName", "/v1/publishers/*/books/*",
    54  			"UpdateBigBook",
    55  			testutils.Problems{{Message: "`big_book.name`"}},
    56  		},
    57  		{
    58  			"Irrelevant", "/v1/{book=publishers/*/books/*}",
    59  			"AcquireBigBook", nil,
    60  		},
    61  	}
    62  
    63  	for _, test := range tests {
    64  		t.Run(test.testName, func(t *testing.T) {
    65  			f := testutils.ParseProto3Tmpl(t, `
    66  				import "google/api/annotations.proto";
    67  				service Library {
    68  					rpc {{.MethodName}}({{.MethodName}}Request) returns (BigBook) {
    69  						option (google.api.http) = {
    70  							patch: "{{.URI}}"
    71  						};
    72  					}
    73  				}
    74  				message BigBook {}
    75  				message {{.MethodName}}Request {}
    76  			`, test)
    77  			method := f.GetServices()[0].GetMethods()[0]
    78  			if diff := test.problems.SetDescriptor(method).Diff(httpNameField.Lint(f)); diff != "" {
    79  				t.Error(diff)
    80  			}
    81  		})
    82  	}
    83  }