-
Notifications
You must be signed in to change notification settings - Fork 0
/
swagger-ui_test.go
183 lines (176 loc) · 5 KB
/
swagger-ui_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
package swaggerui_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"gotest.tools/v3/assert"
swaggerui "go.skymeyer.dev/swagger-ui-bindata"
)
var (
mainJSCallRegion = "// Begin Swagger UI call region"
mainDefaultSpec = "https://petstore.swagger.io/v2/swagger.json"
mainEmbeddedSpec = "./spec.json"
distSwaggerUICSS = "sourceMappingURL=swagger-ui.css.map"
distSwaggerUIBundleVersion = `Vr="4.5.0"`
distSwaggerUIBundleJS = "For license information please see swagger-ui-bundle.js.LICENSE.txt"
distSwaggerUIStandalonePresetJS = "For license information please see swagger-ui-standalone-preset.js.LICENSE.txt"
)
func TestExpectedFiles(t *testing.T) {
for _, d := range []struct {
test string // test description
prefix string // site/mux prefix
url string // URL to call
status int // expected HTTP status code
contains string // expected returned content
opts []swaggerui.Option
}{
{
test: "Test version",
prefix: "/",
url: "/dist/swagger-ui-bundle.js",
status: http.StatusOK,
contains: distSwaggerUIBundleVersion,
opts: []swaggerui.Option{},
},
{
test: "Test main index page with defaults",
prefix: "/",
url: "/",
status: http.StatusOK,
contains: mainJSCallRegion,
opts: []swaggerui.Option{},
},
{
test: "Test main index page default spec",
prefix: "/",
url: "/",
status: http.StatusOK,
contains: mainDefaultSpec,
opts: []swaggerui.Option{},
},
{
test: "Test /dist/swagger-ui.css with defaults",
prefix: "/",
url: "/dist/swagger-ui.css",
status: http.StatusOK,
contains: distSwaggerUICSS,
opts: []swaggerui.Option{},
},
{
test: "Test /dist/swagger-ui-bundle.js with defaults",
prefix: "/",
url: "/dist/swagger-ui-bundle.js",
status: http.StatusOK,
contains: distSwaggerUIBundleJS,
opts: []swaggerui.Option{},
},
{
test: "Test /dist/swagger-ui-standalone-preset.js with defaults",
prefix: "/",
url: "/dist/swagger-ui-standalone-preset.js",
status: http.StatusOK,
contains: distSwaggerUIStandalonePresetJS,
opts: []swaggerui.Option{},
},
{
test: "Test main index page with prefix",
prefix: "/foo/",
url: "/foo/",
status: http.StatusOK,
contains: mainJSCallRegion,
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/"),
},
},
{
test: "Test main index page with prefix (redirect)",
prefix: "/foo/bar/",
url: "/foo/bar",
status: http.StatusMovedPermanently,
contains: "",
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/bar/"),
},
},
{
test: "Test /dist/swagger-ui.css with prefix",
prefix: "/foo/bar/",
url: "/foo/bar/dist/swagger-ui.css",
status: http.StatusOK,
contains: distSwaggerUICSS,
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/bar/"),
},
},
{
test: "Test /dist/swagger-ui-bundle.js with prefix",
prefix: "/foo/bar/",
url: "/foo/bar/dist/swagger-ui-bundle.js",
status: http.StatusOK,
contains: distSwaggerUIBundleJS,
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/bar/"),
},
},
{
test: "Test /dist/swagger-ui-standalone-preset.js with prefix",
prefix: "/foo/bar/",
url: "/foo/bar/dist/swagger-ui-standalone-preset.js",
status: http.StatusOK,
contains: distSwaggerUIStandalonePresetJS,
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/bar/"),
},
},
{
test: "Test main index page with spec URL",
prefix: "/",
url: "/",
status: http.StatusOK,
contains: "http://foo.bar.com/openapi.json",
opts: []swaggerui.Option{
swaggerui.WithSpecURL("http://foo.bar.com/openapi.json"),
},
},
{
test: "Test main index page with embedded spec",
prefix: "/",
url: "/",
status: http.StatusOK,
contains: mainEmbeddedSpec,
opts: []swaggerui.Option{
swaggerui.WithEmbeddedSpec([]byte("fakespec")),
},
},
{
test: "Test main index page with embedded spec (embedded spec content)",
prefix: "/",
url: "/spec.json",
status: http.StatusOK,
contains: "fakespec",
opts: []swaggerui.Option{
swaggerui.WithEmbeddedSpec([]byte("fakespec")),
},
},
{
test: "Test main index page with embedded spec and prefix (embedded spec content)",
prefix: "/foo/bar/",
url: "/foo/bar/spec.json",
status: http.StatusOK,
contains: "fakespec",
opts: []swaggerui.Option{
swaggerui.WithPrefix("/foo/bar/"),
swaggerui.WithEmbeddedSpec([]byte("fakespec")),
},
},
} {
r := httptest.NewRequest(http.MethodGet, d.url, nil)
w := httptest.NewRecorder()
mux := http.NewServeMux()
mux.Handle(d.prefix, swaggerui.New(d.opts...).Handler())
mux.ServeHTTP(w, r)
assert.Equal(t, d.status, w.Code, d.test)
assert.Assert(t, strings.Contains(w.Body.String(), d.contains), d.test)
}
}