-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
222 lines (202 loc) · 6.99 KB
/
Copy pathconfig_test.go
File metadata and controls
222 lines (202 loc) · 6.99 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
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v3"
)
func TestConfigMerging(t *testing.T) {
app := fiber.New()
t.Run("Default config when no config provided", func(t *testing.T) {
oapi := New(app)
config := oapi.Config()
// Should use defaults
if !config.EnableValidation {
t.Error("Expected EnableValidation to be true by default")
}
if !config.EnableOpenAPIDocs {
t.Error("Expected EnableOpenAPIDocs to be true by default")
}
if config.EnableAuthorization {
t.Error("Expected EnableAuthorization to be false by default")
}
})
t.Run("Empty config preserves defaults", func(t *testing.T) {
oapi := New(app, Config{})
config := oapi.Config()
// Should preserve defaults when empty config is provided
if !config.EnableValidation {
t.Error("Expected EnableValidation to remain true with empty config")
}
if !config.EnableOpenAPIDocs {
t.Error("Expected EnableOpenAPIDocs to remain true with empty config")
}
if config.EnableAuthorization {
t.Error("Expected EnableAuthorization to remain false with empty config")
}
})
t.Run("Explicit config overrides defaults", func(t *testing.T) {
// Provide explicit config with some non-default values
oapi := New(app, Config{
EnableValidation: false, // Explicitly set to false
EnableOpenAPIDocs: false, // Explicitly set to false
EnableAuthorization: true, // This indicates explicit configuration
})
config := oapi.Config()
// Should respect explicit values
if config.EnableValidation {
t.Error("Expected EnableValidation to be false when explicitly set")
}
if config.EnableOpenAPIDocs {
t.Error("Expected EnableOpenAPIDocs to be false when explicitly set")
}
if !config.EnableAuthorization {
t.Error("Expected EnableAuthorization to be true when explicitly set")
}
})
t.Run("Partial explicit config", func(t *testing.T) {
// Provide config with auth service (indicates explicit configuration)
authService := &struct{ AuthorizationService }{}
oapi := New(app, Config{
EnableValidation: false, // Should be respected
EnableOpenAPIDocs: true, // Should be respected
AuthService: authService,
})
config := oapi.Config()
// Should respect explicit values when auth service is provided
if config.EnableValidation {
t.Error("Expected EnableValidation to be false when explicitly configured")
}
if !config.EnableOpenAPIDocs {
t.Error("Expected EnableOpenAPIDocs to be true when explicitly configured")
}
if config.AuthService == nil {
t.Error("Expected AuthService to be set")
}
})
}
func TestOpenAPIInfoConfig(t *testing.T) {
t.Run("Defaults populate info block", func(t *testing.T) {
oapi := New(fiber.New())
cfg := oapi.Config()
if cfg.OpenAPITitle != "Fiber OpenAPI" {
t.Errorf("default OpenAPITitle = %q, want %q", cfg.OpenAPITitle, "Fiber OpenAPI")
}
if cfg.OpenAPIDescription != "API documentation generated by fiber-oapi" {
t.Errorf("default OpenAPIDescription = %q", cfg.OpenAPIDescription)
}
if cfg.OpenAPIVersion != "1.0.0" {
t.Errorf("default OpenAPIVersion = %q, want %q", cfg.OpenAPIVersion, "1.0.0")
}
spec := oapi.GenerateOpenAPISpec()
info := spec["info"].(map[string]interface{})
if info["title"] != "Fiber OpenAPI" {
t.Errorf("spec title = %v", info["title"])
}
if info["description"] != "API documentation generated by fiber-oapi" {
t.Errorf("spec description = %v", info["description"])
}
if info["version"] != "1.0.0" {
t.Errorf("spec version = %v", info["version"])
}
})
t.Run("Custom title/description/version override defaults", func(t *testing.T) {
oapi := New(fiber.New(), Config{
OpenAPITitle: "My Service",
OpenAPIDescription: "Public REST API",
OpenAPIVersion: "2024.10.1",
})
spec := oapi.GenerateOpenAPISpec()
info := spec["info"].(map[string]interface{})
if info["title"] != "My Service" {
t.Errorf("title = %v, want %q", info["title"], "My Service")
}
if info["description"] != "Public REST API" {
t.Errorf("description = %v", info["description"])
}
if info["version"] != "2024.10.1" {
t.Errorf("version = %v, want %q", info["version"], "2024.10.1")
}
})
t.Run("Partial override preserves other defaults", func(t *testing.T) {
oapi := New(fiber.New(), Config{
OpenAPITitle: "Only Title Set",
})
cfg := oapi.Config()
if cfg.OpenAPITitle != "Only Title Set" {
t.Errorf("title not overridden: %q", cfg.OpenAPITitle)
}
if cfg.OpenAPIDescription != "API documentation generated by fiber-oapi" {
t.Errorf("description should keep default, got %q", cfg.OpenAPIDescription)
}
if cfg.OpenAPIVersion != "1.0.0" {
t.Errorf("version should keep default, got %q", cfg.OpenAPIVersion)
}
})
t.Run("Info field + explicit disable + core signal propagates booleans", func(t *testing.T) {
// With a core signal (AuthService here) booleans from `provided` are honored,
// so the explicit EnableOpenAPIDocs:false actually disables auto docs even
// though the user also customized the title.
authService := &struct{ AuthorizationService }{}
oapi := New(fiber.New(), Config{
OpenAPITitle: "Custom",
EnableOpenAPIDocs: false,
AuthService: authService,
})
cfg := oapi.Config()
if cfg.EnableOpenAPIDocs {
t.Error("EnableOpenAPIDocs should be false when explicitly disabled alongside a core signal")
}
if cfg.OpenAPITitle != "Custom" {
t.Errorf("title = %q", cfg.OpenAPITitle)
}
})
t.Run("Info-only config keeps boolean defaults", func(t *testing.T) {
// Without any core signal, info-only customization must not silently flip
// EnableValidation / EnableOpenAPIDocs to false via Go's zero values.
oapi := New(fiber.New(), Config{OpenAPITitle: "Just Title"})
cfg := oapi.Config()
if !cfg.EnableValidation {
t.Error("EnableValidation should stay true when only cosmetic fields are set")
}
if !cfg.EnableOpenAPIDocs {
t.Error("EnableOpenAPIDocs should stay true when only cosmetic fields are set")
}
})
t.Run("Auto-served /openapi.json reflects custom info", func(t *testing.T) {
app := fiber.New()
New(app, Config{
OpenAPITitle: "Auto Served",
OpenAPIDescription: "Served via auto docs route",
OpenAPIVersion: "9.9.9",
})
req := httptest.NewRequest("GET", "/openapi.json", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Test error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("status = %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read response body: %v", err)
}
var spec map[string]interface{}
if err := json.Unmarshal(body, &spec); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
info := spec["info"].(map[string]interface{})
if info["title"] != "Auto Served" {
t.Errorf("title = %v", info["title"])
}
if info["description"] != "Served via auto docs route" {
t.Errorf("description = %v", info["description"])
}
if info["version"] != "9.9.9" {
t.Errorf("version = %v", info["version"])
}
})
}