-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.go
More file actions
130 lines (122 loc) · 3.43 KB
/
Copy pathwebhook_test.go
File metadata and controls
130 lines (122 loc) · 3.43 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
package stdocs
import (
"net/http"
"strings"
"testing"
"github.com/FumingPower3925/stdocs/internal/schema"
)
func TestWithWebhooks_3_1(t *testing.T) {
type UserPayload struct {
ID string `json:"id"`
Name string `json:"name"`
}
hook := Webhook{
Method: "POST",
Summary: "New user created",
Description: "Fired when a new user is created",
OperationID: "newUserWebhook",
RequestBody: &RequestBody{
Schema: mustSchema(t, UserPayload{}),
},
Responses: map[string]*Response{
"200": {Description: "OK"},
},
}
m := New(
WithTitle("T"),
WithVersion(OpenAPI31),
WithWebhooks(map[string]Webhook{"newUser": hook}),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
wh := jget(t, doc, "webhooks", "newUser", "post").(map[string]any)
if wh["summary"] != "New user created" {
t.Errorf("summary = %v", wh["summary"])
}
if wh["operationId"] != "newUserWebhook" {
t.Errorf("operationId = %v", wh["operationId"])
}
// Responses should be present.
resp := jget(t, wh, "responses", "200").(map[string]any)
if resp["description"] != "OK" {
t.Errorf("description = %v", resp["description"])
}
}
func TestWithWebhooks_3_0_3_Ignored(t *testing.T) {
// Webhooks are 3.1-only; on 3.0.x they should be silently
// ignored without error.
m := New(
WithTitle("T"),
WithVersion(OpenAPI30),
WithWebhooks(map[string]Webhook{
"newUser": {Method: "POST", Summary: "x"},
}),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
if _, has := doc["webhooks"]; has {
t.Errorf("3.0.x should not include webhooks, got %v", doc["webhooks"])
}
}
func TestWithWebhooks_Multiple(t *testing.T) {
m := New(
WithTitle("T"),
WithVersion(OpenAPI31),
WithWebhooks(map[string]Webhook{
"newUser": {Method: "POST", Summary: "new user"},
"deletedUser": {Method: "POST", Summary: "user deleted"},
}),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
wh := jget(t, doc, "webhooks").(map[string]any)
if _, ok := wh["newUser"]; !ok {
t.Errorf("newUser missing")
}
if _, ok := wh["deletedUser"]; !ok {
t.Errorf("deletedUser missing")
}
}
func TestWithWebhooks_Default200WhenNoResponses(t *testing.T) {
m := New(
WithTitle("T"),
WithVersion(OpenAPI31),
WithWebhooks(map[string]Webhook{
"x": {Method: "POST", Summary: "x"},
}),
)
m.HandleFunc("GET /x", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
doc := jx(t, b)
wh := jget(t, doc, "webhooks", "x", "post").(map[string]any)
// Should auto-add a 200 response.
if _, has := wh["responses"]; !has {
t.Errorf("responses missing: %v", wh)
}
}
func TestWithWebhooks_MethodCase(t *testing.T) {
// Methods should be lower-cased in the output.
m := New(
WithTitle("T"),
WithVersion(OpenAPI31),
WithWebhooks(map[string]Webhook{
"x": {Method: "POST", Summary: "x"},
}),
)
m.HandleFunc("GET /y", func(w http.ResponseWriter, r *http.Request) {})
b, _ := m.JSON()
// Just check that the JSON has "post" as the key, not "POST".
if !strings.Contains(string(b), `"post":{`) {
t.Errorf("webhook method should be lowercased: %s", b)
}
}
// mustSchema is a small helper to build a Schema from a Go value for
// the Webhook tests, avoiding a full ReflectSchema call.
func mustSchema(t *testing.T, v any) *schema.Schema {
t.Helper()
s, _ := schema.ReflectSchema(v)
return s
}