...
1 package descriptor
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestLoadGrpcAPIServiceFromYAMLInvalidType(t *testing.T) {
9
10 service, err := loadGrpcAPIServiceFromYAML([]byte(`type: not.the.right.type`), "invalidtype")
11 if err != nil {
12 t.Fatal(err)
13 }
14
15 if service == nil {
16 t.Fatal("No service returned")
17 }
18 }
19
20 func TestLoadGrpcAPIServiceFromYAMLSingleRule(t *testing.T) {
21 service, err := loadGrpcAPIServiceFromYAML([]byte(`
22 type: google.api.Service
23 config_version: 3
24
25 http:
26 rules:
27 - selector: grpctest.YourService.Echo
28 post: /v1/myecho
29 body: "*"
30 `), "example")
31 if err != nil {
32 t.Fatal(err)
33 }
34
35 if service.Http == nil {
36 t.Fatal("HTTP is empty")
37 }
38
39 if len(service.Http.GetRules()) != 1 {
40 t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
41 }
42
43 rule := service.Http.GetRules()[0]
44 if rule.GetSelector() != "grpctest.YourService.Echo" {
45 t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
46 }
47 if rule.GetPost() != "/v1/myecho" {
48 t.Errorf("Rule has unexpected post '%v'", rule.GetPost())
49 }
50 if rule.GetBody() != "*" {
51 t.Errorf("Rule has unexpected body '%v'", rule.GetBody())
52 }
53 }
54
55 func TestLoadGrpcAPIServiceFromYAMLRejectInvalidYAML(t *testing.T) {
56 service, err := loadGrpcAPIServiceFromYAML([]byte(`
57 type: google.api.Service
58 config_version: 3
59
60 http:
61 rules:
62 - selector: grpctest.YourService.Echo
63 - post: thislinebreakstheselectorblockabovewiththeleadingdash
64 body: "*"
65 `), "invalidyaml")
66 if err == nil {
67 t.Fatal(err)
68 }
69
70 if !strings.Contains(err.Error(), "line 6") {
71 t.Errorf("Expected yaml error to be detected in line 6. Got other error: %v", err)
72 }
73
74 if service != nil {
75 t.Fatal("Service returned")
76 }
77 }
78
79 func TestLoadGrpcAPIServiceFromYAMLMultipleWithAdditionalBindings(t *testing.T) {
80 service, err := loadGrpcAPIServiceFromYAML([]byte(`
81 type: google.api.Service
82 config_version: 3
83
84 http:
85 rules:
86 - selector: first.selector
87 post: /my/post/path
88 body: "*"
89 additional_bindings:
90 - post: /additional/post/path
91 - put: /additional/put/{value}/path
92 - delete: "{value}"
93 - patch: "/additional/patch/{value}"
94 - selector: some.other.service
95 delete: foo
96 `), "example")
97 if err != nil {
98 t.Fatalf("Failed to load service description from YAML: %v", err)
99 }
100
101 if service == nil {
102 t.Fatal("No service returned")
103 }
104
105 if service.Http == nil {
106 t.Fatal("HTTP is empty")
107 }
108
109 if len(service.Http.GetRules()) != 2 {
110 t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
111 }
112
113 first := service.Http.GetRules()[0]
114 if first.GetSelector() != "first.selector" {
115 t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
116 }
117 if first.GetBody() != "*" {
118 t.Errorf("first.selector has unexpected body '%v'", first.GetBody())
119 }
120 if first.GetPost() != "/my/post/path" {
121 t.Errorf("first.selector has unexpected post '%v'", first.GetPost())
122 }
123 if len(first.GetAdditionalBindings()) != 4 {
124 t.Fatalf("first.selector has unexpected number of bindings %v instead of four. Got: %v", len(first.GetAdditionalBindings()), first.GetAdditionalBindings())
125 }
126 if first.GetAdditionalBindings()[0].GetPost() != "/additional/post/path" {
127 t.Errorf("first.selector additional binding 0 has unexpected post '%v'", first.GetAdditionalBindings()[0].GetPost())
128 }
129 if first.GetAdditionalBindings()[1].GetPut() != "/additional/put/{value}/path" {
130 t.Errorf("first.selector additional binding 1 has unexpected put '%v'", first.GetAdditionalBindings()[0].GetPost())
131 }
132 if first.GetAdditionalBindings()[2].GetDelete() != "{value}" {
133 t.Errorf("first.selector additional binding 2 has unexpected delete '%v'", first.GetAdditionalBindings()[0].GetPost())
134 }
135 if first.GetAdditionalBindings()[3].GetPatch() != "/additional/patch/{value}" {
136 t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
137 }
138
139 second := service.Http.GetRules()[1]
140 if second.GetSelector() != "some.other.service" {
141 t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
142 }
143 if second.GetDelete() != "foo" {
144 t.Errorf("some.other.service has unexpected delete '%v'", second.GetDelete())
145 }
146 if len(second.GetAdditionalBindings()) != 0 {
147 t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings())
148 }
149 }
150
151 func TestLoadGrpcAPIServiceFromYAMLUnknownKeys(t *testing.T) {
152 service, err := loadGrpcAPIServiceFromYAML([]byte(`
153 type: google.api.Service
154 config_version: 3
155
156 very: key
157 much: 1
158
159 http:
160 rules:
161 - selector: some.other.service
162 delete: foo
163 invalidkey: yes
164 `), "example")
165 if err != nil {
166 t.Fatalf("Failed to load service description from YAML: %v", err)
167 }
168
169 if service == nil {
170 t.Fatal("No service returned")
171 }
172
173 if service.Http == nil {
174 t.Fatal("HTTP is empty")
175 }
176
177 if len(service.Http.GetRules()) != 1 {
178 t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
179 }
180
181 first := service.Http.GetRules()[0]
182 if first.GetSelector() != "some.other.service" {
183 t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
184 }
185 if first.GetDelete() != "foo" {
186 t.Errorf("first.selector has unexpected delete '%v'", first.GetPost())
187 }
188 }
189
View as plain text