-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeta_test.go
More file actions
183 lines (162 loc) · 5.36 KB
/
meta_test.go
File metadata and controls
183 lines (162 loc) · 5.36 KB
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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
goparser "go/parser"
"go/token"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
"github.com/go-openapi/spec"
)
func TestSetInfoVersion(t *testing.T) {
info := new(spec.Swagger)
err := setInfoVersion(info, []string{"0.0.1"})
require.NoError(t, err)
assert.EqualT(t, "0.0.1", info.Info.Version)
}
func TestSetInfoLicense(t *testing.T) {
info := new(spec.Swagger)
err := setInfoLicense(info, []string{"MIT http://license.org/MIT"})
require.NoError(t, err)
assert.EqualT(t, "MIT", info.Info.License.Name)
assert.EqualT(t, "http://license.org/MIT", info.Info.License.URL)
}
func TestSetInfoContact(t *testing.T) {
info := new(spec.Swagger)
err := setInfoContact(info, []string{"Homer J. Simpson <homer@simpsons.com> http://simpsons.com"})
require.NoError(t, err)
assert.EqualT(t, "Homer J. Simpson", info.Info.Contact.Name)
assert.EqualT(t, "homer@simpsons.com", info.Info.Contact.Email)
assert.EqualT(t, "http://simpsons.com", info.Info.Contact.URL)
}
func TestParseInfo(t *testing.T) {
swspec := new(spec.Swagger)
parser := newMetaParser(swspec)
docFile := "fixtures/goparsing/classification/doc.go"
fileSet := token.NewFileSet()
fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments)
if err != nil {
t.FailNow()
}
err = parser.Parse(fileTree.Doc)
require.NoError(t, err)
verifyInfo(t, swspec.Info)
}
func TestParseSwagger(t *testing.T) {
swspec := new(spec.Swagger)
parser := newMetaParser(swspec)
docFile := "fixtures/goparsing/classification/doc.go"
fileSet := token.NewFileSet()
fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments)
if err != nil {
t.FailNow()
}
err = parser.Parse(fileTree.Doc)
verifyMeta(t, swspec)
require.NoError(t, err)
}
func verifyMeta(t *testing.T, doc *spec.Swagger) {
assert.NotNil(t, doc)
verifyInfo(t, doc.Info)
assert.Equal(t, []string{"application/json", "application/xml"}, doc.Consumes)
assert.Equal(t, []string{"application/json", "application/xml"}, doc.Produces)
assert.Equal(t, []string{"http", "https"}, doc.Schemes)
assert.Equal(t, []map[string][]string{{"api_key": {}}}, doc.Security)
expectedSecuritySchemaKey := spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Type: "apiKey",
In: "header",
Name: "KEY",
},
}
expectedSecuritySchemaOAuth := spec.SecurityScheme{
SecuritySchemeProps: spec.SecuritySchemeProps{
Type: "oauth2",
In: "header",
AuthorizationURL: "/oauth2/auth",
TokenURL: "/oauth2/token",
Flow: "accessCode",
Scopes: map[string]string{
"bla1": "foo1",
"bla2": "foo2",
},
},
}
expectedExtensions := spec.Extensions{
"x-meta-array": []any{
"value1",
"value2",
},
"x-meta-array-obj": []any{
map[string]any{
"name": "obj",
"value": "field",
},
},
"x-meta-value": "value",
}
expectedInfoExtensions := spec.Extensions{
"x-info-array": []any{
"value1",
"value2",
},
"x-info-array-obj": []any{
map[string]any{
"name": "obj",
"value": "field",
},
},
"x-info-value": "value",
}
assert.NotNil(t, doc.SecurityDefinitions["api_key"])
assert.NotNil(t, doc.SecurityDefinitions["oauth2"])
assert.Equal(t, spec.SecurityDefinitions{"api_key": &expectedSecuritySchemaKey, "oauth2": &expectedSecuritySchemaOAuth}, doc.SecurityDefinitions)
assert.Equal(t, expectedExtensions, doc.Extensions)
assert.Equal(t, expectedInfoExtensions, doc.Info.Extensions)
assert.EqualT(t, "localhost", doc.Host)
assert.EqualT(t, "/v2", doc.BasePath)
}
func verifyInfo(t *testing.T, info *spec.Info) {
t.Helper()
require.NotNil(t, info)
assert.EqualT(t, "0.0.1", info.Version)
assert.EqualT(t, "there are no TOS at this moment, use at your own risk we take no responsibility", info.TermsOfService)
assert.EqualT(t, "Petstore API.", info.Title)
descr := `the purpose of this application is to provide an application
that is using plain go code to define an API
This should demonstrate all the possible comment annotations
that are available to turn go code into a fully compliant swagger 2.0 spec`
assert.EqualT(t, descr, info.Description)
require.NotNil(t, info.License)
assert.EqualT(t, "MIT", info.License.Name)
assert.EqualT(t, "http://opensource.org/licenses/MIT", info.License.URL)
require.NotNil(t, info.Contact)
assert.EqualT(t, "John Doe", info.Contact.Name)
assert.EqualT(t, "john.doe@example.com", info.Contact.Email)
assert.EqualT(t, "http://john.doe.com", info.Contact.URL)
}
func TestMoreParseMeta(t *testing.T) {
for _, docFile := range []string{
"fixtures/goparsing/meta/v1/doc.go",
"fixtures/goparsing/meta/v2/doc.go",
"fixtures/goparsing/meta/v3/doc.go",
"fixtures/goparsing/meta/v4/doc.go",
} {
swspec := new(spec.Swagger)
parser := newMetaParser(swspec)
fileSet := token.NewFileSet()
fileTree, err := goparser.ParseFile(fileSet, docFile, nil, goparser.ParseComments)
if err != nil {
t.FailNow()
}
err = parser.Parse(fileTree.Doc)
require.NoError(t, err)
assert.EqualT(t, "there are no TOS at this moment, use at your own risk we take no responsibility", swspec.Info.TermsOfService)
/*
jazon, err := json.MarshalIndent(swspec.Info, "", " ")
require.NoError(t, err)
t.Logf("%v", string(jazon))
*/
}
}