...
1 package descriptor
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
8 )
9
10 func TestLoadOpenAPIConfigFromYAMLRejectInvalidYAML(t *testing.T) {
11 config, err := loadOpenAPIConfigFromYAML([]byte(`
12 openapiOptions:
13 file:
14 - file: test.proto
15 - option:
16 schemes:
17 - HTTP
18 - HTTPS
19 - WSS
20 securityDefinitions:
21 security:
22 ApiKeyAuth:
23 type: TYPE_API_KEY
24 in: IN_HEADER
25 name: "X-API-Key"
26 `), "invalidyaml")
27 if err == nil {
28 t.Fatal(err)
29 }
30
31 if !strings.Contains(err.Error(), "line 3") {
32 t.Errorf("Expected yaml error to be detected in line 3. Got other error: %v", err)
33 }
34
35 if config != nil {
36 t.Fatal("Config returned")
37 }
38 }
39
40 func TestLoadOpenAPIConfigFromYAML(t *testing.T) {
41 config, err := loadOpenAPIConfigFromYAML([]byte(`
42 openapiOptions:
43 file:
44 - file: test.proto
45 option:
46 schemes:
47 - HTTP
48 - HTTPS
49 - WSS
50 securityDefinitions:
51 security:
52 ApiKeyAuth:
53 type: TYPE_API_KEY
54 in: IN_HEADER
55 name: "X-API-Key"
56 `), "openapi_options")
57 if err != nil {
58 t.Fatal(err)
59 }
60
61 if config.OpenapiOptions == nil {
62 t.Fatal("OpenAPIOptions is empty")
63 }
64
65 opts := config.OpenapiOptions
66 if numFileOpts := len(opts.File); numFileOpts != 1 {
67 t.Fatalf("expected 1 file option but got %d", numFileOpts)
68 }
69
70 fileOpt := opts.File[0]
71
72 if fileOpt.File != "test.proto" {
73 t.Fatalf("file option has unexpected binding %s", fileOpt.File)
74 }
75
76 swaggerOpt := fileOpt.Option
77
78 if swaggerOpt == nil {
79 t.Fatal("expected option to be set")
80 }
81
82 if numSchemes := len(swaggerOpt.Schemes); numSchemes != 3 {
83 t.Fatalf("expected 3 schemes but got %d", numSchemes)
84 }
85 if swaggerOpt.Schemes[0] != options.Scheme_HTTP {
86 t.Fatalf("expected first scheme to be HTTP but got %s", swaggerOpt.Schemes[0])
87 }
88 if swaggerOpt.Schemes[1] != options.Scheme_HTTPS {
89 t.Fatalf("expected second scheme to be HTTPS but got %s", swaggerOpt.Schemes[1])
90 }
91 if swaggerOpt.Schemes[2] != options.Scheme_WSS {
92 t.Fatalf("expected third scheme to be WSS but got %s", swaggerOpt.Schemes[2])
93 }
94
95 if swaggerOpt.SecurityDefinitions == nil {
96 t.Fatal("expected securityDefinitions to be set")
97 }
98 if numSecOpts := len(swaggerOpt.SecurityDefinitions.Security); numSecOpts != 1 {
99 t.Fatalf("expected 1 security option but got %d", numSecOpts)
100 }
101 secOpt, ok := swaggerOpt.SecurityDefinitions.Security["ApiKeyAuth"]
102 if !ok {
103 t.Fatal("no SecurityScheme for key \"ApiKeyAuth\"")
104 }
105 if secOpt.Type != options.SecurityScheme_TYPE_API_KEY {
106 t.Fatalf("expected scheme type to be TYPE_API_KEY but got %s", secOpt.Type)
107 }
108 if secOpt.In != options.SecurityScheme_IN_HEADER {
109 t.Fatalf("expected scheme in to be IN_HEADER but got %s", secOpt.In)
110 }
111 if secOpt.Name != "X-API-Key" {
112 t.Fatalf("expected name to be X-API-Key but got %s", secOpt.Name)
113 }
114 }
115
116 func TestLoadOpenAPIConfigFromYAMLUnknownKeys(t *testing.T) {
117 _, err := loadOpenAPIConfigFromYAML([]byte(`
118 closedapiOptions:
119 get: it?
120 openapiOptions:
121 file:
122 - file: test.proto
123 option:
124 schemes:
125 - HTTP
126 `), "openapi_options")
127 if err == nil {
128 t.Errorf("Expected invalid key error")
129 }
130
131 }
132
View as plain text