-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_test.go
192 lines (176 loc) · 5.18 KB
/
load_test.go
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
package openapi_test
import (
"context"
"embed"
"fmt"
"io"
"io/fs"
"testing"
"github.com/Masterminds/semver"
"github.com/chanced/openapi"
"github.com/chanced/transcode"
"github.com/chanced/uri"
)
//go:embed testdata
var testdata embed.FS
func TestLoadRefComponent(t *testing.T) {
f, err := testdata.Open("testdata/documents/comprefs.yaml")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
loadfn := func(ctx context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
b, err := io.ReadAll(f)
if err != nil {
return 0, nil, err
}
return openapi.KindDocument, b, nil
}
doc, err := openapi.Load(ctx, "testdata/documents/comprefs.yaml", NoopValidator{}, loadfn)
if err != nil {
t.Error(err)
}
if doc == nil {
t.Errorf("failed to load document")
}
// litter.Dump(doc)
if doc.Components.Responses.Get("Referenced").Object.Description != "/components/responses/Referenced" {
t.Errorf("expected %q got %q", "/components/responses/Referenced", doc.Components.Responses.Get("Referenced").Object.Description)
}
refpath := doc.Paths.Get("/ref")
if refpath.Post.Responses.Get("200").Object.Description != "/components/responses/Referenced" {
t.Errorf("expected %q got %q", "/components/responses/Referenced", doc.Paths.Get("/refs").Post.Responses.Get("200").Object.Description)
}
rb := doc.Components.RequestBodies.Get("Referenced")
if rb.Object.Description != "/components/requestBodies/Referenced" {
t.Errorf("expected requestBody to have description of %q, got %q", "/components/requestBodies/Referenced", rb.Object.Description)
}
rbr := refpath.Post.RequestBody.Object
if rbr.Description != rb.Object.Description {
t.Errorf("expected requestBody to have description of %q, got %q", rb.Object.Description, rbr.Description)
}
}
func TestLoad_Local(t *testing.T) {
ctx := context.Background()
dir, err := fs.Sub(testdata, "testdata")
if err != nil {
t.Fatal(err)
}
f, err := dir.Open("documents/petstore.yaml")
if err != nil {
t.Fatal(err)
}
loadfn := func(ctx context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
b, err := io.ReadAll(f)
// fmt.Println(string(b))
if err != nil {
return 0, nil, err
}
return openapi.KindDocument, b, nil
}
doc, err := openapi.Load(ctx, "https://documents/petstore.yaml", NoopValidator{}, loadfn)
if err != nil {
t.Error(err)
}
if doc == nil {
t.Errorf("failed to load document")
}
}
func TestLoad_Remote(t *testing.T) {
ctx := context.Background()
dir, err := fs.Sub(testdata, "testdata")
if err != nil {
t.Fatal(err)
}
loadfn := func(ctx context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
var f fs.File
var err error
if uri.String() == "https://example.com/schemas/string-map" {
f, err = dir.Open("schemas/string-map.yaml")
} else {
f, err = dir.Open("documents/remote-refs.yaml")
}
if err != nil {
return 0, nil, err
}
b, err := io.ReadAll(f)
if err != nil {
return 0, nil, err
}
fmt.Println("kind", kind)
return kind, b, nil
}
// testing loading remote refs
doc, err := openapi.Load(ctx, "testdata/documents/remote-refs.yaml", NoopValidator{}, loadfn)
if err != nil {
t.Error(err)
}
if doc == nil {
t.Errorf("failed to load document")
}
}
func TestDynamicRefs(t *testing.T) {
f, err := testdata.Open("testdata/documents/dynamic-refs.yaml")
if err != nil {
t.Fatal(err)
}
listOfT, err := testdata.Open("testdata/schemas/list-of-t.json")
if err != nil {
t.Fatal(err)
}
listOfStrings, err := testdata.Open("testdata/schemas/list-of-strings.json")
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
doc, err := openapi.Load(ctx, "testdata/documents/dynamic-refs.yaml", NoopValidator{}, func(ctx context.Context, uri uri.URI, kind openapi.Kind) (openapi.Kind, []byte, error) {
switch uri.String() {
case "https://json-schema.blog/list-of-t":
d, err := io.ReadAll(listOfT)
return openapi.KindSchema, d, err
case "https://json-schema.blog/list-of-strings":
d, err := io.ReadAll(listOfStrings)
return openapi.KindSchema, d, err
case "testdata/documents/dynamic-refs.yaml":
d, err := io.ReadAll(f)
return openapi.KindDocument, d, err
default:
return 0, nil, fmt.Errorf("uknown uri %q", uri)
}
})
if err != nil {
t.Error(err)
}
// litter.Dump(doc)
los := doc.Components.Schemas.Get("ListOfStrings")
if los.Ref.Resolved.Ref.Resolved == nil {
t.Error("expected /list-of-t to be resolved")
}
}
type NoopValidator struct{}
func (NoopValidator) Validate(data []byte, resource uri.URI, kind openapi.Kind, openapi semver.Version, jsonschema uri.URI) error {
return nil
}
func TestTryGetOpenAPIVersion(t *testing.T) {
f, err := testdata.Open("testdata/documents/petstore.yaml")
if err != nil {
t.Fatal(err)
}
d, _ := io.ReadAll(f)
d, err = transcode.JSONFromYAML(d)
if err != nil {
t.Errorf("failed to transcode data")
}
if len(d) == 0 {
t.Fatal("file was empty")
}
vstr, ok := openapi.TryGetOpenAPIVersion(d)
if !ok {
t.Error("failed to get openapi")
}
if vstr != "3.1.0" {
t.Errorf("expected 3.1.0 got %q", vstr)
}
}
func (NoopValidator) ValidateDocument(document *openapi.Document) error { return nil }
var _ openapi.Validator = (*NoopValidator)(nil)