File: grpc_api_configuration_test.go

package info (click to toggle)
golang-github-grpc-ecosystem-grpc-gateway 2.20.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,236 kB
  • sloc: javascript: 357; makefile: 147; sh: 26
file content (188 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package descriptor

import (
	"strings"
	"testing"
)

func TestLoadGrpcAPIServiceFromYAMLInvalidType(t *testing.T) {
	// Ideally this would fail but for now this test documents that it doesn't
	service, err := loadGrpcAPIServiceFromYAML([]byte(`type: not.the.right.type`), "invalidtype")
	if err != nil {
		t.Fatal(err)
	}

	if service == nil {
		t.Fatal("No service returned")
	}
}

func TestLoadGrpcAPIServiceFromYAMLSingleRule(t *testing.T) {
	service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
 rules:
 - selector: grpctest.YourService.Echo
   post: /v1/myecho
   body: "*"
`), "example")
	if err != nil {
		t.Fatal(err)
	}

	if service.Http == nil {
		t.Fatal("HTTP is empty")
	}

	if len(service.Http.GetRules()) != 1 {
		t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
	}

	rule := service.Http.GetRules()[0]
	if rule.GetSelector() != "grpctest.YourService.Echo" {
		t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
	}
	if rule.GetPost() != "/v1/myecho" {
		t.Errorf("Rule has unexpected post '%v'", rule.GetPost())
	}
	if rule.GetBody() != "*" {
		t.Errorf("Rule has unexpected body '%v'", rule.GetBody())
	}
}

func TestLoadGrpcAPIServiceFromYAMLRejectInvalidYAML(t *testing.T) {
	service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
 rules:
 - selector: grpctest.YourService.Echo
   - post: thislinebreakstheselectorblockabovewiththeleadingdash
   body: "*"
`), "invalidyaml")
	if err == nil {
		t.Fatal(err)
	}

	if !strings.Contains(err.Error(), "line 6") {
		t.Errorf("Expected yaml error to be detected in line 6. Got other error: %v", err)
	}

	if service != nil {
		t.Fatal("Service returned")
	}
}

func TestLoadGrpcAPIServiceFromYAMLMultipleWithAdditionalBindings(t *testing.T) {
	service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
 rules:
 - selector: first.selector
   post: /my/post/path
   body: "*"
   additional_bindings:
   - post: /additional/post/path
   - put: /additional/put/{value}/path
   - delete: "{value}"
   - patch: "/additional/patch/{value}"
 - selector: some.other.service
   delete: foo
`), "example")
	if err != nil {
		t.Fatalf("Failed to load service description from YAML: %v", err)
	}

	if service == nil {
		t.Fatal("No service returned")
	}

	if service.Http == nil {
		t.Fatal("HTTP is empty")
	}

	if len(service.Http.GetRules()) != 2 {
		t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
	}

	first := service.Http.GetRules()[0]
	if first.GetSelector() != "first.selector" {
		t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
	}
	if first.GetBody() != "*" {
		t.Errorf("first.selector has unexpected body '%v'", first.GetBody())
	}
	if first.GetPost() != "/my/post/path" {
		t.Errorf("first.selector has unexpected post '%v'", first.GetPost())
	}
	if len(first.GetAdditionalBindings()) != 4 {
		t.Fatalf("first.selector has unexpected number of bindings %v instead of four. Got: %v", len(first.GetAdditionalBindings()), first.GetAdditionalBindings())
	}
	if first.GetAdditionalBindings()[0].GetPost() != "/additional/post/path" {
		t.Errorf("first.selector additional binding 0 has unexpected post '%v'", first.GetAdditionalBindings()[0].GetPost())
	}
	if first.GetAdditionalBindings()[1].GetPut() != "/additional/put/{value}/path" {
		t.Errorf("first.selector additional binding 1 has unexpected put '%v'", first.GetAdditionalBindings()[0].GetPost())
	}
	if first.GetAdditionalBindings()[2].GetDelete() != "{value}" {
		t.Errorf("first.selector additional binding 2 has unexpected delete '%v'", first.GetAdditionalBindings()[0].GetPost())
	}
	if first.GetAdditionalBindings()[3].GetPatch() != "/additional/patch/{value}" {
		t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
	}

	second := service.Http.GetRules()[1]
	if second.GetSelector() != "some.other.service" {
		t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
	}
	if second.GetDelete() != "foo" {
		t.Errorf("some.other.service has unexpected delete '%v'", second.GetDelete())
	}
	if len(second.GetAdditionalBindings()) != 0 {
		t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings())
	}
}

func TestLoadGrpcAPIServiceFromYAMLUnknownKeys(t *testing.T) {
	service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

very: key
much: 1

http:
 rules:
 - selector: some.other.service
   delete: foo
   invalidkey: yes
`), "example")
	if err != nil {
		t.Fatalf("Failed to load service description from YAML: %v", err)
	}

	if service == nil {
		t.Fatal("No service returned")
	}

	if service.Http == nil {
		t.Fatal("HTTP is empty")
	}

	if len(service.Http.GetRules()) != 1 {
		t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
	}

	first := service.Http.GetRules()[0]
	if first.GetSelector() != "some.other.service" {
		t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
	}
	if first.GetDelete() != "foo" {
		t.Errorf("first.selector has unexpected delete '%v'", first.GetPost())
	}
}