-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathspec_test.go
More file actions
211 lines (160 loc) · 5.23 KB
/
spec_test.go
File metadata and controls
211 lines (160 loc) · 5.23 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package loads
import (
_ "embed"
"encoding/json"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
//go:embed fixtures/json/petstore-basic.json
var petStoreJSON []byte
//go:embed fixtures/yaml/search.yaml
var yamlSpec []byte
//go:embed fixtures/json/expected-expanded.json
var expectedExpanded []byte
//go:embed fixtures/json/cascade-ref-expanded.json
var cascadeRefExpanded []byte
func TestUnknownSpecVersion(t *testing.T) {
_, err := Analyzed([]byte{}, "0.9")
require.Error(t, err)
}
func TestDefaultsTo20(t *testing.T) {
d, err := Analyzed(petStoreJSON, "")
require.NoError(t, err)
require.NotNil(t, d)
assert.EqualT(t, "2.0", d.Version())
assert.EqualT(t, "/api", d.BasePath())
}
func TestLoadsYAMLContent(t *testing.T) {
d, err := Analyzed(yamlSpec, "")
require.NoError(t, err)
require.NotNil(t, d)
sw := d.Spec()
assert.EqualT(t, "1.0.0", sw.Info.Version)
}
// for issue #11.
func TestRegressionExpand(t *testing.T) {
swaggerFile := "fixtures/yaml/swagger/1/2/3/4/swagger.yaml"
document, err := Spec(swaggerFile)
require.NoError(t, err)
require.NotNil(t, document)
d, err := document.Expanded()
require.NoError(t, err)
require.NotNil(t, d)
assert.JSONMarshalAsT(t, expectedExpanded, d.Spec())
}
func TestCascadingRefExpand(t *testing.T) {
swaggerFile := "fixtures/yaml/swagger/spec.yml"
document, err := Spec(swaggerFile)
require.NoError(t, err)
require.NotNil(t, document)
d, err := document.Expanded()
require.NoError(t, err)
require.NotNil(t, d)
assert.JSONMarshalAsT(t, cascadeRefExpanded, d.Spec())
}
func TestFailsInvalidJSON(t *testing.T) {
_, err := Analyzed(json.RawMessage([]byte("{]")), "")
require.Error(t, err)
}
// issue go-swagger/go-swagger#1816 (regression when cloning original spec).
func TestIssue1846(t *testing.T) {
swaggerFile := "fixtures/bugs/1816/fixture-1816.yaml"
document, err := Spec(swaggerFile)
require.NoError(t, err)
require.NotNil(t, document)
sp, err := cloneSpec(document.Spec())
require.NoError(t, err)
jazon, err := json.MarshalIndent(sp, "", " ")
require.NoError(t, err)
rex := regexp.MustCompile(`"\$ref":\s*"(.+)"`)
m := rex.FindAllStringSubmatch(string(jazon), -1)
require.NotNil(t, m)
for _, matched := range m {
subMatch := matched[1]
require.TrueTf(t,
strings.HasPrefix(subMatch, "#/definitions") || strings.HasPrefix(subMatch, "#/responses"),
"expected $ref to point either to definitions or responses section, got: %s", matched[0])
}
}
func TestEmbedded(t *testing.T) {
swaggerFile := "fixtures/yaml/swagger/spec.yml"
document, err := Spec(swaggerFile)
require.NoError(t, err)
require.NotNil(t, document)
raw, err := json.Marshal(document.Raw())
require.NoError(t, err)
spc, err := json.Marshal(document.Spec())
require.NoError(t, err)
d, err := Embedded(raw, spc)
require.NoError(t, err)
require.NotNil(t, d)
assert.JSONMarshalAsT(t, raw, d.Raw())
assert.JSONMarshalAsT(t, spc, d.Spec())
}
func TestDocument(t *testing.T) {
document, err := Embedded(petStoreJSON, petStoreJSON)
require.NoError(t, err)
require.EqualT(t, "petstore.swagger.wordnik.com", document.Host())
require.JSONMarshalAsT(t, petStoreJSON, document.OrigSpec())
require.JSONMarshalAsT(t, petStoreJSON, document.Pristine().Spec())
spc := document.Spec()
spc.Definitions = nil
before := document.Spec()
require.Empty(t, before.Definitions)
reset := document.ResetDefinitions()
require.JSONMarshalAsT(t, petStoreJSON, reset.Spec())
}
func TestSpecCircular(t *testing.T) {
swaggerFile := "fixtures/json/resources/pathLoaderIssue.json"
document, err := Spec(swaggerFile)
require.NoError(t, err)
require.NotNil(t, document)
}
func TestIssueSpec145(t *testing.T) {
t.Run("with remote $ref", func(t *testing.T) {
docPath := filepath.Join("fixtures", "bugs", "145", "Program Files (x86)", "AppName", "todos.json")
t.Run("with Spec loader", func(t *testing.T) {
document, err := Spec(docPath)
require.NoError(t, err)
require.NotNil(t, document)
_, err = document.Expanded()
require.NoError(t, err)
})
t.Run("with JSONSpec loader", func(t *testing.T) {
document, err := JSONSpec(docPath)
require.NoError(t, err)
require.NotNil(t, document)
_, err = document.Expanded()
require.NoError(t, err)
})
})
t.Run("with self-contained root", func(t *testing.T) {
docPath := filepath.Join("fixtures", "bugs", "145", "Program Files (x86)", "AppName", "todos-expanded.json")
t.Run("with Spec loader", func(t *testing.T) {
document, err := Spec(docPath)
require.NoError(t, err)
require.NotNil(t, document)
require.EqualT(t, docPath, document.SpecFilePath())
expanded, err := document.Expanded()
require.NoError(t, err)
require.EqualT(t, docPath, expanded.SpecFilePath())
})
t.Run("with JSONSpec loader", func(t *testing.T) {
document, err := JSONSpec(docPath)
require.NoError(t, err)
require.NotNil(t, document)
_, err = document.Expanded()
require.NoError(t, err)
t.Run("with Pristine", func(t *testing.T) {
pristine := document.Pristine()
require.EqualT(t, document.SpecFilePath(), pristine.SpecFilePath())
})
})
})
}