-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute_test.go
More file actions
150 lines (117 loc) · 3.77 KB
/
route_test.go
File metadata and controls
150 lines (117 loc) · 3.77 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
package api_test
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/bjaus/api"
)
func TestRouteOptions_applied_via_registration(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("Options Test"))
type Resp struct {
ID string `json:"id"`
}
api.Get(r, "/items/{id}", func(_ context.Context, _ *api.Void) (*Resp, error) {
return &Resp{ID: "1"}, nil
},
api.WithStatus(http.StatusCreated),
api.WithSummary("Get an item"),
api.WithDescription("Fetches a single item by ID"),
api.WithTags("items", "read"),
api.WithDeprecated(),
api.WithError(api.WithErrors(api.CodeNotFound, api.CodeConflict)),
api.WithOperationID("getItemById"),
api.WithExtension("x-custom", "value"),
)
spec := r.Spec()
path, ok := spec.Paths["/items/{id}"]
require.True(t, ok)
op := path["get"]
assert.Equal(t, "Get an item", op.Summary)
assert.Equal(t, "Fetches a single item by ID", op.Description)
assert.Contains(t, op.Tags, "items")
assert.Contains(t, op.Tags, "read")
assert.True(t, op.Deprecated)
assert.Equal(t, "getItemById", op.OperationID)
assert.Contains(t, op.Extensions, "x-custom")
assert.Equal(t, "value", op.Extensions["x-custom"])
// Verify errors are reflected in the spec.
assert.Contains(t, op.Responses, "404")
assert.Contains(t, op.Responses, "409")
}
func TestWithLink(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("Link Test"))
type Resp struct {
ID string `json:"id"`
}
api.Get(r, "/users/{id}", func(_ context.Context, _ *api.Void) (*Resp, error) {
return &Resp{ID: "123"}, nil
}, api.WithLink("GetUser", api.Link{
OperationID: "getUserById",
Description: "Fetch the created user",
Parameters: map[string]any{"id": "$response.body#/id"},
}))
spec := r.Spec()
path, ok := spec.Paths["/users/{id}"]
require.True(t, ok, "path /users/{id} should exist")
op, ok := path["get"]
require.True(t, ok, "GET operation should exist")
require.Contains(t, op.Responses, "200")
resp200 := op.Responses["200"]
require.NotNil(t, resp200.Links)
require.Contains(t, resp200.Links, "GetUser")
assert.Equal(t, "getUserById", resp200.Links["GetUser"].OperationID)
assert.Equal(t, "Fetch the created user", resp200.Links["GetUser"].Description)
}
func TestWithCallback(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("Callback Test"))
type Resp struct {
ID string `json:"id"`
}
api.Post(r, "/subscribe", func(_ context.Context, _ *api.Void) (*Resp, error) {
return &Resp{ID: "sub-1"}, nil
}, api.WithCallback("onEvent", map[string]api.PathItem{
"{$request.body#/callbackUrl}": {
"post": api.Operation{
Summary: "Event callback",
},
},
}))
spec := r.Spec()
path, ok := spec.Paths["/subscribe"]
require.True(t, ok, "path /subscribe should exist")
op, ok := path["post"]
require.True(t, ok, "POST operation should exist")
require.NotNil(t, op.Callbacks)
require.Contains(t, op.Callbacks, "onEvent")
cbPaths := op.Callbacks["onEvent"]
require.Contains(t, cbPaths, "{$request.body#/callbackUrl}")
}
func TestWithDescription_in_spec(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("Desc Test"))
api.Get(r, "/health", func(_ context.Context, _ *api.Void) (*api.Void, error) {
return &api.Void{}, nil
}, api.WithDescription("Health check endpoint"))
spec := r.Spec()
path, ok := spec.Paths["/health"]
require.True(t, ok)
op := path["get"]
assert.Equal(t, "Health check endpoint", op.Description)
}
func TestWithDescription_absent(t *testing.T) {
t.Parallel()
r := api.New(api.WithTitle("No Desc"))
api.Get(r, "/ping", func(_ context.Context, _ *api.Void) (*api.Void, error) {
return &api.Void{}, nil
})
spec := r.Spec()
path, ok := spec.Paths["/ping"]
require.True(t, ok)
op := path["get"]
assert.Empty(t, op.Description)
}