-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs_test.go
More file actions
147 lines (128 loc) · 3.83 KB
/
docs_test.go
File metadata and controls
147 lines (128 loc) · 3.83 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
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"strings"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestSetupDocs_Documentation(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Setup documentation
oapi.SetupDocs()
// Test documentation endpoint
req := httptest.NewRequest("GET", "/docs", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
// Check content type
contentType := resp.Header.Get("Content-Type")
if !strings.Contains(contentType, "text/html") {
t.Errorf("Expected HTML content type, got %s", contentType)
}
// Check HTML content contains Redoc
body, _ := io.ReadAll(resp.Body)
htmlContent := string(body)
if !strings.Contains(htmlContent, "redoc") {
t.Error("Expected HTML to contain Redoc elements")
}
if !strings.Contains(htmlContent, "/openapi.json") {
t.Error("Expected HTML to reference OpenAPI JSON endpoint")
}
if !strings.Contains(htmlContent, "API Documentation") {
t.Error("Expected HTML to contain default title")
}
}
func TestSetupDocs_CustomConfig(t *testing.T) {
app := fiber.New()
oapi := New(app)
// Add a test route
Get(oapi, "/health", func(c *fiber.Ctx, input struct{}) (interface{}, struct{}) {
return map[string]string{"status": "ok"}, struct{}{}
}, OpenAPIOptions{
OperationID: "health-check",
Summary: "Health check",
})
// Setup documentation with custom config
customConfig := DocConfig{
Title: "My Custom API",
Description: "Custom API description",
Version: "2.0.0",
DocsPath: "/api-docs",
JSONPath: "/api-spec.json",
YamlPath: "/api-spec.yaml",
}
oapi.SetupDocs(customConfig)
// Test custom JSON endpoint
req := httptest.NewRequest("GET", "/api-spec.json", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
// Parse the OpenAPI spec
body, _ := io.ReadAll(resp.Body)
var spec map[string]interface{}
if err := json.Unmarshal(body, &spec); err != nil {
t.Fatalf("Failed to parse OpenAPI JSON: %v", err)
}
// Check custom info
if info, ok := spec["info"].(map[string]interface{}); ok {
if info["title"] != "My Custom API" {
t.Errorf("Expected title 'My Custom API', got %v", info["title"])
}
if info["description"] != "Custom API description" {
t.Errorf("Expected custom description, got %v", info["description"])
}
if info["version"] != "2.0.0" {
t.Errorf("Expected version '2.0.0', got %v", info["version"])
}
} else {
t.Error("Expected info section in OpenAPI spec")
}
// Test custom docs endpoint
req2 := httptest.NewRequest("GET", "/api-docs", nil)
resp2, err := app.Test(req2)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if resp2.StatusCode != 200 {
t.Errorf("Expected status 200, got %d", resp2.StatusCode)
}
body2, _ := io.ReadAll(resp2.Body)
htmlContent := string(body2)
if !strings.Contains(htmlContent, "My Custom API") {
t.Error("Expected HTML to contain custom title")
}
if !strings.Contains(htmlContent, "/api-spec.json") {
t.Error("Expected HTML to reference custom JSON endpoint")
}
}
func TestConvertFiberPathToOpenAPI(t *testing.T) {
tests := []struct {
fiberPath string
expectedPath string
}{
{"/users/:id", "/users/{id}"},
{"/users/:userId/posts/:postId", "/users/{userId}/posts/{postId}"},
{"/simple", "/simple"},
{"/mixed/:id/static/:name/end", "/mixed/{id}/static/{name}/end"},
{"/:single", "/{single}"},
{"/", "/"},
}
for _, test := range tests {
result := convertFiberPathToOpenAPI(test.fiberPath)
if result != test.expectedPath {
t.Errorf("convertFiberPathToOpenAPI(%s) = %s; expected %s",
test.fiberPath, result, test.expectedPath)
}
}
}