File: openapi_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 (131 lines) | stat: -rw-r--r-- 3,234 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
package descriptor

import (
	"strings"
	"testing"

	"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
)

func TestLoadOpenAPIConfigFromYAMLRejectInvalidYAML(t *testing.T) {
	config, err := loadOpenAPIConfigFromYAML([]byte(`
openapiOptions:
file:
- file: test.proto
  - option:
      schemes:
        - HTTP
        - HTTPS
        - WSS
      securityDefinitions:
        security:
          ApiKeyAuth:
            type: TYPE_API_KEY
            in: IN_HEADER
            name: "X-API-Key"
`), "invalidyaml")
	if err == nil {
		t.Fatal(err)
	}

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

	if config != nil {
		t.Fatal("Config returned")
	}
}

func TestLoadOpenAPIConfigFromYAML(t *testing.T) {
	config, err := loadOpenAPIConfigFromYAML([]byte(`
openapiOptions:
  file:
  - file: test.proto
    option:
      schemes:
      - HTTP
      - HTTPS
      - WSS
      securityDefinitions:
        security:
          ApiKeyAuth:
            type: TYPE_API_KEY
            in: IN_HEADER
            name: "X-API-Key"
`), "openapi_options")
	if err != nil {
		t.Fatal(err)
	}

	if config.OpenapiOptions == nil {
		t.Fatal("OpenAPIOptions is empty")
	}

	opts := config.OpenapiOptions
	if numFileOpts := len(opts.File); numFileOpts != 1 {
		t.Fatalf("expected 1 file option but got %d", numFileOpts)
	}

	fileOpt := opts.File[0]

	if fileOpt.File != "test.proto" {
		t.Fatalf("file option has unexpected binding %s", fileOpt.File)
	}

	swaggerOpt := fileOpt.Option

	if swaggerOpt == nil {
		t.Fatal("expected option to be set")
	}

	if numSchemes := len(swaggerOpt.Schemes); numSchemes != 3 {
		t.Fatalf("expected 3 schemes but got %d", numSchemes)
	}
	if swaggerOpt.Schemes[0] != options.Scheme_HTTP {
		t.Fatalf("expected first scheme to be HTTP but got %s", swaggerOpt.Schemes[0])
	}
	if swaggerOpt.Schemes[1] != options.Scheme_HTTPS {
		t.Fatalf("expected second scheme to be HTTPS but got %s", swaggerOpt.Schemes[1])
	}
	if swaggerOpt.Schemes[2] != options.Scheme_WSS {
		t.Fatalf("expected third scheme to be WSS but got %s", swaggerOpt.Schemes[2])
	}

	if swaggerOpt.SecurityDefinitions == nil {
		t.Fatal("expected securityDefinitions to be set")
	}
	if numSecOpts := len(swaggerOpt.SecurityDefinitions.Security); numSecOpts != 1 {
		t.Fatalf("expected 1 security option but got %d", numSecOpts)
	}
	secOpt, ok := swaggerOpt.SecurityDefinitions.Security["ApiKeyAuth"]
	if !ok {
		t.Fatal("no SecurityScheme for key \"ApiKeyAuth\"")
	}
	if secOpt.Type != options.SecurityScheme_TYPE_API_KEY {
		t.Fatalf("expected scheme type to be TYPE_API_KEY but got %s", secOpt.Type)
	}
	if secOpt.In != options.SecurityScheme_IN_HEADER {
		t.Fatalf("expected scheme  in to be IN_HEADER but got %s", secOpt.In)
	}
	if secOpt.Name != "X-API-Key" {
		t.Fatalf("expected name to be X-API-Key but got %s", secOpt.Name)
	}
}

func TestLoadOpenAPIConfigFromYAMLUnknownKeys(t *testing.T) {
	_, err := loadOpenAPIConfigFromYAML([]byte(`
closedapiOptions:
  get: it?
openapiOptions:
  file:
  - file: test.proto
    option:
      schemes:
      - HTTP
`), "openapi_options")
	if err == nil {
		t.Errorf("Expected invalid key error")
	}

}