-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger_test.go
More file actions
284 lines (227 loc) · 7.57 KB
/
swagger_test.go
File metadata and controls
284 lines (227 loc) · 7.57 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package grpckit
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewSwaggerHandler(t *testing.T) {
// Create temp swagger file
tmpDir := t.TempDir()
specPath := filepath.Join(tmpDir, "swagger.json")
validSpec := `{"openapi": "3.0.0", "info": {"title": "Test API", "version": "1.0"}}`
if err := os.WriteFile(specPath, []byte(validSpec), 0644); err != nil {
t.Fatalf("failed to write spec file: %v", err)
}
handler, err := newSwaggerHandler(specPath)
if err != nil {
t.Fatalf("newSwaggerHandler failed: %v", err)
}
if handler == nil {
t.Fatal("expected non-nil handler")
}
if handler.specPath != specPath {
t.Errorf("expected specPath %s, got %s", specPath, handler.specPath)
}
if string(handler.specData) != validSpec {
t.Errorf("expected specData %s, got %s", validSpec, string(handler.specData))
}
}
func TestNewSwaggerHandler_FileNotFound(t *testing.T) {
_, err := newSwaggerHandler("/nonexistent/swagger.json")
if err == nil {
t.Error("expected error for non-existent file")
}
}
func TestNewSwaggerHandler_InvalidJSON(t *testing.T) {
tmpDir := t.TempDir()
specPath := filepath.Join(tmpDir, "swagger.json")
if err := os.WriteFile(specPath, []byte("not valid json"), 0644); err != nil {
t.Fatalf("failed to write spec file: %v", err)
}
_, err := newSwaggerHandler(specPath)
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestNewSwaggerHandlerFromBytes(t *testing.T) {
validSpec := []byte(`{"openapi": "3.0.0"}`)
handler, err := newSwaggerHandlerFromBytes(validSpec)
if err != nil {
t.Fatalf("newSwaggerHandlerFromBytes failed: %v", err)
}
if handler == nil {
t.Fatal("expected non-nil handler")
}
if string(handler.specData) != string(validSpec) {
t.Errorf("expected specData to match input")
}
// specPath should be empty for bytes-based handler
if handler.specPath != "" {
t.Errorf("expected empty specPath, got %s", handler.specPath)
}
}
func TestNewSwaggerHandlerFromBytes_InvalidJSON(t *testing.T) {
_, err := newSwaggerHandlerFromBytes([]byte("not json"))
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestSwaggerHandler_UIHandler(t *testing.T) {
handler, _ := newSwaggerHandlerFromBytes([]byte(`{"openapi": "3.0.0"}`))
uiHandler := handler.UIHandler()
req := httptest.NewRequest(http.MethodGet, "/swagger/", nil)
rec := httptest.NewRecorder()
uiHandler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", rec.Code)
}
if rec.Header().Get("Content-Type") != "text/html; charset=utf-8" {
t.Errorf("expected Content-Type text/html, got %s", rec.Header().Get("Content-Type"))
}
body := rec.Body.String()
if !strings.Contains(body, "swagger-ui") {
t.Error("expected swagger-ui in HTML body")
}
// Template escapes slashes as \/ for JS safety
if !strings.Contains(body, "/swagger/spec.json") && !strings.Contains(body, `\/swagger\/spec.json`) {
t.Error("expected spec URL in HTML body")
}
}
func TestSwaggerHandler_SpecHandler(t *testing.T) {
specData := []byte(`{"openapi": "3.0.0", "info": {"title": "Test"}}`)
handler, _ := newSwaggerHandlerFromBytes(specData)
specHandler := handler.SpecHandler()
req := httptest.NewRequest(http.MethodGet, "/swagger/spec.json", nil)
rec := httptest.NewRecorder()
specHandler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", rec.Code)
}
if rec.Header().Get("Content-Type") != "application/json" {
t.Errorf("expected Content-Type application/json, got %s", rec.Header().Get("Content-Type"))
}
if rec.Body.String() != string(specData) {
t.Errorf("expected spec data in response body")
}
}
func TestRegisterSwaggerEndpoints(t *testing.T) {
tmpDir := t.TempDir()
specPath := filepath.Join(tmpDir, "swagger.json")
if err := os.WriteFile(specPath, []byte(`{"openapi": "3.0.0"}`), 0644); err != nil {
t.Fatalf("failed to write spec file: %v", err)
}
mux := http.NewServeMux()
err := registerSwaggerEndpoints(mux, specPath)
if err != nil {
t.Fatalf("registerSwaggerEndpoints failed: %v", err)
}
// Test /swagger/ (UI)
req := httptest.NewRequest(http.MethodGet, "/swagger/", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("/swagger/ expected status 200, got %d", rec.Code)
}
// Test /swagger/spec.json
req = httptest.NewRequest(http.MethodGet, "/swagger/spec.json", nil)
rec = httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("/swagger/spec.json expected status 200, got %d", rec.Code)
}
}
func TestRegisterSwaggerEndpoints_InvalidFile(t *testing.T) {
mux := http.NewServeMux()
err := registerSwaggerEndpoints(mux, "/nonexistent/swagger.json")
if err == nil {
t.Error("expected error for non-existent file")
}
}
func TestRegisterSwaggerEndpointsFromBytes(t *testing.T) {
mux := http.NewServeMux()
specData := []byte(`{"openapi": "3.0.0"}`)
err := registerSwaggerEndpointsFromBytes(mux, specData)
if err != nil {
t.Fatalf("registerSwaggerEndpointsFromBytes failed: %v", err)
}
// Test /swagger/
req := httptest.NewRequest(http.MethodGet, "/swagger/", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("/swagger/ expected status 200, got %d", rec.Code)
}
}
func TestRegisterSwaggerEndpointsFromBytes_InvalidJSON(t *testing.T) {
mux := http.NewServeMux()
err := registerSwaggerEndpointsFromBytes(mux, []byte("invalid"))
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestRegisterSwaggerHandler_Routing(t *testing.T) {
handler, _ := newSwaggerHandlerFromBytes([]byte(`{"openapi": "3.0.0"}`))
mux := http.NewServeMux()
registerSwaggerHandler(mux, handler)
tests := []struct {
path string
expectedStatus int
description string
}{
{"/swagger/", http.StatusOK, "UI at /swagger/"},
{"/swagger", http.StatusMovedPermanently, "UI at /swagger (redirects to /swagger/)"},
{"/swagger/spec.json", http.StatusOK, "Spec at /swagger/spec.json"},
{"/swagger/unknown", http.StatusNotFound, "404 for unknown paths"},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != tt.expectedStatus {
t.Errorf("%s: expected status %d, got %d", tt.path, tt.expectedStatus, rec.Code)
}
})
}
}
func TestRegisterSwaggerNotFound(t *testing.T) {
mux := http.NewServeMux()
registerSwaggerNotFound(mux)
req := httptest.NewRequest(http.MethodGet, "/swagger/", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Errorf("expected status 404, got %d", rec.Code)
}
body := rec.Body.String()
if !strings.Contains(body, "make swagger") {
t.Error("expected helpful message about running 'make swagger'")
}
}
func TestSwaggerUIHTML_Template(t *testing.T) {
// Verify the template contains expected elements
if !strings.Contains(swaggerUIHTML, "swagger-ui") {
t.Error("template should contain swagger-ui")
}
if !strings.Contains(swaggerUIHTML, "{{.SpecURL}}") {
t.Error("template should contain SpecURL placeholder")
}
if !strings.Contains(swaggerUIHTML, "SwaggerUIBundle") {
t.Error("template should contain SwaggerUIBundle")
}
}
func TestSetSwaggerData(t *testing.T) {
// Save original
original := globalSwaggerData
defer func() {
globalSwaggerData = original
}()
testData := []byte(`{"test": "data"}`)
SetSwaggerData(testData)
if string(globalSwaggerData) != string(testData) {
t.Errorf("expected globalSwaggerData to be set")
}
}