github.com/googleapis/api-linter@v1.65.2/rules/aip0127/http_annotation_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 aip0127 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/googleapis/api-linter/rules/internal/testutils" 22 ) 23 24 func TestHasAnnotation(t *testing.T) { 25 ann := `option (google.api.http).get = "/v1/foo/";` 26 tests := []struct { 27 name string 28 Istream string 29 Ostream string 30 annotation string 31 problems testutils.Problems 32 }{ 33 {"ValidUU", "", "", ann, testutils.Problems{}}, 34 {"InvalidUU", "", "", "", testutils.Problems{{Message: "google.api.http"}}}, 35 {"ValidUS", "", "stream ", ann, testutils.Problems{}}, 36 {"InvalidUS", "", "stream ", "", testutils.Problems{{Message: "google.api.http"}}}, 37 {"ValidSU", "stream ", "", ann, testutils.Problems{}}, 38 {"InvalidSU", "stream ", "", "", testutils.Problems{{Message: "google.api.http"}}}, 39 {"ValidSS", "stream ", "stream ", "", testutils.Problems{}}, 40 {"InvalidSS", "stream ", "stream ", ann, testutils.Problems{{Message: "google.api.http"}}}, 41 } 42 for _, test := range tests { 43 t.Run(test.name, func(t *testing.T) { 44 // Use of strings.ReplaceAll here allows a replacement using quotes, 45 // which Go templates has no way to get around. 46 f := testutils.ParseProto3Tmpl(t, strings.ReplaceAll(` 47 import "google/api/annotations.proto"; 48 service Library { 49 rpc ReadBook({{.Istream}}ReadBookRequest) returns ({{.Ostream}}ReadBookResponse) { 50 {{.Annotation}} 51 } 52 } 53 message ReadBookRequest {} 54 message ReadBookResponse {} 55 `, "{{.Annotation}}", test.annotation), test) 56 m := f.GetServices()[0].GetMethods()[0] 57 if diff := test.problems.SetDescriptor(m).Diff(hasAnnotation.Lint(f)); diff != "" { 58 t.Errorf(diff) 59 } 60 }) 61 } 62 }