-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoverage_test.go
More file actions
181 lines (149 loc) · 5.75 KB
/
Copy pathcoverage_test.go
File metadata and controls
181 lines (149 loc) · 5.75 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
package fiberoapi
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
// Thin helper tests targeting code paths previously at 0% coverage so the
// project stays comfortably above 80% as more features ship.
type covOut struct {
Message string `json:"message"`
}
func TestHeadHelperRoutesAndSurfacesInSpec(t *testing.T) {
app := fiber.New()
oapi := New(app)
Head(oapi, "/ping", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "ok"}, struct{}{}
}, OpenAPIOptions{OperationID: "ping"})
resp, err := app.Test(httptest.NewRequest("HEAD", "/ping", nil))
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
spec := oapi.GenerateOpenAPISpec()
pingPath := spec["paths"].(map[string]any)["/ping"].(map[string]any)
_, hasHead := pingPath["head"]
assert.True(t, hasHead, "HEAD operation should appear in the spec")
}
func TestPatchHelperRoutesAndSurfacesInSpec(t *testing.T) {
app := fiber.New()
oapi := New(app)
type patchInput struct {
ID string `uri:"id" validate:"required"`
}
Patch(oapi, "/users/:id", func(c fiber.Ctx, in patchInput) (covOut, struct{}) {
return covOut{Message: "patched " + in.ID}, struct{}{}
}, OpenAPIOptions{OperationID: "patchUser"})
resp, err := app.Test(httptest.NewRequest("PATCH", "/users/42", nil))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
var out covOut
require.NoError(t, json.Unmarshal(body, &out))
assert.Equal(t, "patched 42", out.Message)
spec := oapi.GenerateOpenAPISpec()
usersPath := spec["paths"].(map[string]any)["/users/{id}"].(map[string]any)
_, hasPatch := usersPath["patch"]
assert.True(t, hasPatch, "PATCH operation should appear in the spec")
}
func TestGroupPackageLevelHelper_RouterApp(t *testing.T) {
// The free function Group(router, ...) dispatches to either *OApiApp or
// *OApiGroup. Cover the *OApiApp branch.
app := fiber.New()
oapi := New(app)
v1 := Group(oapi, "/api/v1")
Get(v1, "/health", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "ok"}, struct{}{}
}, OpenAPIOptions{OperationID: "health"})
resp, err := app.Test(httptest.NewRequest("GET", "/api/v1/health", nil))
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}
func TestGroupPackageLevelHelper_RouterNestedGroup(t *testing.T) {
// Cover the *OApiGroup branch of the free Group helper (sub-group).
app := fiber.New()
oapi := New(app)
v1 := Group(oapi, "/api/v1")
users := Group(v1, "/users")
Get(users, "/me", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "me"}, struct{}{}
}, OpenAPIOptions{OperationID: "me"})
resp, err := app.Test(httptest.NewRequest("GET", "/api/v1/users/me", nil))
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
}
func TestGroupPackageLevelHelper_UnsupportedRouterPanics(t *testing.T) {
defer func() {
r := recover()
assert.NotNil(t, r, "Group with unsupported router type should panic")
}()
type fake struct{ OApiRouter }
Group(fake{}, "/x")
}
func TestOApiAppUse_PassesMiddlewareToFiber(t *testing.T) {
// Verify the OApiApp.Use thin passthrough actually plumbs the middleware
// down to fiber.App.Use so registered handlers run on every request.
app := fiber.New()
oapi := New(app)
calls := 0
oapi.Use(func(c fiber.Ctx) error {
calls++
return c.Next()
})
Get(oapi, "/echo", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "ok"}, struct{}{}
}, OpenAPIOptions{OperationID: "echo"})
resp, err := app.Test(httptest.NewRequest("GET", "/echo", nil))
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, 1, calls, "middleware registered via OApiApp.Use must run")
}
func TestOApiGroupUse_PassesMiddlewareToFiber(t *testing.T) {
// Same coverage for OApiGroup.Use — the group-scoped middleware should run
// only for routes registered under that group.
app := fiber.New()
oapi := New(app)
api := oapi.Group("/api")
groupCalls := 0
api.Use(func(c fiber.Ctx) error {
groupCalls++
return c.Next()
})
Get(api, "/inside", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "in"}, struct{}{}
}, OpenAPIOptions{OperationID: "inside"})
Get(oapi, "/outside", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "out"}, struct{}{}
}, OpenAPIOptions{OperationID: "outside"})
respIn, err := app.Test(httptest.NewRequest("GET", "/api/inside", nil))
require.NoError(t, err)
assert.Equal(t, 200, respIn.StatusCode)
assert.Equal(t, 1, groupCalls, "group middleware must run for /api/* routes")
respOut, err := app.Test(httptest.NewRequest("GET", "/outside", nil))
require.NoError(t, err)
assert.Equal(t, 200, respOut.StatusCode)
assert.Equal(t, 1, groupCalls, "group middleware must NOT run for routes outside the group")
}
func TestOpenAPIYamlEndpoint_ServesYAMLSpec(t *testing.T) {
// Exercise the YAML branch of the auto-registered docs routes.
app := fiber.New()
oapi := New(app)
Get(oapi, "/things", func(c fiber.Ctx, _ struct{}) (covOut, struct{}) {
return covOut{Message: "ok"}, struct{}{}
}, OpenAPIOptions{OperationID: "things"})
resp, err := app.Test(httptest.NewRequest("GET", "/openapi.yaml", nil))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
require.Contains(t, resp.Header.Get("Content-Type"), "yaml")
body, _ := io.ReadAll(resp.Body)
var parsed map[string]any
require.NoError(t, yaml.Unmarshal(body, &parsed), "served YAML must be parseable: %s", body)
assert.Equal(t, "3.0.0", parsed["openapi"])
paths, ok := parsed["paths"].(map[string]any)
require.True(t, ok)
_, hasThings := paths["/things"]
assert.True(t, hasThings, "YAML spec should include registered routes")
}