-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi_test.go
150 lines (115 loc) · 2.52 KB
/
openapi_test.go
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 goapi_test
import (
"context"
"net/http"
"os"
"os/exec"
"reflect"
"strings"
"testing"
"github.com/ysmood/goapi"
"github.com/ysmood/goapi/lib/middlewares"
"github.com/ysmood/goapi/lib/openapi"
"github.com/ysmood/got"
"github.com/ysmood/vary"
)
type Res interface {
goapi.Response
}
var _ = goapi.Interface(new(Res), Res01{}, Res02{})
type Res01 struct {
goapi.StatusOK
Data string
Header struct {
SetCookie string
}
}
type Res02 struct {
goapi.StatusForbidden
Error openapi.Error
}
func (Res02) Description() string {
return "returns 403"
}
type Res03 struct {
goapi.StatusOK
Data string
Meta string
}
type Three struct {
goapi.StatusOK
Data string `response:"direct"`
}
type Four struct {
goapi.StatusOK
Data goapi.DataStream
}
func fnFour() Four {
return Four{}
}
type Five struct {
goapi.StatusOK
Data goapi.DataStream
}
func (Five) ContentType() string {
return "image/png"
}
func TestOpenAPI(t *testing.T) {
g := got.T(t)
r := goapi.New()
tr := g.Serve()
tr.Mux.Handle("/", r.Server())
r.Use(middlewares.Identity)
r.GET("/override", func(w http.ResponseWriter, r *http.Request) {})
r.GET("/one", func(_ context.Context, p struct {
goapi.InURL
ID string `default:"123" description:"id" examples:"[\"456\"]"`
Type *openapi.Code `description:"type code"`
}, h struct {
goapi.InHeader
UA string
}, b struct {
Data string `json:"data"`
}) Res {
return Res01{}
}).OpenAPI(func(doc *openapi.Operation) {
doc.OperationID = "one"
doc.Summary = "test"
doc.Description = "test endpoint"
doc.Tags = []string{"test"}
doc.Security = []map[string][]string{{"auth": {"read"}}}
})
r.GET("/two/{id}", func(struct {
goapi.InURL
ID string
}) Res03 {
return Res03{}
})
r.GET("/three", func() Three {
return Three{}
})
r.POST("/three", func() Three {
return Three{}
})
r.GET("/four", fnFour)
r.GET("/five", func() Five {
return Five{}
})
doc := r.OpenAPI().JSON()
// Ensure you have nodejs installed
{
g.E(os.WriteFile("tmp/openapi.json", []byte(doc), 0666))
out, err := exec.Command("npx", strings.Split("rdme openapi:validate tmp/openapi.json", " ")...).CombinedOutput()
g.Desc("%s", out).Nil(err)
}
g.Snapshot("openapi", g.JSON(doc))
}
func TestAddInterfaces(t *testing.T) {
g := got.T(t)
set := vary.NewInterfaces()
type AddInterfaces interface{}
set.New(new(AddInterfaces))
goapi.AddInterfaces(set)
g.Eq(goapi.Interfaces[vary.ID(reflect.TypeOf(new(AddInterfaces)).Elem())].ID(),
"github.com/ysmood/goapi_test.AddInterfaces")
}