-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathexamples_test.go
More file actions
189 lines (155 loc) · 4.1 KB
/
examples_test.go
File metadata and controls
189 lines (155 loc) · 4.1 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
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package loads_test
import (
"embed"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"path"
"path/filepath"
"github.com/go-openapi/loads"
"github.com/go-openapi/swag/loading"
)
//go:embed fixtures
var embeddedFixtures embed.FS
// Example with default loaders defined at the package level.
func ExampleSpec_file() {
path := "fixtures/yaml/swagger/spec.yml"
doc, err := loads.Spec(path)
if err != nil {
fmt.Println("Could not load this spec")
return
}
fmt.Printf("Spec loaded: %q\n", doc.Host())
// Output: Spec loaded: "api.example.com"
}
// Example with custom loaders passed as options.
func ExampleLoaderOption() {
path := "fixtures/yaml/swagger/spec.yml"
// a simpler version of loads.JSONDoc
jsonLoader := loads.NewDocLoaderWithMatch(
func(pth string, _ ...loading.Option) (json.RawMessage, error) {
buf, err := os.ReadFile(pth)
return json.RawMessage(buf), err
},
func(pth string) bool {
return filepath.Ext(pth) == ".json"
},
)
// equivalent to the default loader at the package level, which does:
//
// loads.AddLoader(loading.YAMLMatcher, loading.YAMLDoc)
yamlLoader := loads.NewDocLoaderWithMatch(
loading.YAMLDoc,
func(pth string) bool {
return filepath.Ext(pth) == ".yml"
},
)
doc, err := loads.Spec(path, loads.WithDocLoaderMatches(jsonLoader, yamlLoader))
if err != nil {
fmt.Println("Could not load this spec")
return
}
fmt.Printf("Spec loaded: %q\n", doc.Host())
// Output: Spec loaded: "api.example.com"
}
// Loads a JSON document from http, with a custom header.
func ExampleJSONSpec_http_custom_header() {
ts := serveSomeJSONDocument()
defer ts.Close()
doc, err := loads.JSONSpec(ts.URL,
loads.WithLoadingOptions(loading.WithCustomHeaders(map[string]string{
"X-API-key": "my-api-key",
})))
if err != nil {
panic(err)
}
fmt.Println(doc.Host())
fmt.Println(doc.Version())
// Output:
// api.example.com
// 2.0
}
// Loads a YAML document and get the deserialized [spec.Swagger] specification.
func ExampleSpec_http_yaml() {
ts := serveSomeYAMLDocument()
defer ts.Close()
// loads a YAML spec from a http URL
doc, err := loads.Spec(ts.URL)
if err != nil {
panic(err)
}
fmt.Println(doc.Host())
fmt.Println(doc.Version())
spec := doc.Spec()
if spec == nil {
panic("spec should not be nil")
}
// Output:
// api.example.com
// 2.0
}
// Loads a JSON document and get the deserialized [spec.Swagger] specification.
func ExampleSpec_http_json() {
ts := serveSomeJSONDocument()
defer ts.Close()
// loads a YAML spec from a http URL
doc, err := loads.Spec(ts.URL)
if err != nil {
panic(err)
}
fmt.Println(doc.Host())
fmt.Println(doc.Version())
spec := doc.Spec()
if spec == nil {
panic("spec should not be nil")
}
// Output:
// api.example.com
// 2.0
}
// Loads a JSON document from the embedded file system and get the deserialized [spec.Swagger] specification.
func ExampleSpec_embedded_yaml() {
// loads a YAML spec from a file on an embedded file system
doc, err := loads.Spec(
path.Join("fixtures", "yaml", "swagger", "spec.yml"), // [embed.FS] sep is "/" even on windows
loads.WithLoadingOptions(
loading.WithFS(embeddedFixtures),
))
if err != nil {
panic(err)
}
fmt.Println(doc.Host())
fmt.Println(doc.Version())
spec := doc.Spec()
if spec == nil {
panic("spec should not be nil")
}
// Output:
// api.example.com
// 2.0
}
func serveSomeYAMLDocument() *httptest.Server {
source, err := os.Open(filepath.Join("fixtures", "yaml", "swagger", "spec.yml"))
if err != nil {
panic(err)
}
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = io.Copy(rw, source)
}))
}
func serveSomeJSONDocument() *httptest.Server {
source, err := os.Open(filepath.Join("fixtures", "json", "resources", "pathLoaderIssue.json"))
if err != nil {
panic(err)
}
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = io.Copy(rw, source)
}))
}